@echo off

rem Doku:
rem Organizes Start of Application PLMJobManger
rem JobServer and JobClient Computers can be startet via this script
rem 10.09.2014 J.Fes #New: First Version
rem 11.11.2014 J.Fes #LUp: default Start Times reduced
rem 10.12.2014 J.Fes #LUp: Optimised
rem 08.02.2017 J.Fes #LUp: add Usage ProgStart_If_NotRuning.cmd

 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%

 set ProgKill_log_DP=%CmdCurRoot_DP%\_LogFiles
 if not exist "%ProgKill_log_DP%" md "%ProgKill_log_DP%"
 set ProgKill_log_DPNE=%ProgKill_log_DP%\AutoStartKillPros_Log.txt

 REM #Doc: run JobManagerStartCustom.cmd to get JobMgr_RootL_DP
 if exist "%CmdCurRoot_DP%\JobManagerStartCustom.cmd" call "%CmdCurRoot_DP%\JobManagerStartCustom.cmd"

 REM #Doc: In case of maintain JobMgr please use:
 REM  Call :PLMJobManagerIsInMaintance "10.12.2014 11:00"  "10.12.2014 14:00" "PLMJobMnager Update 01"  "If having Questions please call #Number#" "or send mail to support@addPLM.com"

 REM #ToDo: Setup CustomName
 set CustomNameShort=#CustomNameShort#
 REM #ToDo: Setup UNC Path to PLMShare
 set CmdCurRootDefault_UNC_DP=\\CustomServer\PLMShare\JobManagerV3
 REM #ToDo: Setup Custom_WorkInst
 set Custom_WorkInst=#CustomNameShort#_WorkInst_TestSystem

 REM #Doc: Setup Base Dirs
 set BinServer_DP=%CmdCurRoot_DP%\..\01-BinServer
 set BinClient_DP=%CmdCurRoot_DP%\..\02-BinClient
 set BinPublic_DP=%CmdCurRoot_DP%\..\03-BinPublic

 call "%BinPublic_DP%\ColorSet.cmd" Reset

 REM #ToDo: setup PLMShare usage
 if /I "%ComputerName:~0,4%" equ "LOC1"               call :PLMShare1_NetC_DISK
 if /I "%ComputerName%"      equ "ComuterName1_Site1" call :PLMShare1_NetC_DISK
 if /I "%ComputerName:~0,4%" equ "LOC2"               call :PLMShare2_NetC_DISK
 if /I "%ComputerName%"      equ "ComuterName1_Site2" call :PLMShare2_NetC_DISK

 Echo --------------------------------------------------------------------------
 Set MyTitle=PLMJobManager AutoStart \\%ComputerName% [%Date% %Time%]
 title %MyTitle%
 Echo %MyTitle%

 rem New 10-12-2014 10:08/J.Fes
 call "%BinPublic_DP%\ColorSet.cmd" Dialog
 Echo --------------------------------------------------------------------------
 Echo  S   = Run AutoStart (default)
 Echo  Q   = QUIT / Exit  AutoStart will not be performed
 Echo.
 if /I "%~1" equ "/DefaultRunNo" call "%BinPublic_DP%\wait_for_key.cmd" "10" "Q" "S" DoNext
 if /I "%~1" neq "/DefaultRunNo" call "%BinPublic_DP%\wait_for_key.cmd" "10" "S" "Q" DoNext

 rem set /P DoNext= if using other keys (inc.Return) no AutoStart will be done:
 if /I "%DoNext%" equ "S" goto :Next
  call "%BinPublic_DP%\ColorSet.cmd" ERR
  Echo AutoStart not performed
  Pause
 goto :eof



:Next
 call "%BinPublic_DP%\ColorSet.cmd" IsExecuting
 call "%BinPublic_DP%\DesktopRefresh.exe"


 REM #ToDo: Set Base Settings TestSystem or ProdSystem
 set PLMSettingJobContr1=/ServerSettingsXML TestSystem /WorkInstTitel 10-PLM1 /TCPPort 13000
 set PLMSettingInterakt1=/ServerSettingsXML TestSystem /WorkInstTitel 11-PLM1-Interak. /TCPPort 13000
 set PLMSettingInterakt2=/ServerSettingsXML TestSystem /WorkInstTitel 20-PLM2 /TCPPort 14000
 set PLMSettingInterakt2=/ServerSettingsXML TestSystem /WorkInstTitel 21-PLM2-Interak. /TCPPort 14000

 REM #ToDo: Set JobServer start
 if /I "%ComputerName%" equ "#JobServerN#" (
  call :CleanServerSysLog "%CmdCurRootDefault_UNC_DP%\90-DATA\%Custom_WorkInst%\11-ServerSysLog"
  call :JobServer_Start1
 )

 REM #ToDo: Set JobClient start for Site 1
 if /I "%ComputerName:~0,5%" equ "SITE1" call :JobClient_Start1

 REM #ToDo: Set JobClient start for Site 2
 if /I "%ComputerName:~0,5%" equ "SITE2" call :JobClient_Start2
 exit /B
goto :eof

rem ============================================================================
:PLMShare1_NetC_DISK
 REM #ToDo: Setup PLMShare1
 call "%BinPublic_DP%\ConnectNetUnc.cmd" "Use PLMShare1 description" J: \\CustomServer1\PLMshare . . PLMShare_DiskConnectResult
 Echo PLMShare_DiskConnectResult:[%PLMshare_DiskConnectResult%]
goto :eof

:PLMShare2_NetC_DISK
 REM #ToDo: Setup Location Dir
 call "%BinPublic_DP%\ConnectNetUnc.cmd" "Use PLMShare2 description" J: \\CustomServer2\PLMshare . . PLMShare_DiskConnectResult
 Echo PLMShare_DiskConnectResult:[%PLMshare_DiskConnectResult%]
goto :eof

rem ============================================================================
:JobClient_Stop
  rem 07.05.2012 15:24 J.Fes Stopt alle JobClient Processe
  call :Wait "%~1" "Step:[%StepCur%\%StepsN%] Stop JobClient Progs:[JobClient]"
  Call "%BinPublic_DP%\ProgKill.cmd" JobClient "%ProgKill_log_DPNE%" "10"
  Call "%BinPublic_DP%\ProgKill.cmd" PLMJobManagerWatchDog "%ProgKill_log_DPNE%" "1"

  Call "%BinPublic_DP%\ProgKill.cmd" JfProcessMonitor      "%ProgKill_log_DPNE%" "3"
  Call "%BinPublic_DP%\ProgKill.cmd" PLMJobManagerWatchDog "%ProgKill_log_DPNE%" "2"
  call :CleanDirs
goto :eof

rem ============================================================================
:JobServer_Stop
  call :Wait "%~1" "Step:[%StepCur%\%StepsN%] Stop JobServer Progs:[JobClient,JobServer]"
  Call "%BinPublic_DP%\ProgKill.cmd" JobClient "%ProgKill_log_DPNE%" "10"
  Call "%BinPublic_DP%\ProgKill.cmd" JobServer "%ProgKill_log_DPNE%" "20"
  Call "%BinPublic_DP%\ProgKill.cmd" PLMJobManagerWatchDog "%ProgKill_log_DPNE%" "3"

  Call "%BinPublic_DP%\ProgKill.cmd" JfProcessMonitor      "%ProgKill_log_DPNE%" "3"
  Call "%BinPublic_DP%\ProgKill.cmd" PLMJobManagerWatchDog "%ProgKill_log_DPNE%" "2"
  call :CleanDirs
 goto :eof
rem ============================================================================
:CleanDirs
  if exist "%temp%" call :DelDir "%temp%"
  rem if exist "C:\TEMP" call :DelDir "C:\TEMP"
  if defined JobMgr_RootL_DP if exist "%JobMgr_RootL_DP%" call :DelDir "%JobMgr_RootL_DP%"
 goto :eof

rem ============================================================================
:JobClient_Start1
 setlocal

  set StepsN=2
  set StepCur=0

  REM -- Stop JobClient --------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  call :JobClient_Stop 2

  REM -- Start caffeine --------------------------------------------------------
REM   set /A StepCur=%StepCur% + 1
REM   call :Wait 0 "Step:[%StepCur%\%StepsN%] CheckIf [Caffeine is running]"
REM   call "%BinPublic_DP%\ProgStart_If_NotRuning.cmd" "caffeine" "%BinPublic_DP%\caffeine.exe"

  REM -- Start JobClient -------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  set toRun="%BinClient_DP%\JobManagerStart.cmd" /JobClient %PLMSettingJobContr1% /JobClientStartMode Pause
  call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [JobClient]"
  call %toRun%

 endlocal
goto :eof
rem ============================================================================
:JobClient_Start2
 setlocal

  set StepsN=3
  set StepCur=0

  REM -- Stop JobClient --------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  call :JobClient_Stop 2

  REM -- Start caffeine --------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  if exist "%BinPublic_DP%\caffeine.exe" start "Caffeine" "%BinPublic_DP%\caffeine.exe"

  REM -- Start JobClient -------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  set toRun="%BinClient_DP%\JobManagerStart.cmd" /JobClient %PLMSettingJobContr1% /JobClientStartMode Pause
  call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [JobClient]"
  call %toRun%

 endlocal
goto :eof



rem ============================================================================
:JobServer_Start1

  REM #Doc: Start JobServer 1

  Echo.
  Echo AutoStart PLMJobManager JobServer:\\%ComputerName%

  set StepsN=5
  set StepCur=0

  REM -- Stop JobServer -----------
  set /A StepCur=%StepCur% + 1
  call :JobServer_Stop 3

REM   set toRun=call "%BinServer_DP%\JobManagerStart.cmd" /Server %PLMSettingJobContr1% /AutoStart TaskScheduler:On
REM   set /A StepCur=%StepCur% + 1
REM   call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [TaskScheduler]"
REM   call %toRun%

  REM -- Start greenshot ----------
  set /A StepCur=%StepCur% + 1
  call :Wait 0 "Step:[%StepCur%\%StepsN%] CheckIf [GreenShot is running]"
  call "%BinPublic_DP%\ProgStart_If_NotRuning.cmd" "greenshot" "%BinServer_DP%\JobManagerStart.cmd" /Screenshot

REM   REM -- Start caffeine -------
REM   set /A StepCur=%StepCur% + 1
REM   call :Wait 0 "Step:[%StepCur%\%StepsN%] CheckIf [Caffeine is running]"
REM   call "%BinPublic_DP%\ProgStart_If_NotRuning.cmd" "caffeine" "%BinPublic_DP%\caffeine.exe"

  REM -- Start JobServer for Services:PLMSite1
  set toRun=call "%BinServer_DP%\JobManagerStart.cmd" /Server %PLMSettingJobContr1% /AutoStart JobControl:A
  set /A StepCur=%StepCur% + 1
  call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [JobControl]"
  call %toRun%

REM   REM -- Start JobServer for Services:PLMSite2
REM   set toRun=call "%BinServer_DP%\JobManagerStart.cmd" /Server %PLMSettingJobContr2% /AutoStart JobControl:A
REM   set /A StepCur=%StepCur% + 1
REM   call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [JobControl]"
REM   call %toRun%

  REM -- Start JobServer for Interaktiv usage:PLMSite1
  set toRun=call "%BinServer_DP%\JobManagerStart.cmd" /Server %PLMSettingInterakt1%
  set /A StepCur=%StepCur% + 1
  call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [Interaktiv]"
  call %toRun%

REM   REM -- Start JobServer for Interaktiv usage:PLMSite2
REM   set toRun=call "%BinServer_DP%\JobManagerStart.cmd" /Server %PLMSettingInterakt2%
REM   set /A StepCur=%StepCur% + 1
REM   call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [Interaktiv]"
REM   call %toRun%

  REM -- Start JobClient -------------------------------------------------------
  set /A StepCur=%StepCur% + 1
  set toRun="%BinClient_DP%\JobManagerStart.cmd" /JobClient %PLMSettingJobContr1% /JobClientStartMode Pause
  call :Wait 10 "Step:[%StepCur%\%StepsN%] Start [JobClient]"
  call %toRun%


goto :eof

rem ============================================================================
:CleanServerSysLog
 rem neu 02.02.2011 13:14 J.Fes
 rem P1=RootDir WorkInst
 set CleanDir=%~1
 set DelDateOlderThen=90

 call "%BinPublic_DP%\ColorSet.cmd" IsExecuting
 Echo --------------------------------------------------------------------------
 Echo CleanUp.: [%CleanDir%]
 Echo older...: [%DelDateOlderThen%] Days
 Echo --------------------------------------------------------------------------
 rem neu 02.02.2011 13:14 J.Fes
 rem Option: /preview = List Only DoNot Delete Data
 set toRun="%BinPublic_DP%\delage32.exe" "%CleanDir%\*.log" %DelDateOlderThen% /modified /recurse /rd /includeRHS /preview
 call "%BinPublic_DP%\ColorSet.cmd" ERR
 Echo #ToDo: :CleanServerSysLog /preview is activ please setup
 call "%BinPublic_DP%\ColorSet.cmd" IsExecuting
 Echo %toRun%
 Echo #ToDo: :CleanServerSysLog /preview is activ please setup
 pause
goto :eof

rem ================================================================================--
:DelDir
 setlocal
  rem call :DelDir "%temp%"
  set Dir_to_Clean_DP=%~1
  echo Cleaning %Dir_to_Clean_DP%...
  echo.
  REM Delete all files (recursive) in %Dir_to_Clean_DP%
  del /s /q "%Dir_to_Clean_DP%" 1>NUL 2>NUL
  REM Determine the items left and try to delete any subdirs
  for /f "delims=#" %%i in ('dir /a:d /b %Dir_to_Clean_DP%') do rd /s /q %Dir_to_Clean_DP%\"%%i" 1>NUL 2>NUL
 endlocal
goto :eof


rem ============================================================================
rem ##### ------------ Standard Subroutines --------------------- ######
:PathCutLastDir
 rem schneidet das letzte Verzeichniss Uebergeben path ab
 rem P1 = Name der Variablen
 rem P2 = der Übergebene Path
 rem Example : call :PathCutLastDir AppRoot_DP %~dp0
 rem Example : call :PathCutLastDir MyVar %~dp0

 set PathCutLastDir$temp=%2
 set PathCutLastDir$tempLastChar=%PathCutLastDir$temp:~-1%
 set PathCutLastDir$tempNoBacks=%PathCutLastDir$temp:~0,-1%

 if "%PathCutLastDir$tempLastChar%"=="\" (
   REM "\" abschneiden wenn vorhanden
   call :PathCutLastDirDo PathCutLastDir$tempRes %PathCutLastDir$tempNoBacks%
 ) else (
   rem
   call :PathCutLastDirDo PathCutLastDir$tempRes %PathCutLastDir$temp%
 )
 set PathCutLastDir$tempRes=%PathCutLastDir$tempRes:~0,-1%
 set %1=%PathCutLastDir$tempRes%
 set PathCutLastDir$temp=
 set PathCutLastDirDo$temp1=
 set PathCutLastDir$tempNoBacks=
 set PathCutLastDir$tempNoLastChar=
 set PathCutLastDir$tempLastChar=
 set PathCutLastDir$tempRes=
 goto :eof

:PathCutLastDirDo
 rem hiermit schneiden wir den Path ab
 rem echo.
 rem Echo P1=%1 P2=%2
 set PathCutLastDirDo$temp=%~dp2
 REM "\" abschneiden
 set PathCutLastDirDo$temp1=%PathCutLastDir$temp:~0,-1%
 set %1=%PathCutLastDirDo$temp%
 set PathCutLastDirDo$temp=
 set PathCutLastDirDo$temp1=
 goto :eof

rem ================================================================================--
:Wait
 setlocal
 set WaitTime=2
 if "%~1" neq "" set WaitTime=%~1
 if "%~2" equ "" goto :JumbOverPrintInfo
 Echo --------------------------------------------------------------------------
 Echo %2
 Echo Wait:%WaitTime%[sec] (%Date% %time:~0,8%)
:JumbOverPrintInfo
 rem ping localhost -n %WaitTime% >nul
 timeout /T %WaitTime%
 rem call CHOICE.com /C:w /N /T:w,%WaitTime% warte:%WaitTime%[s] oder "w" fuer weiter druecken.
 endlocal
goto :eof
rem ============================================================================
:PLMJobManagerIsInMaintance
 call "%BinPublic_DP%\ColorSet.cmd" ERR
 Echo --------------------------------------------------------------------------
 Echo PLMJobManager AutoStart not exscuted Maintance is activ
 Echo --------------------------------------------------------------------------
 if "%~3" neq "" Echo Reason..: %~3
 if "%~4" neq "" Echo Info....: %~4
 if "%~5" neq "" Echo Info....: %~5
 Echo.
 Echo Started.: %~1
 Echo Ends at~: %~2
 Echo.
 Echo If you have any questions please contact your Admin.
 Echo.
 Echo  !! -- AutoStart will not be executed -- !!
 Echo.
 pause
 exit
 goto :eof
