@ECHO OFF
set  installDir=%1
rem %N is the Nth parameter
rem %Var% is Var's value, :uuu is "label uuu"

if "%1"==""  goto noInstallDir
  echo install directory: %installDir%  
  goto installme
:noInstallDir
   set /p "installDir=Please type destination directory in full directory name: "
   echo install directory: %installDir%

:installme

rem all space removed at begining and end
set installDir=%installDir: =% 

rem 2nd and len=2 so , it should be ":\"
set ExtractStr=%installDir:~1,2%  
 
set ConStr=:\
if  not %ExtractStr% == %ConStr% (echo Wrong directory: You must use full directory name, including drive name such as C: or D:, or E:... and \ . Fail to install. && pause && exit 1) 

IF exist %installDir% ( echo %installDir% exists ) ELSE ( mkdir %installDir% && echo %installDir% created)

rem for /f %%I in (.) do set CurrDirName=%%~nxI  => no drive name
rem echo my current dir: %CurrDirName%
rem  echo %cd%   =>%cd% contains full name including drive

rem pause 

rem 4 sec delay
rem ping 127.0.0.1 -n 5 > nul 


echo function startup > startup.m
echo   disp('Embedded C code creator from Dafulai Electronics Inc already installed!') >> startup.m
echo   setenv("embeddedCreatorInstallDir","%installDir%"); >> startup.m
rem echo   addpath("%installDir%");  >> startup.m
echo end >> startup.m


echo function addMyPath > addMyPath.m
echo MyPathStr=strtrim("%installDir%"); >> addMyPath.m
echo addpath(MyPathStr); >> addMyPath.m
echo savepath >> addMyPath.m
echo setEmbeddedLicense >> addMyPath.m
echo quit >> addMyPath.m
echo end >> addMyPath.m

pause

rem got matlab install dir
SETLOCAL EnableDelayedExpansion
SET "MATLAB_EXE_PATH="

REM --- PHASE 1: Fast Search Common MATLAB Roots ---
ECHO Starting fast search in common MATLAB installation folders...

REM Explicitly define the paths to check, one by one, to handle spaces correctly
SET "PATH1=C:\Program Files\MATLAB"
SET "PATH2=D:\Program Files\MATLAB"
SET "PATH3=E:\Program Files\MATLAB"

ECHO We will search matlab.exe install directory, it will take time.

REM Iterate through the defined paths
FOR %%P IN ("!PATH1!" "!PATH2!" "!PATH3!") DO (
    IF NOT DEFINED MATLAB_EXE_PATH (
        ECHO Checking standard location: %%~P...
        IF EXIST "%%~P" (
            REM Use WHERE command limited to the specific root directory with quotes
            FOR /F "delims=" %%F IN ('where /R "%%~P" matlab.exe 2^>nul') DO (
                SET "MATLAB_EXE_PATH=%%F"
                GOTO FoundMatlab
            )
        )
    )
)
REM --- PHASE 2: Fallback Full Drive Search ---
ECHO We didn't find matlab.exe in possible default folder, we will search entire directory, it will take longer time.
IF NOT DEFINED MATLAB_EXE_PATH (
    ECHO Fast search failed. Starting slow, full drive search on C: and D: drives as a fallback...
    ECHO This may take a long time to complete.

    SET "SEARCH_DRIVES=C:\ D:\"
    FOR %%D IN (%SEARCH_DRIVES%) DO (
        IF NOT DEFINED MATLAB_EXE_PATH (
            ECHO Searching entire drive %%D...
            FOR /F "delims=" %%F IN ('where /R %%D matlab.exe 2^>nul') DO (
                SET "MATLAB_EXE_PATH=%%F"
                GOTO FoundMatlab
            )
        )
    )
)

:FoundMatlab
IF DEFINED MATLAB_EXE_PATH (
        
    REM Extract only the directory path from the full file path
    rem FOR %%G IN ("!MATLAB_EXE_PATH!") DO SET "MATLAB_BIN_PATH=%%~dpG"
    
    

    REM Temporarily add the bin path to the current command session's PATH
    rem SET "PATH=%PATH%;!MATLAB_BIN_PATH!"
    
    REM Launch the executable using quotes
    rem "!MATLAB_EXE_PATH!"    
) ELSE (
    ECHO.
    ECHO Error: matlab.exe not found anywhere on C:, D:, or E: drives.
    ECHO Please ensure MATLAB is installed correctly.
    PAUSE
    exit 1

)

rem ENDLOCAL



rem matlab -nodesktop -nosplash -r addMyPath
"!MATLAB_EXE_PATH!" -nodesktop -nosplash -r addMyPath
rem "!MATLAB_EXE_PATH!"
xcopy . %installDir% /e /-y


echo Embedded C Code creator installed successfully. 
echo Please wait for a while, system is setting up Enbedded C creator License.
echo If you select cancel License key, you only can use demo mode.
echo In demo mode, it has 15 minutes normal running.  After 15 minutes, Modbus Server/Client (including debug function) will stop, the other functions are OK.
ENDLOCAL

pause




