@echo off
setlocal
set initial_cd=%CD%

if "%~0"=="REBUILD_ALL_R.cmd" goto START
cls
echo RE-BUILDING ALL PROJECTS ΔΔ "RELEASE":
echo ίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίί

:START
call :REBUILD_PROJECT "ConsoleCalculator"
if %errorlevel% neq 0 goto FAILURE

call :REBUILD_PROJECT "LnkConverter utility"
if %errorlevel% neq 0 goto FAILURE

call :REBUILD_CPP_PROJECT "XDeviceAccess.InterSys"
if %errorlevel% neq 0 goto FAILURE

rem call :REBUILD_PROJECT ""
rem if %errorlevel% neq 0 goto FAILURE

call :RESTORE_DIR
::==================================================================================================

:SUCCESS
if "%~0"=="REBUILD_ALL_R.cmd" goto FINISH
echo.
echo άάά REBUILD ALL PROJECTS (RELEASE) ΔΔ SUCCESS άάά
goto FINISH

:FAILURE
if "%~0"=="REBUILD_ALL_R.cmd" goto ERROR
echo.
echo άάά ## REBUILD ALL PROJECTS (RELEASE) ΔΔ FAILURE άάά
goto ERROR

::==================================================================================================

:REBUILD_PROJECT
if "%~1"=="" (
echo ## Empty parameter for REBUILD_PROJECT subroutine.
goto RETURN_ERROR )
call :CHANGE_DIR "%initial_cd%\%~1"
if %errorlevel% neq 0 goto :EOF

call REBUILD_R.cmd
if %errorlevel% neq 0 goto :EOF

:COMPLETE_REBUILD_PROJECT
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
goto :EOF

:REBUILD_CPP_PROJECT
if "%~1"=="" (
echo ## Empty parameter for REBUILD_CPP_PROJECT subroutine.
goto RETURN_ERROR )
call :CHANGE_DIR "%initial_cd%\%~1"
if %errorlevel% neq 0 goto :EOF

call REBUILD_X64_R.cmd
if %errorlevel% neq 0 goto :EOF

call REBUILD_X86_R.cmd
if %errorlevel% neq 0 goto :EOF

call CLEAN_INTERMEDIATE.cmd
if %errorlevel% neq 0 goto :EOF
goto COMPLETE_REBUILD_PROJECT

::==================================================================================================

:CHANGE_DIR
call :RESET_ERROR
echo.
echo * Changing directory to: "%~1" ...
cd /d "%~1"
goto :EOF

:RESTORE_DIR
call :RESET_ERROR
if /i "%CD%"=="%initial_cd%" goto :EOF
echo.
echo * Restoring directory to: "%initial_cd%" ...
cd /d "%initial_cd%"
if /i "%CD%"=="%initial_cd%" goto :EOF
echo.
echo ## === FATAL ERROR: ===
echo ## Can not restore directory to: "%initial_cd%".
echo ## Batch processing is terminated!
echo 
pause
EXIT 255

::==================================================================================================
:RESET_ERROR
set errorlevel=0
set errorlevel=
goto :EOF
::==================================================================================================
:RETURN_SUCCESS
set errorlevel=0
set errorlevel=
goto :EOF
:RETURN_ERROR
verify # 2>nul
goto :EOF
rem :END_LOCAL_RETURN_SUCCESS
rem endlocal
rem goto RETURN_SUCCESS
rem :END_LOCAL_RETURN_ERROR
rem endlocal
rem goto RETURN_ERROR
::==================================================================================================
:ERROR
if "%~0"=="REBUILD_ALL_R.cmd" ( call :RESTORE_DIR
) else if "%$cmd_plain%"=="1" ( call :RESTORE_DIR
echo 
) else (
echo 
pause
call :RESTORE_DIR )
:ERROR_EXIT
endlocal
verify # 2>nul
goto :EOF
::--------------------------------------------------------------------------------------------------
:FINISH
if not "%~0"=="REBUILD_ALL_R.cmd" (
echo.
if not "%$cmd_plain%"=="1" pause )
call :RESTORE_DIR
if %errorlevel% neq 0 goto ERROR_EXIT
endlocal
call :RESET_ERROR
goto :EOF
::==================================================================================================
