@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: Set Up Base ---------------------------------------------------------"


 REM -- Rootpfade setzen -------------------------------------------------------
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%

 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 "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_DPNE=%BinL_DP%\JFProcessMonitor.exe
 robocopy %robocopy_opt% /NP /NJS /NJH /NDL /NFL "%BinPublic_DP%" "%BinL_DP%" "JFProcessMonitor.exe"


 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%"

 REM -- Option: Control software starten ---------------------------------------
 rem #New: 30.11.2013/J.Fes
 if /I "%JobMgr_use_PLMJobManagerWatchDog%" equ "ON" (
  REM Echo Check Start: PLMJobManagerWatchDog.exe
  Echo --------------------------------------------------------------------------
  call "ProgStart_If_NotRuning.cmd" "PLMJobManagerWatchDog" "%BinPublic_DP%\PLMJobManagerWatchDog_Start.cmd"
 )

 rem #New: 19.06.2014/J.Fes
 if /I "%JobMgr_start_TaskMgr%" equ "ON" (
  REM Echo Check Start: TaskMgr.exe
  Echo --------------------------------------------------------------------------
  call "ProgStart_If_NotRuning.cmd" "TaskMgr" "taskmgr.exe"
 )

 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

 REM -- cut '\' on end
 if "%UGII_BASE_DIR:~-1%" equ "\" set UGII_BASE_DIR=%UGII_BASE_DIR:~0,-1%
 if "%UGII_ROOT_DIR:~-1%" equ "\" set UGII_ROOT_DIR=%UGII_ROOT_DIR:~0,-1%

 REM -- extend search path
 set Path=%UGII_BASE_DIR%;%UGII_ROOT_DIR%;%Path%

 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

Echo 14.03.2016  #toDo: solve issue call :ExecuteRf_ERR_Write
REM  if not exist "%UGII_LOCAL_USER_DEFAULTS%" call :ExecuteRf_ERR_Write "fnf:UGII_LOCAL_USER_DEFAULTS"     "File not found: UGII_LOCAL_USER_DEFAULTS"       "UGII_LOCAL_USER_DEFAULTS: [%UGII_LOCAL_USER_DEFAULTS%]"
REM  if not exist "%UGII_LOAD_OPTIONS%"        call :ExecuteRf_ERR_Write "fnf:UGII_LOAD_OPTIONS"            "File not found: UGII_LOAD_OPTIONS"              "UGII_LOAD_OPTIONS.......: [%UGII_LOAD_OPTIONS%]"
REM  if not exist "%refile_part_exe_DPNE%"     call :ExecuteRf_ERR_Write "fnf:refile_part.exe"              "File not found: refile_part.exe"                "refile_part_exe.........: [%refile_part_exe_DPNE%]"

 REM -- Exit if Error happend
 if exist "%RfErrLog_DPNE%" call :ExecuteRf_ERR_Show& goto :Rf_end

 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#"
REM  Call Echo "Special Options.........:"
 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"

REM  if /i "%JobMgr_RfPMon_USE%" neq "false" start "PMon Job:[%JobName%][%TcSiteId%]" %toMon%
 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"

 IF /i "%JobMgr_Debug%" neq "ON" goto :Next
   Echo #Debug: Refile Results Info:
   Echo #Debug: RfPrg........: %refile_part_exe_DPNE%
   Echo #Debug: RfPrgExitCode: %RfPrgExitCode%
REM    Call :TypeFile "%RfJobLog_DPNE%"     "Rf Job Log:"
REM    Call :TypeFile "%RfFailedLog_DPNE%"  "Rf Failed Log:"
   pause
:Next

 if defined title_str title %title_str%
 if "%RfPrgExitCode%" equ "0" goto :RfPrgExit_OK

:RfPrgExit_ERR
 set title_str=ERR:%RfPrgExitCode% !! error running refile_part.exe [%Date%/%time:~0,8%] !!
 rem #New 11.11.2015/J.Fes Prevent Issues of 'dead Sessions' in TC.DB
 Echo --------------------------------------------------------------------------
 Echo %title_str%
 call :ExecuteRf_ERR_Write "RfPrg.End NOT with '0'" ""  "refile_part: Ends with [ErrorLevel:%RfPrgExitCode%]"
 call :ExecuteRf_ERR_Show

:JumbtoRfDataSetsExit
 goto :Rf_end

:RfPrgExit_OK
 Call :ColorSet OK
 REM -- delete alle files not to send to JobServer
 if /I "%JobMgr_TcNxDebug%" neq "ON" (
       if /I "%JobMgr_Debug%" neq "ON" (
          Echo CleanUp......: %temp%\..
          del /S /Q "%temp%\*.bak","%temp%\*.tmp","%temp%\*.prt">nul 1>nul 2>nul
       )
 )
 goto :Rf_end

:Rf_end
 if /i "%JobMgr_Debug%" neq "ON" goto :JumbOverDebug
  Echo.
  Echo #Debug:%~0 [Rf:End]
  pause
:JumbOverDebug

goto :eof



REM ####### ------------ Standard Subroutines ---------------------- ###########
REM ============================================================================
:ExecuteRf_ERR_Write
 Call :ColorSet ERR
 Set RfErr_MSG_Key=%~1
 Set RfErr_MSG_Sort=%~2
 Set RfErr_MSG_L1=%~3
 Set RfErr_MSG_L2=%~4
 Set RfErr_MSG_L3=%~5
 Set RfErr_MSG_L4=%~6

 if "%RfErr_MSG_Key%" equ "" goto :Next
 rem add Error Key
 if     defined RfErr_MSG_Keys set RfErr_MSG_Keys=%RfErr_MSG_Keys% %RfErr_MSG_Key%
 if not defined RfErr_MSG_Keys set RfErr_MSG_Keys=%RfErr_MSG_Key%


 set title_str=ERR:%RfErr_MSG_Key% %RfErr_MSG_Sort%
 if "%RfErr_MSG_Sort%" neq "" set title_str=ERR:%RfErr_MSG_Key%

 if not exist "%RfErrLog_DPNE%" Call :ErrLogHeaderWrite

 REM Error in die Datei schreiben!
 call Echo "%title_str%" "%RfErrLog_DPNE%"
:Next
 if "%RfErr_MSG_L1%" neq "" call Echo "%RfErr_MSG_L1%" "%RfErrLog_DPNE%"
 if "%RfErr_MSG_L2%" neq "" call Echo "%RfErr_MSG_L2%" "%RfErrLog_DPNE%"
 if "%RfErr_MSG_L3%" neq "" call Echo "%RfErr_MSG_L3%" "%RfErrLog_DPNE%"
 if "%RfErr_MSG_L4%" neq "" call Echo "%RfErr_MSG_L4%" "%RfErrLog_DPNE%"
REM  Call Echo "." "%RfErrLog_DPNE%"
REM  pause

goto :eof

REM -- -- -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- -- -
:ErrLogHeaderWrite
 call Echo "DateTime: %date% %time%"  "%RfErrLog_DPNE%"
 call Echo "Computer: %ComputerName%" "%RfErrLog_DPNE%"
 call Echo "User....: %UserName%"     "%RfErrLog_DPNE%"
 call Echo "--------------------------------------------------------------------------" "%RfErrLog_DPNE%"
 goto :eof

REM -- -- -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- -- -
:ErrLogFooterWrite
 REM schreibt die erweiterten Infos an einen ErrLogFile
 REM Info: die Variable RfErrLog_DPNE muss gesetzt sein!!
 Echo."%RfErrLog_DPNE%"
 call Echo "--------------------------------------------------------------------------" "%RfErrLog_DPNE%" "NOECHO"
 call Echo "Environ Info of:\\%ComputerName%" "%RfErrLog_DPNE%" "NOECHO"
 call Echo "." "%RfErrLog_DPNE%" "NOECHO"
 call Echo "-- Path Environ ----------------------------------------------------------" "%RfErrLog_DPNE%" "NOECHO"
 set PATH >>"%RfErrLog_DPNE%"

 call Echo "-- UG* Environ -----------------------------------------------------------" "%RfErrLog_DPNE%" "NOECHO"
 set UG >>"%RfErrLog_DPNE%" 2>nul
 call Echo "-- NX* Environ -----------------------------------------------------------" "%RfErrLog_DPNE%" "NOECHO"
 set NX >>"%RfErrLog_DPNE%" 2>nul
 call Echo "-- All Environ -----------------------------------------------------------" "%RfErrLog_DPNE%" "NOECHO"
 set >>"%RfErrLog_DPNE%" 2>nul
 goto :eof

REM -- -- -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- --  -- -- -
:ExecuteRf_ERR_Show
 set title_str=ERR:Refile on:[\\%ComputerName%][%Date%/%time:~0,8%]!!
 call :ErrLogFooterWrite
 title %title_str%
 Echo --------------------------------------------------------------------------
 Type "%RfErrLog_DPNE%"
 REM Exit wenn nicht auf dem Client das Ergebniss angezeigt werden soll
 if /i "%JobMgr_Debug%" neq "ON" goto :ExecuteRf_ERR_Show_end
 Echo.
 Echo after press enter you will see the error reason in notepad editor ....
 Echo Info: explorer start's with dir %temp%
 pause
 start "" Notepad.exe "%RfErrLog_DPNE%"
 start "" Explorer "%temp%"
 Echo #Hint: Type exit and script continues
 cmd
 :ExecuteRf_ERR_Show_end
goto :eof

REM ============================================================================
:ColorSet
Rem Stand 25.06.2011
 REM call :ColorSet Standard
 REM call :ColorSet IsExecuting
 REM call :ColorSet IsIdel
 REM call :ColorSet End
 REM call :ColorSet Ende
 REM call :ColorSet Ok
 REM call :ColorSet Err

 if /I "%~1" equ "Standard"    set JobColorLast=70
 if /I "%~1" equ "IsExecuting" set JobColorLast=E0
 if /I "%~1" equ "ExtractLi"   set JobColorLast=B0
 if /I "%~1" equ "EvalResult"  set JobColorLast=B1
 if /I "%~1" equ "IsIdel"      set JobColorLast=70
 if /I "%~1" equ "End"         set JobColorLast=70
 if /I "%~1" equ "Ende"        set JobColorLast=70
 if /I "%~1" equ "Ok"          set JobColorLast=A0
 if /I "%~1" equ "Err"         set JobColorLast=C0

 REM set color
 if "%JobColorLast%" neq "" color %JobColorLast%
 goto :eof

REM ============================================================================
:TypeFile
 REM Types the description and the contens of a file
 setlocal
  set FileToType_DPNE=%~1
  set FileDescription=%~2
  Echo ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---  ---
  Echo Description    : %FileDescription%
  Echo Contens of File: %FileToType_DPNE%
  if     exist "%FileToType_DPNE%" type "%FileToType_DPNE%"
  if not exist "%FileToType_DPNE%" Echo Info...........: File.notf.:[%FileToType_DPNE%]
 endlocal
 goto :eof

REM ============================================================================
:Parse_NE
 set %2_NE=%~nx1
 goto :eof

REM ============================================================================
:end
