@Echo off

 rem ---------------------------------------------------------------------------
 rem JobManagerV2 Script to Purge TC Dataset's
 rem (c) J.Feuerstein 12.08.2015 15:28
 rem
 rem Aenderungs Info:
 rem User    Date/Time (Alt+D)    Description:
 rem J.Fes   12.08.2015 15:28     Ersterstellung
 rem J.Fes   12.08.2015 15:28     Testing
 rem J.Fes   25.01.2017 20:50     Update TC-Prompt and ERROR handler
 rem ---------------------------------------------------------------------------

 set JobMgr_TcUser=%1
 set JobMgr_TcPw=%2

 rem wird im JobScript angegben so wird der File:[#JobName#_ObjectsList.txt] nicht dem JobFile hinzugefuegt.
 set JobAddFile_ObjectsList_txt=false
 rem wird im JobScript angegben so wird der File:[#JobName#_ObjectsListAllDetails.txt] nicht dem JobFile hinzugefuegt.
 set JobAddFile_ObjectsListAllDetails_txt=false
 rem wird im JobScript angegben so wird der File:[JobData1ToProcess_XML] nicht dem JobFile hinzugefuegt.
 set JobAddFile_JobData1ToProcess_xml=true

 rem -- JobScriptParametersExtended:
 rem    Hinweis:"JobScriptParametersExtended" ist nach dem ausfueller der # variablen dann leer
 rem             wenn das Datenfeld von Script/Param. J.Cmd.ParmExt leer
 #JobScriptParametersExtended#

 rem -- am Anfang Farbe auf IsIdel setzen
 call :ColorSet IsIdel

 rem Standard Path Variablen
 set JobManagerRoot_DP=#00-ApplRoot_DP#
 set BinServer_DP=#01-BinServer_DP#
 set BinClient_DP=#02-BinClient_DP#
 set BinPublic_DP=#03-BinPublic_DP#
 set JobScripts_DP=#10-JobScripts_DP#
 set MyApplName_TP=#MyApplName_TP#
 set BinPublic_Custom_DP=#03-BinPublic_Custom_DP#
 set BinPublic_ClientServer_DP=#03_BinPublic_ClientServer_DP#

 rem -- JobParameter
 set JobName=#JobName#
 set JobParameter=#JobParameter#

 rem Root Verzeichniss der Scriptvorlage
 set JobScriptTemplateMinus1_DP=#JobScriptTemplateMinus1_DP#
 set JobScriptTemplate_DP=#JobScriptTemplate_DP#

 rem -- TC Daten
 set TcSiteId=#TcSiteId#
 set CmdCur_Title=#JobName#

 rem -- define incudes #LUp: 25.01.2017/J.Fes
 set JobMgr_10-JobScriptsInc_01=_JobMgrDefaults
 set JobMgr_10-JobScriptsInc_02=_CustomDefaults\TcPrompt

 set JobTimeStart=%Date% %time:~0,8%
 set title_str=Job:[%JobName%][%TcSiteId%][Start:%JobTimeStart%][\\%ComputerName%][%JobMgr_TcUser%]
 title %title_str%


 rem ---------------------------------------------------------------------------
 if /i "%JobMgr_Test%" equ "ON" (
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   Banner Test:ON
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
 )
 if "%JobMgr_Debug%"=="ON" (
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   Banner Debug:ON
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
 )


 rem ---------------------------------------------------------------------------
 rem Rootpfade setzen
 set CmdCur_DPNE=%~0
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%
 set path=%CmdCurRoot_DP%;%Path%

 rem temp dir Organiesieren
 set temp_DP_org=%temp%
 set temp=%CmdCurRoot_DP%\JobTemp
 if not exist "%temp%" md "%temp%"
 cd /d "%temp%"

 rem -- #Step: JobMgr CmdTools local kopieren ----------------------------------
 rem -- #Doc: Steuert aus welchem Bereich die Tools genommen werden J.Fes 14.02.2013
 set JobManagerCmdTools_DP=%JobManagerRoot_DP%\03-BinPublic.ClientServer


 set BinL_DP=%CmdCurRoot_DP%\..\BinL
 if not exist "%BinL_DP%" md "%BinL_DP%"
 Call Echo "TcUser/Group....................: [%JobMgr_TcUser%/%JobMgr_TcGroup%]"
 Call Echo "Upd. JobMgrCmd BinL.............: [%BinL_DP%]"
 Call Echo "from............................: [%JobManagerCmdTools_DP%]"

 set JobManagerCmdTools_L=%BinL_DP%\JobManagerCmdTools.exe
 set robocopy_opt=/R:5 /W:5
 robocopy %robocopy_opt% /NP /NJS /NJH /NDL /NFL "%JobManagerCmdTools_DP%" "%BinL_DP%" "JobManagerCmdTools.exe" >nul 2>nul

 rem new 21.01.2014
 set JFProcessMonitorL_exe=%BinL_DP%\JFProcessMonitor.exe
 robocopy %robocopy_opt% /NP /NJS /NJH /NDL /NFL "%BinPublic_DP%" "%BinL_DP%" "JFProcessMonitor.exe"  >nul 2>nul
 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  Echo #Debug:%~0 [Start:%toRun%]
  pause
 :JumbOverDebug

 rem ---------------------------------------------------------------------------
 rem PaketFile festlegen

 if /I "%JobMgr_Debug%"=="ON" (
  Echo #Debug: Start Explorer at JobDir:[%temp%]
  start "" Explorer "%temp%"
 )

 rem -- Step: TC Prompt erhalten  - - - -
 set TCPrompt_Custom_DPNE=%CmdCurRoot_DP%\TCPrompt_Custom.cmd
 set TCPrompt_Custom_Opt="%TcSiteId%" "Purge Datasets" Quick

 if exist "%TCPrompt_Custom_DPNE%" Call "%TCPrompt_Custom_DPNE%" "%TCPrompt_Custom_Opt%"
 REM -- PreCheck's continue process
 if not defined JobMgr_TCPromptGet_ResCode call ErrorHandel "S" "TcPromt:UNK"  "[Missing -or- NOT executed -or- NOT working]"
 if "%JobMgr_TCPromptGet_ResCode%" neq "0" call ErrorHandel "S" "TcPromt:ERR"  "TcPromptResCode:%JobMgr_TCPromptGet_ResCode% // TcPromptResMsg:%JobMgr_TCPromptGet_ResMsg%"

 if /I "%JobMgr_Debug%"=="ON" (
  Echo #Debug: after get TCPrompt Quick
  set TC
  pause
 )

 rem #Doc deactivate SQL Debug
 set TC_SQL_DEBUG=
 if not defined purge_datasets_keep set purge_datasets_keep=3

 call :ColorSet IsExecuting
 call Echo "%title_str%"
 call Echo "----------------------------------------------------------------------------"
 Call Echo "TC-Purge Job Settings:"
 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call Echo "JobName.........................: %JobName%"
 Call Echo "Computer........................: %ComputerName%"
 Call Echo "TempDir.........................: %temp:~-50%"
 Call Echo "##- - - - TC settings: - - - -##"
 Call Echo "TcSide..........................: #TcSite.SiteInfoWith_SiteId_and_pName#"
 Call Echo "TcUser..........................: %JobMgr_TcUser%"
 Call Echo "TC_ROOT.........................: %TC_ROOT%"
 Call Echo "TC_DATA.........................: %TC_DATA%"
 Call Echo "##- - - - Purge settings: - - ##"
 Call Echo "Purge Methode...................: %purge_datasets_via_methode%"
 Call Echo "Purge Datasets with keep n vers.: %purge_datasets_keep% (n)"


 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  call Echo "#Debug...: %~0"
  pause
 :JumbOverDebug

 if /i "%purge_datasets_via_methode%" equ "M1_use_Tool_Purge_Datasets" call :M1_Use_Tool_Purge_Datasets
 if /i "%purge_datasets_via_methode%" equ "M2_use_Tcpb_Purge_Dataset"  call :M2_use_Tcpb_Purge_Dataset
goto :end


rem -----------------  RUN Methode 2 purge via Tool TC-PowerBox Purge_Datasets -----------------
:M2_use_Tcpb_Purge_Dataset

 rem #Doc: set base Variables
 set tcpb_purge_dataset_input_DPNE=%temp%\01-tcpb_purge_dataset_input.txt
 set tcpb_purge_datasets_log_DPNE=%temp%\02-tcpb_purge_dataset_log.txt
 set tcpb_purge_datasets_cmd_log_DPNE=%temp%\03-tcpb_purge_dataset_cmd_log.txt

 rem #Step: --- Extract tcpb_purge_dataset_input_DPNE content ItemId's ----------------------
 set toRun=%JobManagerCmdTools_L%
 set toRun=%toRun% -Action JobDataExtract_2013_10
 set toRun=%toRun% -SubAction InputListExtract
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 rem #Doc: next 2 Lines: Work around to input #ObjectType #
 set toRun=%toRun% -format "#ItemId#@#Rev#@#ObjectName#@#
 set toRun=%toRun%ObjectType#"
 set toRun=%toRun% -inputList_DPNE "%tcpb_purge_dataset_input_DPNE%"
 set toRun=%toRun% -JobData1ToProcessDoPrint
 call %toRun%


 rem #Step: --- Execute purge datasets via purge_datasets.exe ------------------
 set tcpbRoot_S_DP=
 set tcpbRoot_S_DP=%tcpb_local%\bin\win
 set tcpbRoot_S_DP=%tc_user_lib%\bin\win
 set tcpb_purge_dataset_NE=tcpb_purge_dataset.exe
 set tcpb_purge_dataset_S_DPNE=%tcpbRoot_S_DP%\%tcpb_purge_dataset_NE%
 set path=%tcpbRoot_S_DP%;%path%

 set toRun="%tcpb_purge_dataset_S_DPNE%"
 set toRun=%toRun% -input="%tcpb_purge_dataset_input_DPNE%"
 set toRun=%toRun% -log="%tcpb_purge_datasets_log_DPNE%"
 set toRun=%toRun% -preserve_modification
 set toRun=%toRun% -k=%purge_datasets_keep%
 set toRun=%toRun% -g=dba
 set toRun=%toRun% -u=%JobMgr_TcUser%

if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 set toRunForPrint=%toRun:"=%
 call Echo "---------------------------------------------------------------------------"
 call Echo "#Debug..........................: %~0"
 call Echo "#before run.....................: %tcpb_purge_dataset_S_DPNE%"
 call Echo "#toRun..........................: %toRunForPrint% -p=**********"
 call Echo "---------------------------------------------------------------------------"
 pause
:JumbOverDebug

 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 call Echo "Purge via Dataset's.............: with setting"
 call Echo "tcpb_purge_dataset_input_DPNE...: [%tcpb_purge_dataset_input_DPNE%]"
 call Echo "tcpb_purge_datasets_cmd_log_DPNE: [%tcpb_purge_datasets_cmd_log_DPNE%]"
 call Echo "tcpb_purge_datasets_log_DPNE....: [%tcpb_purge_datasets_log_DPNE%]"
 call Echo "-k..............................: [%purge_datasets_keep%]"
 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 call Echo "Purge via Dataset's.............: Protocol"
 call %toRun% -p=%JobMgr_TcPw% >>"%tcpb_purge_datasets_cmd_log_DPNE%"

if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 set toRunForPrint=%toRun:"='%
 call Echo "---------------------------------------------------------------------------"
 call Echo "#Debug..........................: %~0"
 call Echo "#after run......................: %tcpb_purge_dataset_S_DPNE%"
 call Echo "#toRun..........................: %toRunForPrint% -p=**********"
 pause
:JumbOverDebug

 rem #Step: --- Run set result's -----------------------------------------------
 set toRun=%JobManagerCmdTools_L%
 set toRun=%toRun% -Action "JobDataEdit_2012_09"
 set toRun=%toRun% -SubAction "JobData2ResultsSetProcessValue"
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 set toRun=%toRun% -ObjProcessResultCode "0"
 set toRun=%toRun% -ObjProcessState  "D.OK"
 set toRun=%toRun% -ObjProcessResultMsg "[PurgeDS:M2 via DS]"
 set toRun=%toRun% -JobData2ResultsDoPrint

 call %toRun%
goto :eof

rem -----------------  Methode 1 purge via Tool Purge_Datasets -----------------
:M1_use_tool_Purge_Datasets

 set itemidsfile_DPNE=%CmdCurRoot_DP%\01-itemidsfile.txt
 set purge_datasets_cmd_log_DPNE=%CmdCurRoot_DP%\02-purge_datasets_cmd_log.txt

 rem #Step: --- Extract itemidsfile_DPNE content ItemId's ----------------------
 set toRun=%JobManagerCmdTools_L%
 set toRun=%toRun% -Action JobDataExtract_2013_10
 set toRun=%toRun% -SubAction InputListExtract
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 set toRun=%toRun% -format #ItemId#
 set toRun=%toRun% -inputList_DPNE "%itemidsfile_DPNE%"
 set toRun=%toRun% -JobData1ToProcessDoPrint

 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  set toRun=%toRun% -JobParaDoPrint
  set toRun=%toRun% -verbose
  set toRun=%toRun% -pauseOnEnd
  Echo #Debug:%~0
  Echo Start:%toRun%
  pause
 :JumbOverDebug
 call %toRun%
 
 rem #Step: --- Execute purge datasets via purge_datasets.exe ------------------
 if not defined purge_datasets_keep set purge_datasets_keep=3
 set toRun=purge_datasets.exe
 set toRun=%toRun% -itemidsfile="%itemidsfile_DPNE%"
 set toRun=%toRun% -k=%purge_datasets_keep%
 set toRun=%toRun% -g=dba
 set toRun=%toRun% -u=%JobMgr_TcUser%

if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 set toRunForPrint=%toRun:"=%
 call Echo "---------------------------------------------------------------------------"
 call Echo "#Debug..........................: %~0"
 call Echo "#before run.....................: purge_datasets.exe"
 call Echo "#toRun..........................: %toRunForPrint% -p=**********"
 call Echo "---------------------------------------------------------------------------"
 pause
:JumbOverDebug

 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 call Echo "Purge via ITEMS ................: with settings"
 call Echo "purge_datasets_cmd_log_DPNE.....: [%purge_datasets_cmd_log_DPNE%]"
 call Echo "-itemidsfile....................: [%itemidsfile_DPNE%]"
 call Echo "-k..............................: [%purge_datasets_keep%]"
 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 call Echo "Purge via ITEMS.................: Protocol"
 call %toRun% -p=%JobMgr_TcPw% >>"%purge_datasets_cmd_log_DPNE%"

if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 set toRunForPrint=%toRun:"='%
 call Echo "---------------------------------------------------------------------------"
 call Echo "#Debug..........................: %~0"
 call Echo "#after run......................: purge_datasets.exe"
 call Echo "#toRun..........................: %toRunForPrint% -p=**********"
 pause
:JumbOverDebug


 rem #Step: --- Run set result's -----------------------------------------------
 set toRun=%JobManagerCmdTools_L%
 set toRun=%toRun% -Action "JobDataEdit_2012_09"
 set toRun=%toRun% -SubAction "JobData2ResultsSetProcessValue"
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 set toRun=%toRun% -ObjProcessResultCode "0"
 set toRun=%toRun% -ObjProcessState  "D.OK"
 set toRun=%toRun% -ObjProcessResultMsg "[PurgeDS:M1 via Item]"
 set toRun=%toRun% -JobData2ResultsDoPrint

 call %toRun%
goto :eof

rem #### ----------------- Standard Subroutines ------------------------- ######
:ColorSet
 rem Gelb Schwarz
 if /I "%1"=="IsExecuting" set JobColorLast=E0
 rem Blaugrün Schwarz
 if /I "%1"=="IsIdel"      set JobColorLast=30
 rem Hellgrau Schwarz
 if /I "%1"=="End"         set JobColorLast=70
 if /I "%1"=="Ende"        set JobColorLast=70
 rem Gruen Schwarz
 if /I "%1"=="Ok"          set JobColorLast=A0
 rem Rot Schwarz
 if /I "%1"=="Err"         set JobColorLast=C0

 rem jetzt die Farbe setzen
 if not "%JobColorLast%" equ "" color %JobColorLast%
 goto :eof

rem ---------- END ACTION ------------------------------------------------------
:end

 rem -- Farbe auf JobEnds setzen
 call :ColorSet End

 if "%JobMgr_Debug%"=="ON" (
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   Echo #Debug Info: Ende
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   pause
 )
 if /i "%JobMgr_Test%" equ "ON" (
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   Banner #Test:End
   Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  --
   pause
 )

 rem Cleanup - Logfiles
 cd /D "%temp%"
 if exist "%CmdCurRoot_DP%\JobTemp\srv_jobmanager2TierTransientVolume" rd /S /Q "%CmdCurRoot_DP%\JobTemp\srv_jobmanager2TierTransientVolume"
 call Echo "---------------------------------------------------------------------------"
 call Echo "Job End.........................: [%date% %time:~0,8%]"
 call Echo "Info: all files in..............: [%temp%]"
 call Echo "will be zipt by JobClient and send to JobServer"
