@Echo off
 REM ===========================================================================
 REM Basisscript JobManager V2 fuer den Refile mit NX in der TC Umgebung
 REM (c) J.Feuerstein 24.11.2006
 REM Aenderungs Info:
 REM Date/Time (Alt+D)  Infotext:
 REM 14.03.2016/J.Fes   First release
 REM ===========================================================================

 Echo **************************************************************************
 Echo * Excuting Refile Script for  PLMJobManager Environ                      *
 Echo * Version: 14.03.2016 addPLM GmbH (c)                                    *
 Echo **************************************************************************

setlocal
 REM -- #Step: Farbe setzen IsIdel
 call ColorSet "IsIdel"

 call Echo "--- #Step: Rf Nativ Set Up Base --------------------------------------------"

 

 REM -- temp dir Organiesieren -------------------------------------------------
 set JobTemp=%CmdCurRoot_DP%\zzJobTemp
 if not exist "%JobTemp%" md "%JobTemp%"

 rem #New: 16.03.2016 set default RfMaxDuration
 if not defined RfMaxDuration set RfMaxDuration=-1

 REM -- define cmd files to set variables --------------------------------------
 set RfCustomSetEnv_cmd=%CmdCurRoot_DP%\RfCustomSetEnv.cmd

 Rem -- define ResultClassification_DPNE JobMgr --------------------------------
 set ResultClassification_DPNE=%CmdCurRoot_DP%\Resources\RfNxSylog_ResClRules.xml

 rem -- Execute first time RfCustomSetEnv.cmd
 if exist "%RfCustomSetEnv_cmd%" call "%RfCustomSetEnv_cmd%"

 call Echo "--- #Step: Refile Nativ -----------------------------------------"
 rem #Doc: JobParameter is to set on this line
 call :ExecuteRefile "%JobTemp%" "%RfLogFile1_SO_DPNE%" "%RfFailedLog1_SO_DPNE%" "%RfCmdJobLog_SO_DPNE%" "%RfInputList1_SO_DPNE%" "%RfCustomSetEnv_cmd%" "NO"
 endlocal& set RfPrgExitCode=%RfPrgExitCode%
goto :end


rem ============================================================================
:ExecuteRefile
 rem #Doc: Main Script to run Refile
 rem #Doc get File Params
 set temp=%~1
 rem #New 15.04.2014 13:48
 set tmp=%temp%
 set HOME=%temp%

 set RfJobLog_DPNE=%~2
 set RfFailedLog_DPNE=%~3
 set RfCmdJobLog_DPNE=%~4
 set RfPaket_DPNE=%~5
 set RfCustomSetEnv_cmd=%~6

 set RfErrLog_DPNE=%temp%\_RfErrLog.txt
 set UGII_TMP_DIR=%temp%
 SET UGII_KEEP_SYSTEM_LOG=TRUE
 set UGII_UGSOLIDS_TMP=%UGII_TMP_DIR%


 REM -- Symetric Multi-Processing (SMP) disable
 REM diabled 07.02.2011
 set UGII_SMP_ENABLE=

 REM -- CGM erstellung deaktiveren eingefuegt 09.02.10 J.fes
 set UGII_IMAGE_CREATE_CGM=
 set UGII_IMAGE_CREATE_CGM_DRAW=
 set UGII_IMAGE_CREATE_CGM_OPTIONS=
 set UGII_IMAGE_CREATE_NAME=
 set UGII_IMAGE_CREATE_ORIGINAL_BACKGROUND=


 REM -- define Load_options -----------------------------------------------
 set UGII_LOAD_OPTIONS=%CmdCurRoot_DP%\startup\load_options_Native_Refile.def

 REM -- define nx_refile.dpv -----------------------------------------------
 SET UGII_LOCAL_USER_DEFAULTS=%CmdCurRoot_DP%\startup\defaults_Native_refile.dpv
 set UGII_USER_DIR=%CmdCurRoot_DP%
 set UGII_GROUP_DIR=

 rem 21.01.2014
 rem  - mit max 2 Std Laufzeit
 rem  - mit max 6gb Memory usage für 5 min
 rem  - mit max 5 min ohne das neue files im JobTemp entstehen oder editiert werden
 set toMon=%JFProcessMonitorL_exe%
 set toMon=%toMon% --waitForProcess 15000
 set toMon=%toMon% --processName refile_part
 set toMon=%toMon% --parentProcessId 0
 REM  set toMon=%toMon% --runtimeLimitSeconds 7200
 set toMon=%toMon% --killChildProcesses
 set toMon=%toMon% --fileLastWriteTime "%temp%\*.*;600"
 rem #New 23/11/2014 19:21 / J.Fes
 set toMon=%toMon% --fileContainsString "%temp%\%username%*.syslog;This is most likely the result of a crash of the Teamcenter server, or the Teamcenter server encountering an unrecoverable error"
 rem set toMon=%toMon% --monitorExistingFiles
 REM  set toMon=%toMon% --memoryUsageHigh 6000;300
 set toMon=%toMon% --saveResultsTo "%temp%\zzJFProcessMonitor_log.xml"
 set toMon=%toMon% --tailTextFile "%temp%\*refile_part*.syslog"
 set toMon=%toMon% --titlePrefix "Job:[%JobName%][%TcSiteId%]"

 REM -- set Rf Parameter ----------------
 set RfPrgExitCode=

 REM -- set Rf.Contol defaults ----------
 set JobMgr_RfPMon_USE=true

 REM -- set Custom.Environ
 if exist "%RfCustomSetEnv_cmd%" call "%RfCustomSetEnv_cmd%"

 REM -- Refile Programm --------------------------------------------------------
 set refile_part_exe_DPNE=%UGII_ROOT_DIR%\refile_part.exe
 if exist "%UGII_BASE_DIR%\nxbin\refile_part.exe" set refile_part_exe_DPNE=%UGII_BASE_DIR%\nxbin\refile_part.exe

 call :Parse_NE %UGII_LOAD_OPTIONS%        UGII_LOAD_OPTIONS
 call :Parse_NE %UGII_LOCAL_USER_DEFAULTS% UGII_LOCAL_USER_DEFAULTS

 set titleRf_str=Job:[%JobName%][%TcSiteId%][%Date% %time:~0,5%][\\%ComputerName%]
 title %titleRf_str%

 Echo.                                                                               .
 call ColorSet "IsExecuting"
 Echo %titleRf_str%
 call Echo "----------------------------------------------------------------------------"
 Call Echo "Refile Job Settings:"
 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call Echo "JobName.........................: %JobName%"
 Call Echo "Computer........................: %ComputerName%"
 Call Echo "RfDir...........................: %temp%"
 Call Echo "Site............................: #TcSiteId#"
 Call Echo "RfEvalExtOpt....................: %JobMgr_RfExtEvalROpt%"
 Call Echo "RfProc.Monitor..................: %JobMgr_RfPMon_USE%"
 Call Echo "UGII_LOAD_OPTIONS...............: ..\%UGII_LOAD_OPTIONS_NE%"
 Call Echo "UGII_SITE_DIR...................: %UGII_SITE_DIR%"
 Call Echo "UGII_LOCAL_USER_DEFAULTS........: ..\%UGII_LOCAL_USER_DEFAULTS_NE%"

 cd /d "%temp%"
 set >>"%temp%\zzEnvironBevorRefile.txt"

 Call Echo "----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----"
 Call Echo "Refile Nativ Protocol:"

 set RfPrgExitCode=0
 set toRun=%JobManagerCmdTools_L%
 set toRun=%toRun% -Action "RefileNative_2016_02"
 set toRun=%toRun% -SubAction "PartByPart"
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 set toRun=%toRun% -JobTemp_DP "%temp%"
 set toRun=%toRun% -RfProg_DPNE "%refile_part_exe_DPNE%"

 rem -- #Doc: add additional parameters
 if defined RfTarget_DP                 set toRun=%toRun% -RfTarget_DP %RfTarget_DP%
 if defined RfTargetReplSource_DP       set toRun=%toRun% -RfTargetReplSource_DP %RfTargetReplSource_DP%
 if defined RfTargetReplSourceWith_DP   set toRun=%toRun% -RfTargetReplSourceWith_DP %RfTargetReplSourceWith_DP%
 if defined RfParam                     set toRun=%toRun% -RfParam "''%RfParam%''"
 if defined RfEnv_JobMgr_nRfNXcash_DP   set toRun=%toRun% -RfEnv_JobMgr_nRfNXcash_DP %RfEnv_JobMgr_nRfNXcash_DP%

 set toRun=%toRun% -ShellMaxDurationSEC "%RfMaxDuration%"
 set toRun=%toRun% -JobRoot_DP "%CmdCurRoot_DP%"
 set toRun=%toRun% -JobData1ToProcessDoPrint
 set toRun=%toRun% -JobData2ResultsDoPrint

 if exist "%ResultClassification_DPNE%" set toRun=%toRun% -ResultClassification_DPNE "%ResultClassification_DPNE%"
 if defined ResultClassificationCustom_DPNE set toRun=%toRun% -ResultClassificationCustom_DPNE "%ResultClassificationCustom_DPNE%"
 if defined JobMgr_RfExtEvalROpt set toRun=%toRun% %JobMgr_RfExtEvalROpt%

if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
 set toRun=%toRun% -verbose -pauseOnEnd
 set toRunForPrint=%toRun:"=%
 Echo --------------------------------------------------------------------------
 call Echo "#Debug..: %~0"
 call Echo "toRun...: %toRunForPrint%"
 Echo --------------------------------------------------------------------------
 pause
:JumbOverDebug

 rem --#Step: Now run JobMgrCmdTool for Nativ Refile
 call %toRun%
 set RfPrgExitCode=%errorlevel%


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

 call Echo "--- #Res.: RfPrgExitCode.: [%RfPrgExitCode%] ---------------------------------------------"

 REM  Echo ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----  ----
 Call Echo "Rf:End" "" "NOECHO"
 call "%BinPublic_DP%\Banner.exe" "Rf:End"

 Call ColorSet "END"

goto :eof

REM ####### ------------ Standard Subroutines ---------------------- ###########
:Parse_NE
 set %2_NE=%~nx1
 goto :eof

:end
