 rem #Description checks fcc status
 rem #EXA: call fccCheck.cmd
 rem #Doc: enable via:
 rem #Doc:  set JobMgr_fccCheck_USE=true
 rem #Doc:  set JobMgr_fccCheck_parentfsc_address=#CustomSiteFMSadr#
 rem #Doc:  Optional: activate tryRestart for change between sides
 rem #Doc:  set JobMgr_fccCheck_parentfsc_address_tryRestart=true
 rem #Doc:
 rem #New: 21.08.2017/J.Fes
 rem #LUp: 10.05.2019/J.Fes add tryRestart

 if /I "%JobMgr_fccCheck_USE%" neq "true" (
  Call Echo "fcc check.......................: is disabled"
  Call Echo "Enable check via................: set JobMgr_fccCheck_USE=true"
  goto :eof
 )
 call :fccCheckBase
 call :fccCheck_parentfsc_address
 Call Echo "fcc check.......................: %tc_fccstatus%"

 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  REM set toRun=%toRun% -verbose -pauseOnEnd
  call Echo "---------------------------------------------------------------------------"
  call Echo "#Debug..........................: fcc check END"
  pause
 :JumbOverDebug
goto :eof

:fccCheckBase

 REM #New:22.08.2017/J.Fes CHK:FMS_HOME
 if not exist "%FMS_HOME%" (
   call ErrorHandel "C" "fcc.ERR:FMS_HOME dnf" "FMS_HOME not defined -or- FMS_HOME dir not found:[%FMS_HOME%]"
   call ErrorHandel "PS" "#ToDo" "Please check installation and fcc configuration"
   goto :eof
 )

 REM :CHK if fcc is started
 set tc_fccstatus=ERR
 set zzfccStatusLog_DPNE=%temp%\zzfcc1Status.log
 call "%fms_home%\bin\fccstat.exe" -status>>"%zzfccStatusLog_DPNE%"
 if "%errorlevel%" equ "0" (
  set tc_fccstatus=OK active
  goto :eof
 )

 rem fcc try start #LUp:28.08.2017/J.Fes
 Call Echo "fccstat.exe.....................: fcc '-start' in process"
 setlocal
  set TC_TMP_DIR=%CmdCurRoot_DP%\..\zzfcctmp
  set log_DP=%temp%
  set temp=%TC_TMP_DIR%
  set tmp=%TC_TMP_DIR%
  if not exist "%TC_TMP_DIR%" md "%TC_TMP_DIR%"
  pushd "%TC_TMP_DIR%"
  call "%fms_home%\bin\fccstat.exe" -start>>"%log_DP%\zzfcc4Start.log"
  popd
 endlocal

 rem fcc check after start
 set tc_fccstatus=ERR Start
 set zzfccStatusLog_DPNE=%temp%\zzfcc3Status.log
 call "%fms_home%\bin\fccstat.exe" -status>>"%zzfccStatusLog_DPNE%"
 if "%errorlevel%" equ "0" set tc_fccstatus=OK
 Call Echo "fccstat.exe status after start..: %tc_fccstatus%"

 if "%tc_fccstatus%" equ "OK" (
  set tc_fccstatus=OK active after start
  goto :eof
 )

:fccStartERR
 rem #Doc: Now we have an ISSUE fcc canot be started
 call ColorSet ERR
 call ErrorHandel "C" "fcc.ERR" "fcc cannot be started"
 call ErrorHandel "C" "fcc Status/Start Returns" "[errorlevel:%tc_fccstatus%]"
 call ErrorHandel "C" "please look at 1 Status"  "[%temp%\zzfcc1Status.log]"
 call ErrorHandel "C" "please look at 2 Start"   "[%temp%\zzfcc2Start.log]"
 call ErrorHandel "C" "please look at 3 Status"  "[%temp%\zzfcc3Status.log]"
 call ErrorHandel "C" "#ToDo" "Please check installation and fcc configuration"
 call ErrorHandel "PS" "fcc.ERR" "fcc cannot be started"
goto :eof

::-- --
:fccCheck_parentfsc_address
 rem CHK if fcc uses correct parentfsc_address
 if not defined JobMgr_fccCheck_parentfsc_address (
  Call Echo "fccCHK:Parentfsc check..........: !!! - is disabled - !!!"
  Call Echo "fccCHK:Parentfsc enable CHK via.: set JobMgr_fccCheck_parentfsc_address=https://Customserver.dotx.doty:10010/..."
  goto :eof
 )
 set JobMgr_fccCHKParentfsc_Res=ERR
 rem #Doc: check if custom JobMgr_fccCheck_parentfsc_address is contained in %temp%\zzfcc3Status.log
 find /i "%JobMgr_fccCheck_parentfsc_address%" "%zzfccStatusLog_DPNE%">>"%temp%\zzfcc4Chk_parentfsc.log"
 if "%errorlevel%" equ "0" set JobMgr_fccCHKParentfsc_Res=OK& goto :Next


 rem #New: 10.05.2019/J.Fes check if restart is activ
 if /I "%JobMgr_fccCheck_parentfsc_address_tryRestart%" neq "true" goto :next
 Call Echo "fccCHK:Parentfsc check restart..: is activ"
 Call Echo "fccCHK:Parentfsc check restart..: clear + purge"
 call "%fms_home%\bin\fccstat.exe" -clear>>"%temp%\zzfcc5tryRestart.log"
 call "%fms_home%\bin\fccstat.exe" -purge>>"%temp%\zzfcc5tryRestart.log"
 timeout /T 3
 Call Echo "fccCHK:Parentfsc check restart..: kill"
 call "%fms_home%\bin\fccstat.exe" -kill>>"%temp%\zzfcc5tryRestart.log"
 timeout /T 3
 Call Echo "fccCHK:Parentfsc check restart..: start"
 call "%fms_home%\bin\fccstat.exe" -start>>"%temp%\zzfcc5tryRestart.log"
 timeout /T 3
 Call Echo "fccCHK:Parentfsc check restart..: get status again"
 set zzfccStatusLog_DPNE=%temp%\zzfcc6StatusAfterRestart.log
 call "%fms_home%\bin\fccstat.exe" -status>>"%zzfccStatusLog_DPNE%"
 rem #Doc: check if custom JobMgr_fccCheck_parentfsc_address is contained in %temp%\zzfcc3Status.log
 find /i "%JobMgr_fccCheck_parentfsc_address%" "%zzfccStatusLog_DPNE%">>"%temp%\zzfcc7Chk_AfterRestart.log"
 if "%errorlevel%" equ "0" set JobMgr_fccCHKParentfsc_Res=OK& goto :Next


:Next
 Call Echo "fccCHK:fcc should use ParentFsc.: %JobMgr_fccCheck_parentfsc_address%"
 Call Echo "fccCHK:ParentfscAdr CHK result..: %JobMgr_fccCHKParentfsc_Res%"
 if "%JobMgr_fccCHKParentfsc_Res%" equ "OK" goto :eof

:fccChkERR
  call ErrorHandel "C" "fcc.ERR:ParentFscAdr"  "fcc is using wrong parend fsc adress"
  call ErrorHandel "C" "fcc.ERR:ParentFscAdr"  "fcc should use ParentFsc Adress %JobMgr_fccCheck_parentfsc_address%"
  call ErrorHandel "C" "ParentFsc chk returns" "%JobMgr_fccParentfscCHK%"
  call ErrorHandel "C" "Please look at file"   "%temp%\zzfcc4Chk_parentfsc.log"
  call ErrorHandel "PS" "fcc.ERR:ParentFscAdr'%JobMgr_fccCheck_parentfsc_address%' is wrong please check fcc at:\\%Computername%" "#toDo: Please check parentfsc adress settings in fcc.xml and restart fcc"
goto :eof
