@Echo off
@if not defined CmdCurRoot_DP  @Echo off
rem Description: Checks ImplRepo Service
rem #New: 07.11.2016 / J.Fes
rem #LUp: 19.02.2020 / J.Fes add wait 2
rem #LUp: 04.06.2020 / J.Fes add control case1

setlocal
 if defined CmdCurRoot_DP goto :next
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%

:next
 rem set zzTCtmpRepoService_DP=%CmdCurRoot_DP%\..\zzTCtmpRepoService
 call Parse_Absolut_DP zzTCtmpRepoService_DP "%CmdCurRoot_DP%\..\zzTCtmpRepoService\"
 set TC_tao_imr_activator_IsInStart_DPNE=%zzTCtmpRepoService_DP%\TC_tao_imr_activator_IsInStart.cmd

 rem zzTCtmpRepoService_DP
 if not exist "%zzTCtmpRepoService_DP%" md "%zzTCtmpRepoService_DP%"
 Call Echo "#zzTCtmpRepoService_DP..........: %zzTCtmpRepoService_DP%"

 REM #Doc: get ImplRepo Service Status
 call :ImplRepo_Service_GetStatus

 REM #Doc: OK best case Service is UP and not in start - process
 if "%ImplRepo_ServiceStatus%" equ "1" if not exist "%TC_tao_imr_activator_IsInStart_DPNE%" goto :case1

 REM #Doc: Use Start Repro Service now because TC_tao_imr_activator_IsInStart_DPNE not exist
 if "%ImplRepo_ServiceStatus%" neq "1" if not exist "%TC_tao_imr_activator_IsInStart_DPNE%" goto :case2

 REM #toDO: wenn der Repor sevice nicht aktiv ist aber die Datei existiert dann X sec warten neuer test ob nun der service da ist wenn nicht dann datei llöschen und call :ImplRepo_Service_StartNow
 if "%ImplRepo_ServiceStatus%" equ "0" if exist "%TC_tao_imr_activator_IsInStart_DPNE%" goto :case3

 REM #Doc: Use Wait Start Repro Service because ImplRepo_ServiceStatus=1 and TC_tao_imr_activator_IsInStart_DPNE exist
 if "%ImplRepo_ServiceStatus%" equ "1" if exist "%TC_tao_imr_activator_IsInStart_DPNE%" goto :case4

:case1
 goto :next

:case2
 call :ImplRepo_Service_StartNow
 goto :next

:case3
 call :ImplRepo_Service_WaitCheckStartNow
 goto :next

:case4
 call :ImplRepo_Service_WaitUntilServiceUp
 goto :next


:next
 REM #Doc: Info at the end about if Reproservice is running
 if "%ImplRepo_ServiceStatus%" equ "1" (
  call :ImplRepo_Service_MsgReproServiceIsRunning
 ) else (
  call :ImplRepo_Service_MsgReproServiceIsNotRunning
 )

goto :end

REM ---------------------------------------------------------------------------
:ImplRepo_Service_GetStatus
 rem #New03.06.2020 gets Info if repro service is running
 call ProgIs_Running.cmd tao_imr_activator ImplRepo_ServiceStatus "EchoOff"
 Call Echo "Chk Repo Service Status.........: %ImplRepo_ServiceStatus%"
 rem call ProgIs_Running.cmd tao_imr_activator ImplRepo_ServiceStatus
 goto :eof

REM ---------------------------------------------------------------------------
:ImplRepo_Service_WaitCheckStartNow
 REM #Doc: Case if Repor service not active but %TC_tao_imr_activator_IsInStart_DPNE%" exist
 REM #Doc: wenn der Repro sevice nicht aktiv ist aber die Datei existiert dann X sec warten


 REM #Step: Wait 5 seconds
 set toRun_Wait=5
 Call Echo "ImplRepo_Service................: Wait [%toRun_Wait% sec] if ImplRepo Service is NowUp"
 timeout /T %toRun_Wait%

 REM #Step: get service status
 call :ImplRepo_Service_GetStatus
 REM #Doc: if now service is up then end
 if "%ImplRepo_ServiceStatus%" equ "1" goto :myend

 REM #Step: delete "%TC_tao_imr_activator_IsInStart_DPNE%"
 if exist "%TC_tao_imr_activator_IsInStart_DPNE%" del "%TC_tao_imr_activator_IsInStart_DPNE%"
 REM #Step: get start the service now
 call :ImplRepo_Service_StartNow

 REM neuer test ob nun der service da ist wenn nicht dann datei llöschen und call :ImplRepo_Service_StartNow
:myend
 goto :eof

REM ---------------------------------------------------------------------------
:ImplRepo_Service_WaitUntilServiceUp
 set CountN=0
 set CountNMax=10
:ImplRepo_Service_WaitUntilServiceUpRedo
 set /A CountN=%CountN% + 1
 set toRun_Wait=1
 Call Echo "ImplRepo_Service................: Wait [%toRun_Wait% sec] (%CountN%/%CountNMax%) until ImplRepo Service is Up"
 timeout /T %toRun_Wait%
 call :ImplRepo_Service_GetStatus
 if exist "%TC_tao_imr_activator_IsInStart_DPNE%" if %CountN% leq %CountNMax% goto :ImplRepo_Service_WaitUntilServiceUpRedo
 goto :eof



REM ---------------------------------------------------------------------------
:ImplRepo_Service_StartNow
 Rem #NEW: 19.02.2020/J.Fes: add

 set TC_TMP_DIR=%zzTCtmpRepoService_DP%
 set TEMP=%zzTCtmpRepoService_DP%
 set TMP=%zzTCtmpRepoService_DP%
 set TCSERVER_USER_DIR=%zzTCtmpRepoService_DP%

 if exist "%TC_tao_imr_activator_IsInStart_DPNE%" del "%TC_tao_imr_activator_IsInStart_DPNE%"

 Call Echo "ImplRepo_Service................: Start ImplRepo_Service new" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM #Doc: files is Autogenerater via %~0 on %ComuterName%" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "set ImplRepo_Service_StartNew_Date=%Date% " "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "set ImplRepo_Service_StartMew_Time=%time% " "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM Value: TC_TMP_DIR=%TC_TMP_DIR%" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM Value: temp=%temp%" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM Value: tmp=%tmp%" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM Value: TC_ROOT=%TC_ROOT%" "%TC_tao_imr_activator_IsInStart_DPNE%"
 Call Echo "REM Value: start_imr.bat=%TC_ROOT%\iiopservers\start_imr.bat" "%TC_tao_imr_activator_IsInStart_DPNE%"

 cd /d "%zzTCtmpRepoService_DP%"
 set toRun=start "ImplRepo Service - CMM - Start:[%Date% %time:~0,5%]" /D "%zzTCtmpRepoService_DP%" cmd /c "%TC_ROOT%\iiopservers\start_imr.bat"
 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  Echo #Debug:%~0 [before Start toRun]
  Echo %toRun%
  set toRunForPrint=%toRun:"=%
  call Echo " %~0 toRun:%toRunForPrint% " "" "NOECHO"
  pause
 :JumbOverDebug
 rem -- #Doc: Repro Server wird hier gestartet
 %toRun%

 set toRun_Wait=10
 Call Echo "ImplRepo_Service................: Sleep [%toRun_Wait% sec] after Start ImplRepo Service"
 timeout /T %toRun_Wait%

 if exist "%TC_tao_imr_activator_IsInStart_DPNE%" del "%TC_tao_imr_activator_IsInStart_DPNE%"
 rem -- #Dco: at the End check if ImplRepo Service is now running
 call :ImplRepo_Service_GetStatus
goto :eof

REM ---------------------------------------------------------------------------
:ImplRepo_Service_MsgReproServiceIsRunning
  Call Echo "ImplRepo_Service................: ImplRepo_Service OK is running"
goto :eof

:ImplRepo_Service_MsgReproServiceIsNotRunning
  call ErrorHandel "PS" "ImplRepo Service" "[ImplReproService:Not stated cannot continue please check ISSUE]"
  pause
goto :eof

REM ---------------------------------------------------------------------------
:end
if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 call Echo "---------------------------------------------------------------------------"
 call Echo "#Debug..........................: %~0"
 call Echo "ImplRepo_Service................: END"
 pause
:JumbOverDebug

 rem in any case delete at the end file TC_tao_imr_activator_IsInStart_DPNE
 if exist "%TC_tao_imr_activator_IsInStart_DPNE%" del "%TC_tao_imr_activator_IsInStart_DPNE%"

endlocal

