@goto INITIALIZE
:BEGIN
if "%~0"=="RTX_REBUILD.cmd" set is_nested_call=1

if !is_nested_call!==1 goto START
cls
echo RE-BUILDING RTX-PROJECTS ΔΔ "DEBUG":
echo ίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίί

:START
call :REBUILD_PROJECT "user_rtx.core"
if !errorlevel! neq 0 goto FAILURE

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

call :RESTORE_DIR
if !errorlevel! neq 0 goto ERROR
::==================================================================================================

:SUCCESS
if !is_nested_call!==1 goto FINISH
echo.
echo άάά REBUILD RTX-PROJECTS (DEBUG) ΔΔ SUCCESS άάά
goto FINISH

:FAILURE
if !is_nested_call!==1 goto ERROR
echo.
echo άάά ## REBUILD RTX-PROJECTS (DEBUG) ΔΔ FAILURE άάά
goto ERROR

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

:REBUILD_PROJECT
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING
if "%~1"=="" (
echo ## Empty parameter for REBUILD_PROJECT subroutine.
goto END_LOCAL_RETURN_ERROR )

call :CHANGE_DIR "%initial_cd%\%~1"
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR

call REBUILD.cmd
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR

echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
goto END_LOCAL_RETURN_SUCCESS

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

:CHANGE_DIR
if "%~1"=="" (
echo ## Empty parameter for CHANGE_DIR subroutine.
goto RETURN_ERROR )
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%"
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
goto :EOF
echo.
echo ## Can not restore directory to: "%initial_cd%".
goto TERMINATED

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

:EXEC_CMD
call :SET_ERROR
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING
echo.
echo ^>^> %* ...
%*
if %errorlevel% neq 0 goto END_LOCAL_RETURN_ERROR
goto END_LOCAL_RETURN_SUCCESS

::==================================================================================================
:INITIALIZE
@echo off
call :SET_ERROR
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING
set dp_0=%~dp0
set param_1=%~1
set param_2=%~2
set param_3=%~3
set param_4=%~4
set param_5=%~5
set param_6=%~6
set param_7=%~7
set param_8=%~8
set param_9=%~9
call :SET_ERROR
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
set is_nested_call=0
set initial_cd=!CD!
goto BEGIN
::==================================================================================================
:CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING
echo.
echo ## Can not switch on simple command processing (without delayed expansion).
goto TERMINATED
:CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
echo.
echo ## Can not switch on extended command processing.
goto TERMINATED
::--------------------------------------------------------------------------------------------------
rem :
rem echo.
rem echo ## 
rem goto TERMINATED
::--------------------------------------------------------------------------------------------------
:TERMINATED
echo ## CMD-execution is terminated (in "%~dpnx0").
echo 
pause
EXIT 255
::==================================================================================================
:RESET_ERROR
set errorlevel=0
set errorlevel=
goto :EOF
:SET_ERROR
verify # 2>nul
goto :EOF
::==================================================================================================
:RETURN_SUCCESS
set errorlevel=0
set errorlevel=
goto :EOF
:RETURN_ERROR
verify # 2>nul
goto :EOF
:END_LOCAL_RETURN_SUCCESS
endlocal
goto RETURN_SUCCESS
:END_LOCAL_RETURN_ERROR
endlocal
goto RETURN_ERROR
::==================================================================================================
:FINISH
if !is_nested_call!==0 if not "!$cmd_plain!"=="1" (
echo.
pause )
endlocal
endlocal
goto RETURN_SUCCESS
:ERROR
if !is_nested_call!==0 (
echo 
if not "%$cmd_plain%"=="1" pause )
endlocal
endlocal
goto RETURN_ERROR
::==================================================================================================
