@echo off
setlocal
set initial_cd=%CD%
verify # 2>nul
setlocal enableextensions enabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
call $SQLTRANS "(SettingsOnly)"
if !errorlevel! neq 0 goto ERROR

if "%~0"=="TRANSLATE_PROGRAMMATICS.cmd" goto START
cls

echo TRANSLATION OF PROGRAMMATICS ():
echo ίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίίί
echo.
echo ώ SQL-server  : !$sql_server!
echo ώ SQL-database: !$sql_database!
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
echo ΝΝ STARTING THE SCRIPT(S) ON SQL-SERVER: ΝΝΝ (TRANSLATION CONFIRMATION) ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
echo.
call $CONFIRMPAUSE.cmd
if !errorlevel! neq 0 goto ERROR

:START
call ^^!DROP_PROGRAMMATICS.cmd
if !errorlevel! neq 0 goto FAILURE

rem call :PROCESS_FOLDER "Table types" "TRANSLATE_TYPES"
rem if !errorlevel! neq 0 goto FAILURE

rem call :PROCESS_FOLDER "Auxiliary tables" "TRANSLATE_TABLES"
rem if !errorlevel! neq 0 goto FAILURE

rem call :PROCESS_FOLDER "Functions" "TRANSLATE_FUNCTIONS"
rem if !errorlevel! neq 0 goto FAILURE

rem call :PROCESS_FOLDER "Views" "TRANSLATE_VIEWS"
rem if !errorlevel! neq 0 goto FAILURE

call :PROCESS_FOLDER "Base procedures" "TRANSLATE_BASE_PROCEDURES"
if !errorlevel! neq 0 goto FAILURE

call :PROCESS_FOLDER "Application procedures" "TRANSLATE_APP_PROCEDURES"
if !errorlevel! neq 0 goto FAILURE

call :RESTORE_DIR
if !errorlevel! neq 0 goto FAILURE

call LIST_PROGRAMMATICS.cmd
if !errorlevel! neq 0 goto FAILURE

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

:SUCCESS
if "%~0"=="TRANSLATE_PROGRAMMATICS.cmd" goto FINISH
echo.
echo άάά TRANSLATE PROGRAMMATICS ΔΔ SUCCESS άάά
goto FINISH

:FAILURE
if "%~0"=="TRANSLATE_PROGRAMMATICS.cmd" goto ERROR
echo.
echo άάά ## TRANSLATE PROGRAMMATICS ΔΔ FAILURE άάά
goto ERROR

::==================================================================================================
:CANT_SWITCH_ON_EXTENDED_CMD_PROCESSING
echo.
echo ## Can not switch on extended command processing.
goto ERROR
::--------------------------------------------------------------------------------------------------
:CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
echo.
echo ## Can not switch on simple command processing.
verify # 2>nul
goto :EOF
::==================================================================================================

:PROCESS_FOLDER
verify # 2>nul
setlocal enableextensions disabledelayedexpansion
if errorlevel 1 goto CANT_SWITCH_ON_SIMPLE_CMD_PROCESSING_RETURN_ERROR
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
call :CHANGE_DIR "%initial_cd%\%~1"
if %errorlevel% neq 0 goto :EOF
call %~2.cmd
if %errorlevel% neq 0 goto :EOF
::-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo.
echo ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ
goto :EOF

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

: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%".
set SetLocalCheck$$=
if "!SetLocalCheck$$!"=="" (
echo ## Batch processing is terminated^^!
) else (
echo ## Batch processing is terminated!
)
echo 
pause
EXIT 255

::==================================================================================================
:RESET_ERROR
set errorlevel=0
set errorlevel=
goto :EOF
::==================================================================================================
:ERROR
if "%~0"=="TRANSLATE_PROGRAMMATICS.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"=="TRANSLATE_PROGRAMMATICS.cmd" (
echo.
if not "%$cmd_plain%"=="1" pause )
call :RESTORE_DIR
if %errorlevel% neq 0 goto ERROR_EXIT
endlocal
call :RESET_ERROR
goto :EOF
::==================================================================================================
