@goto INITIALIZE
:BEGIN
if "!P1$$!"=="(NestedCall)" set is_nested_call$$=1

if !is_nested_call$$!==1 goto START
cls
echo LAUNCHING SSMS:
echo ßßßßßßßßßßßßßßß

:START
set IdeRootFile$$=!DP0$$!$ide_root.sql
if not exist "!IdeRootFile$$!" goto IDE_ROOT_NOT_FOUND

if not defined SSMS_ExePath   goto SSMS_EXE_PATH_NOT_DEF
if not exist "!SSMS_ExePath!" goto SSMS_EXE_PATH_NOT_FOUND

call $SQLTRANS "[SettingsOnly]"
if !errorlevel! neq 0 goto ERROR

if !is_nested_call$$!==1 goto SKIP_VARIABLES_PRINT
echo.
echo ž $sql_server = !$sql_server!
echo ž $sql_database = !$sql_database!
if defined $sql_user echo ž $sql_user = !$sql_user!
echo.
echo ž $sql_translation_folder = !$sql_translation_folder!
echo ž $sqltrans_timestamp = !$sqltrans_timestamp!
echo.
echo ž SQLAUX_ROOT = !SQLAUX_ROOT!
echo ž ServiceScriptRoot_AUX = !ServiceScriptRoot_AUX!
echo.
echo ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
:SKIP_VARIABLES_PRINT

set SsmsCommand$$="!SSMS_ExePath!" -S "!$sql_server!" -d "!$sql_database!"
if defined $sql_user set SsmsCommand$$=!SsmsCommand$$! -U !$sql_user!
if not defined $sql_user set SsmsCommand$$=!SsmsCommand$$! -E
set SsmsCommand$$=!SsmsCommand$$! "!IdeRootFile$$!"

call :EXEC_CMD start "" /max !SsmsCommand$$!
if !errorlevel! neq 0 goto ERROR

if !is_nested_call$$!==1 goto :EOF
echo.
echo ÜÜÜ DONE ÜÜÜ
goto :EOF

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

:SSMS_EXE_PATH_NOT_DEF
echo.
echo ## Environment variable not defined: "SSMS_ExePath".
goto ERROR

:SSMS_EXE_PATH_NOT_FOUND
echo.
echo ## Executable path not found: "!SSMS_ExePath!".
echo ## (Environment variable: "SSMS_ExePath".)
goto ERROR

:IDE_ROOT_NOT_FOUND
echo.
echo ## File not found: "!IdeRootFile$$!".
goto ERROR

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

: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 DP0$$=%~dp0
set P1$$=%~1
set P2$$=%~2
set P3$$=%~3
set P4$$=%~4
set P5$$=%~5
set P6$$=%~6
set P7$$=%~7
set P8$$=%~8
set P9$$=%~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 (
echo.
pause )
endlocal
endlocal
goto RETURN_SUCCESS
:ERROR
if !is_nested_call$$!==0 (
echo 
pause )
endlocal
endlocal
goto RETURN_ERROR
::==================================================================================================
