@echo off
:: Получаем аргументы из командной строки
:: %1 - includes_USER
:: %2 - code_USER
:: %3 - режим (например, debug)

:: Аргументы:
:: %1 — includes строка (в кавычках)
:: %2 — sources строка
:: %3 — defines строка
:: %4 — режим компиляции (debug/release)

:: Сохраняем как переменные
set includes_USER=%~1
set code_USER=%~2
set defines_USER=%~3
set compil_mode=%~4

:: Заменяем __EQ__ на =
set defines_USER=%defines_USER:__EQ__==%


set defines_WRAPPER=-D"MATLAB"^ -D"__sizeof_ptr=8"

:: -------------------------USERS PATHS AND CODE---------------------------
::-------------------------------------------------------------------------


:: -----------------------MCU LIBRARIES & SIMULATOR------------------------
:: -----MCU LIBRARIES STUFF----
:: заголовочные файлы
set includes_MCU= -I".\MCU_STM32F1xx_Matlab"^
 -I".\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_SIMULINK"^
 -I".\MCU_STM32F1xx_Matlab\Drivers\CMSIS"^
 -I".\MCU_STM32F1xx_Matlab\Drivers\CMSIS\Device\STM32F1xx"^
 -I".\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Inc"^
 -I".\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Inc\Legacy"
  
:: код библиотек МК, переделанный для матлаб
set code_MCU= .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_adc.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_adc_ex.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c

:: .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ramfunc.c^
:: .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c^
:: .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c^
:: .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c^

:: --------MCU SIMULATOR-------
:: код, которая будет симулировать перефирию МК в симулинке
set code_MCU_Sim= .\MCU_STM32F1xx_Matlab\stm32f1xx_matlab_conf.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_SIMULINK\stm32f1xx_matlab_gpio.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_SIMULINK\stm32f1xx_matlab_tim.c^
 .\MCU_STM32F1xx_Matlab\Drivers\STM32F1xx_SIMULINK\stm32f1xx_periph_registers.c 
::-------------------------------------------------------------------------


:: -------------------------WRAPPER PATHS AND CODE---------------------------
:: оболочка, которая будет моделировать работу МК в симулинке
set includes_WRAPPER= -I".\MCU_Wrapper"
set code_WRAPPER= .\MCU_Wrapper\MCU.c^
 .\MCU_Wrapper\mcu_wrapper.c^
 .\MCU_Wrapper\app_wrapper.c
::-------------------------------------------------------------------------


:: ---------------------SET PARAMS FOR MEX COMPILING-----------------------
:: --------ALL INCLUDES--------
set includes= %includes_USER% %includes_MCU% %includes_WRAPPER%
set codes= %code_WRAPPER% %code_USER% %code_MCU% %code_MCU_Sim%
set defines= %defines_USER% %defines_WRAPPER%
:: -------OUTPUT FOLDER--------
set output= -outdir "."

:: если нужен дебаг, до запускаем run mex с припиской debug
IF %compil_mode%==[debug] (set debug= -g)
::-------------------------------------------------------------------------


::------START COMPILING-------    
echo Compiling...


echo ===========================
echo INCLUDES:
echo USER:
for %%f in (%includes_USER%) do (
    echo   %%f
)
echo INTERNAL:
for %%f in (%includes_MCU%) do (
    echo   %%f
)
for %%f in (%includes_WRAPPER%) do (
    echo   %%f
)

echo ===========================
echo SOURCES:
echo USER:
for %%f in (%code_USER%) do (
    echo   %%f
)
echo INTERNAL:
for %%f in (%code_WRAPPER%) do (
    echo   %%f
)
for %%f in (%code_MCU_Sim%) do (
    echo   %%f
)
for %%f in (%code_MCU%) do (
    echo   %%f
)

echo ===========================
echo DEFINES:
echo USER:
for %%d in (%defines_USER%) do (
    echo   %%d
)
echo INTERNAL:
for %%f in (%defines_WRAPPER%) do (
    echo   %%f
)
echo ===========================
echo MODE: %compil_mode%
echo ===========================

mex %output% %defines% %includes% %codes% %debug%
echo %DATE% %TIME%