@Echo off

rem begin
rem #LUp: 22.07.2022
 mode CON COLS=180 LINES=45
 set LUp=28.02.2017 10:13
 set LUp=28.03.2017 12:00/J.Fes
 set LUp=14.07.2022 18:00/J.Fes
 set titletxt=Setup 180 SQLCMD.exe [%Date%/%time:~0,8%]

 if "%~1" equ ":InstallRun" goto :InstallRun

 rem ---------------------------------------------------------------------------
:InstallPrepare
 set titletxt=Install Prepare:%titletxt%
 title %titletxt%

 rem --#Doc: Ermitteln des Pfades in dem das script gestartet wird
 Set CmdCurRoot_DP=%~dp0
 Set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%
 call "%CmdCurRoot_DP%\ColorSet.cmd" "ExtractLi"

 Call :SqlCmdBIN_DP_GET

 Set SetupL_DP=%temp%\$%~n0
 if not exist "%SetupL_DP%" md "%SetupL_DP%"

 Call "%CmdCurRoot_DP%\Echo" "############################################################################"
 Call "%CmdCurRoot_DP%\Echo" "Start Setup....................: SQLCMD.exe"
 Call "%CmdCurRoot_DP%\Echo" "On computer....................: \\%ComputerName%"
 Call "%CmdCurRoot_DP%\Echo" "At Date/Time...................: %Date% %time:~0,8"
 Call "%CmdCurRoot_DP%\Echo" "Version........................: %LUp%"

:Menue
 Echo.
 Echo --------------------------------------------------------------------------
 Echo  S = Start Setup
 Echo  Q = Quit Setup
 Echo.
 set doNext=
 set /P doNext= select Action:

 if /I "%doNext%" equ "Q"   (
                           Call "%CmdCurRoot_DP%\Echo" "Setup..........................: stop nothing is changed"
                           pause
                           rem stop
                           Exit
                          )
 if /I "%doNext%" equ "S"   goto :StartSetup
 goto :Menue

:StartSetup
 Call "%CmdCurRoot_DP%\Echo" "Setup..........................: Start: %Date% %time:~0,8"
 Call "%CmdCurRoot_DP%\Echo" "############################################################################"
 Call "%CmdCurRoot_DP%\Echo" "setup source from dir..........: %CmdCurRoot_DP%"
 Call "%CmdCurRoot_DP%\Echo" "setup source to dir............: %SetupL_DP%"

 copy "%CmdCurRoot_DP%\*.msi" "%SetupL_DP%\*.*"  >nul 2>nul
 copy "%CmdCurRoot_DP%\*.cmd" "%SetupL_DP%\*.*"  >nul 2>nul
 copy "%CmdCurRoot_DP%\*.exe" "%SetupL_DP%\*.*"  >nul 2>nul

 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Start "" /D "%SetupL_DP%" %~0 :InstallRun
 goto :end

rem ----------------------------------------------------------------------------
:InstallRun
 call "%SetupL_DP%\ColorSet.cmd" "IsExecuting"
 set titletxt=Install Run:%titletxt%
 title %titletxt%

 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call "%CmdCurRoot_DP%\Echo" "Descripton.....................: Install Run SQLCMD.exe inc. required tools"
 Call "%CmdCurRoot_DP%\Echo" "from setup dir.................: %SetupL_DP%"

 Call :SqlCmdBIN_DP_GET
 if     exist "%SqlCmdEXE_DPNE%" goto :next1
 if not exist "%SqlCmdEXE_DPNE%" goto :next2

 :next1
 Call "%CmdCurRoot_DP%\Echo" "SQLCMD.exe.....................: Installed allready"
 goto :PathCheck


 :next2

 rem silent installation install with script install with msiexec
 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call "%CmdCurRoot_DP%\Echo" "#Step 1/3......................: install %SetupL_DP%\MsODBCsql.msi"
 Call msiexec /i "%SetupL_DP%\msodbcsql.msi"  /qb IACCEPTMSODBCSQLLICENSETERMS=YES


 Call "%CmdCurRoot_DP%\Echo" "#Step 2/3......................: install %SetupL_DP%\MsSqlCmdLnUtils.msi"
 Call msiexec /i "%SetupL_DP%\MsSqlCmdLnUtils.msi"  /qb IACCEPTMSSQLCMDLNUTILSLICENSETERMS=YES


:PathCheck
 Call :SqlCmdBIN_DP_GET

 Call "%CmdCurRoot_DP%\Echo" "#Step 3/3 check path contains..: %SqlCmdBIN_DP%"
 @echo %PATH% | find /c /i "%SqlCmdBIN_DP%"
 if not errorlevel 1 goto :next1
 Call "%CmdCurRoot_DP%\Echo" "not includet ..................: %SqlCmdBIN_DP%"
 Call "%CmdCurRoot_DP%\Echo" "PathOrig (before)..............: %Path%"
 Setx PATH "%Path%;%SqlCmdBIN_DP%" -m
 set path=%Path%;%SqlCmdBIN_DP%
 Call "%CmdCurRoot_DP%\Echo" "PathOrig (after)...............: %Path%;%SqlCmdBIN_DP%"
 goto :next2

:next1
 Call "%CmdCurRoot_DP%\Echo" "allready includet in...........: %Path%"

:next2
 Call :SqlCmdBIN_DP_GET

 if     exist "%SqlCmdEXE_DPNE%" call "%SetupL_DP%\ColorSet.cmd" "OK"
 if not exist "%SqlCmdEXE_DPNE%" call "%SetupL_DP%\ColorSet.cmd" "ERR"

 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call "%CmdCurRoot_DP%\Echo" "SqlCmdBIN_DP...................: %SqlCmdBIN_DP%"
 Call "%CmdCurRoot_DP%\Echo" "SqlCmdEXE_DPNE.................: %SqlCmdEXE_DPNE%"
 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call "%CmdCurRoot_DP%\Echo" "End Setup......................: SQLCMD.exe"
 Call "%CmdCurRoot_DP%\Echo" "on computer....................: %ComputerName%"
 Call "%CmdCurRoot_DP%\Echo" "At Date/Time...................: %Date% %time:~0,8%"

 pause
 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call "%CmdCurRoot_DP%\Echo" "Test...........................: via SQLCMD.exe -?"
 Call "%CmdCurRoot_DP%\Echo" "Info...........................: if SQLCMD.exe help is listed then SQLCMD is installed"
 Call "%CmdCurRoot_DP%\Echo" "****************************************************************************"
 call SQLCMD.exe -?
 Call "%CmdCurRoot_DP%\Echo" "****************************************************************************"
 Call "%CmdCurRoot_DP%\Echo" "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 call "%SetupL_DP%\ColorSet.cmd" "End"
 pause
 goto :end

:SqlCmdBIN_DP_GET
 set SqlCmd_DPNE=
REM  set SqlCmdBIN_DP=%ProgramFiles%\Microsoft SQL Server\110\Tools\Binn
 set SqlCmdBIN_DP=%ProgramW6432%\Microsoft SQL Server\110\Tools\Binn
 set SqlCmdBIN_DP=%ProgramW6432%\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn
 if not exist "%SqlCmdBIN_DP%" set SqlCmdBIN_DP=
 if defined SqlCmdBIN_DP if exist "%SqlCmdBIN_DP%\SQLCMD.exe" set SqlCmdEXE_DPNE=%SqlCmdBIN_DP%\SQLCMD.exe
:goto :eof
    
:end
