@Echo off
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%
 set MyCmd_DPNE=%~0
 if "%path_org%"=="" set path_org=%Path%
 call :PathCutLastDir JobMgrRoot_DP "%~dp0"
 set path=%JobMgrRoot_DP%\03-BinPublic;%CmdCurRoot_DP%;%Path%
 cls
 call ColorSet.cmd Reset
 Echo.
 Echo ##########################################################################
 Echo # PLMJobManager Service Tool's                                           #
 Echo # Date         Who     Details                                           #
 Echo # 19.06.2014   J.Fes   #New: Creation of this Tool                       #
 Echo # 19.06.2014   J.Fes   #New: Set NXV                                     #
 Echo # 10.09.2014   J.Fes   #New: JSE,ASE,ASA,ASR                             #
 Echo # 02.02.2015   J.Fes   #New: DCL                                         #
 Echo # 09.02.2017   J.Fes   #New: UGII_DISPLAY_DEBUG                          #
 Echo # 13.12.2017   J.Fes   #New: UGII_DISPLAY_DEBUG                          #
 Echo ##########################################################################
 Echo.

:Menu
 call ColorSet.cmd Reset
 Call :DirOrg
 set MyApplTitle=JobMgr Service Menue %MyApplProd_NShort%.%JobMgr_VerNumber% [AppRoot:%JobMgrRoot_DP%]
 Title %MyApplTitle%
 Echo.
 Echo ---------------------------------------------------------------------------------------
 Echo * %MyApplTitle% *
 Echo ---------------------------------------------------------------------------------------
 call ColorSet.cmd Dialog
 Echo  SET = Setx TC and NX Environment Variables
 Echo  DCL = Directory Clean TempDirs + JobMgr
 Echo.
 Echo.       -- Org:JobManagerStartCustom.cmd JobMgr_RootL_DP --
 Echo  JSE = Edit %JobManagerStartCustom_DPNE%
 Echo.
 Echo.       -- Org:Autostart ------
 Echo  ASE = Edit %JobManagerAutostartCustom_DPNE%
 Echo  ASA = Activates Autostart
 Echo  ASR = Removes   Autostart
 Echo.
 Echo.       -- Org:Deploy --
 Echo  JDE = Edit %JobManagerDeployCustom_DPNE%
 Echo  JDR = RUN
 Echo.
 Echo  Q   = Quit (finish this dialog)
 Echo.
 set doNext=

 set /P doNext="please select option: "

 if /I "%doNext%"=="Q"   Exit

 rem ausfuehren naechten Vorgang via :doNextExec
 call :doNextExec
 goto :Menu

rem ============================================================================
:doNextExec

 Call ColorSet.cmd IsExecuting
 if /I "%doNext%"=="SET"             call :SetNXTcEnvVariables
 if /I "%doNext%"=="Nx_Debug_ON"     call :NxDebug_UGII_CHECKING_LEVEL ":ON"
 if /I "%doNext%"=="Nx_Debug_OFF"    call :NxDebug_UGII_CHECKING_LEVEL ":OFF"
 if /I "%doNext%"=="Nx_DebugDisplay_ON"     call :NxDebug_UGII_DISPLAY_DEBUG ":ON"
 if /I "%doNext%"=="Nx_DebugDisplay_OFF"    call :NxDebug_UGII_DISPLAY_DEBUG ":OFF"

 if /I "%doNext%"=="NX_Conf_Rule_ON" call :NX_Conf_Rule ":ON"
 if /I "%doNext%"=="NX_Conf_Rule_OF" call :NX_Conf_Rule ":OFF"

 if /I "%doNext%"=="TC_SQLDebug_ON" call :TcDebug_TC_SQL_DEBUG ":ON"
 if /I "%doNext%"=="TC_SQLDebug_OF" call :TcDebug_TC_SQL_DEBUG ":OFF"
 if /I "%doNext%"=="TC_TCDef_ON"    call :TC_TCDef ":ON"


 if /I "%doNext%"=="JSE"              call :JSE
 if /I "%doNext%"=="ASE"              call :ASE
 if /I "%doNext%"=="ASA"              call :ASA
 if /I "%doNext%"=="ASR"              call :ASR

 if /I "%doNext%"=="JDE"              call :JDE
 if /I "%doNext%"=="JDR"              call :JDR

 if /I "%doNext%"=="DCL"              call :DCL
 if /I "%doNext%"=="R"  Start  " " Explorer.exe /e, "%JobMgrRoot_DP%"
 goto :eof

rem ============================================================================
:DCL
 call ColorSet.cmd Reset
 Echo.
 Echo ** Directory Clean *******************************************************
 Call ColorSet.cmd Dialog
 Echo  J    = Clean in JobMgrDir...: [%JobMgrRoot_DP%\*.bak;*.tmp;Thumbs.db]
 Echo  T    = Clean Temp Dir
 Echo.
 Echo  Q   = Quit (finish this Dialog)
 set doNextNow=
 set /P doNextNow="please select option: "
 if /I "%doNextNow%" equ "J" Call :DCL_JobMgrDir
 if /I "%doNextNow%" equ "T" CALL "%JobMgrRoot_DP%\03-BinPublic\TempDel.cmd"
 if /I "%doNextNow%" equ "Q" goto :DCL_Exit
 goto :DCL
:DCL_Exit
goto :eof

:DCL_JobMgrDir
 set toRun=del /s "%JobMgrRoot_DP%\*.bak","%JobMgrRoot_DP%\*.tmp","%JobMgrRoot_DP%\Thumbs.db"
 Call ColorSet.cmd IsExecuting
 Echo %toRun%
 %toRun%
goto :eof

rem ============================================================================
:JSE
  rem #New: 10.09.2014/JFes
  set CustomTool_NE=JobManagerStartCustom.cmd
  Echo -------------------------------------------------------------------------
  Echo Edit %CustomTool_NE%
  set CustomToolTemplate_DPNE=%JobMgrRoot_DP%\99-Install\%CustomTool_NE%
  set CustomTool_DPNE=%JobManagerStartCustom_DPNE%
  if not exist "%CustomTool_DPNE%" if exist "%CustomToolTemplate_DPNE%" (
    Echo Info Tool.....: %CustomTool_NE% do not exist
    Echo Create........: New
    Echo from template.: %CustomToolTemplate_DPNE%
    Echo to............: %CustomTool_DPNE%
    copy "%CustomToolTemplate_DPNE%" "%CustomTool_DPNE%"
    pause
  )
  start "" "%EditorEXE%" "%CustomTool_DPNE%"
 goto :eof

rem ============================================================================
:ASE
  rem #New: 10.09.2014/JFes
  set CustomTool_NE=JobManagerAutostartCustom.cmd
  Echo -------------------------------------------------------------------------
  Echo Edit %CustomTool_NE%
  rem 10.09.2014/JFes Edit JobManagerAutostartCustom.cmd
  set CustomToolTemplate_DPNE=%JobMgrRoot_DP%\99-Install\%CustomTool_NE%
  set CustomTool_DPNE=%JobManagerAutostartCustom_DPNE%
  if not exist "%CustomTool_DPNE%" if exist "%CustomToolTemplate_DPNE%" (
    Echo Info Tool.....: %CustomTool_NE% do not exist
    Echo Create........: New
    Echo from template.: %CustomToolTemplate_DPNE%
    Echo to............: %CustomTool_DPNE%
    copy "%CustomToolTemplate_DPNE%" "%CustomTool_DPNE%"
    pause
  )
  start "" "%EditorEXE%" %JobManagerAutostartCustom_DPNE%
goto :eof
rem ............................................................................
:ASA
  Echo -------------------------------------------------------------------------
  if not exist %JobManagerAutostartCustom_DPNE% (
     Echo Autostart: %JobManagerAutostartCustom_DPNE% not exist
     Echo Hint.....: Please Create Autostart first via command 'ASE'
     pause
     goto :eof
  )
  set toRun=jfShellLinkCreate.exe /Quiet
  set toRun=%toRun% /LinkFile="%JobManagerAutostartCustomLinkFile_DPNE%"
  set toRun=%toRun% /LinkDest="%JobManagerAutostartCustom_DPNE%"
  set toRun=%toRun% /WorkDir="%JobManagerAutostartCustom_DP%"
  set toRun=%toRun% /IconFile="%JobMgrRoot_DP%\99-Install\Icons\AutostartJobMgr.ico"
  if not exist "%JobManagerAutostartCustom_DPNE%" goto :next
  %toRun%

  rem #New: 10.12.2014 15:38 // J.Fes
  set toRun=jfShellLinkCreate.exe /Quiet
  set toRun=%toRun% /LinkFile="%JobManagerToolbarCustomLinkFile_DPNE%"
  set toRun=%toRun% /LinkDest="%JobManagerAutostartCustom_DPNE%"
  set toRun=%toRun% /Arguments="/DefaultRunNo"
  set toRun=%toRun% /WorkDir="%JobManagerToolbarCustomLinkFile_DP%"
  set toRun=%toRun% /IconFile="%JobMgrRoot_DP%\99-Install\Icons\AutostartJobMgr.ico"
  %toRun%

:next
  Echo JobManagerV3: Autostart
  Echo Link........: %JobManagerToolbarCustomLinkFile_DPNE%
  Echo Link........: %JobManagerAutostartCustomLinkFile_DPNE%
  Echo Starts......: %JobManagerAutostartCustom_DPNE%
 if exist "%JobManagerAutostartCustomLinkFile_DPNE%" (
  Echo Status......: succsessfull added
  Start "" Explorer "%JobManagerAutostartCustomLinkFile_DP%"
 ) else (
  Echo Status......: ERROR NOT added
 )
 pause
goto :eof

:ASR
  if exist "%JobManagerAutostartCustomLinkFile_DPNE%" del "%JobManagerAutostartCustomLinkFile_DPNE%"
:next
   Echo JobManagerV3: Autostart
   Echo Link........: %JobManagerAutostartCustomLinkFile_DPNE%
   Echo Starts......: %JobManagerAutostartCustom_DPNE%
  if exist "%JobManagerAutostartCustomLinkFile_DPNE%" (
   Echo Status......: ERROR not removed
   Start "" Explorer "%JobManagerAutostartCustomLinkFile_DP%"
  ) else (
   Echo Status......: Removed successfully
  )
  pause
goto :eof

rem ============================================================================
:JDE
  rem #New: 04.11.2014/JFes
  set CustomTool_NE=JobManagerDeploy.cmd
  Echo -------------------------------------------------------------------------
  Echo Edit %CustomTool_NE%
  set CustomToolTemplate_DPNE=%JobMgrRoot_DP%\99-Install\%CustomTool_NE%
  set CustomTool_DPNE=%JobManagerDeployCustom_DPNE%
  if not exist "%CustomTool_DPNE%" if exist "%CustomToolTemplate_DPNE%" (
    Echo Info Tool.....: %CustomTool_NE% do not exist
    Echo Create........: New
    Echo from template.: %CustomToolTemplate_DPNE%
    Echo to............: %CustomTool_DPNE%
    copy "%CustomToolTemplate_DPNE%" "%CustomTool_DPNE%"
    pause
  )
  start "" "%EditorEXE%" "%CustomTool_DPNE%"
goto :eof

:JDR
  rem #New: 04.11.2014/JFes
  set CustomTool_NE=JobManagerDeploy.cmd
  Echo -------------------------------------------------------------------------
  Echo Run %CustomTool_NE%
  rem 10.09.2014/JFes Edit JobManagerAutostartCustom.cmd
  set CustomTool_DPNE=%JobManagerDeployCustom_DPNE%
  if exist "%CustomTool_DPNE%" (
    Call ColorSet.cmd IsExecuting
    Echo Tool Run.....: %CustomTool_NE%
    Echo File ........: %JobManagerDeployCustom_DPNE%
    Call "%CustomTool_DPNE%"
    pause
  ) else (
    Call ColorSet.cmd ERR
    Echo Tool ERROR...: %CustomTool_NE%
    Echo File ........: %JobManagerDeployCustom_DPNE%
    Echo not exist please use bevor JDE
    pause
  )

goto :eof




rem ============================================================================
:NxDebug_UGII_CHECKING_LEVEL
 rem set unset NxDebug Var's
 goto %~1

:ON
 call :setX_U "UGII_CHECKING_LEVEL" "1"
 goto :NxDebug_end

:OFF
 call :setX_U "UGII_CHECKING_LEVEL" ""
 goto :NxDebug_end

:NxDebug_end
goto :eof
rem ============================================================================
:TC_TCDef
 rem #New 13.12.2017/J.Fes
 goto %~1

:ON
 set TC_TMP_DIR=C:\tmp\TCtmp
 if not exist "%TC_TMP_DIR%" md "%TC_TMP_DIR%"
 if exist "%TC_TMP_DIR%" goto :next
 rem Try2
 set TC_TMP_DIR=C:\%temp%\TCtmp
 if not exist "%TC_TMP_DIR%" md "%TC_TMP_DIR%"

:next

 call :setX_U "WhereReferencePOMPref" "1"
 call :setX_U "TC_TMP_DIR" "%TC_TMP_DIR%"
 call :setX_U "TC_KEEP_SYSTEM_LOG" "true"
 goto :NxDebug_end

:OFF
 call :setX_U "WhereReferencePOMPref" ""
 goto :NxDebug_end

:NxDebug_end
goto :eof
rem ============================================================================
:TcDebug_TC_SQL_DEBUG
 rem set unset TCDebug Var's
 goto %~1

:ON
 call :setX_U "TC_SQL_DEBUG" "BJTP"
 goto :NxDebug_end

:OFF
 call :setX_U "TC_SQL_DEBUG" ""
 goto :NxDebug_end

:NxDebug_end
goto :eof
rem ============================================================================
:NxDebug_UGII_DISPLAY_DEBUG
 rem set unset NxDebug Var's
 REM #New 09.02.2017/J.Fes add UGII_DISPLAY_DEBUG
 goto %~1

:ON
 call :setX_U "UGII_DISPLAY_DEBUG" "1"
 goto :NxDebug_end

:OFF
 call :setX_U "UGII_DISPLAY_DEBUG" ""
 goto :NxDebug_end

:NxDebug_end
goto :eof


rem ============================================================================
:NX_Conf_Rule
 rem set TC config rule
 goto %~1

:ON
 set /P TC_config_rule_name="please enter Name for TC_config_rule_name="
 call :setX_U "TC_config_rule_name" "%TC_config_rule_name%"
 goto :End

:OFF
 set TC_config_rule_name=
 call :setX_U "TC_config_rule_name" ""
 goto :End

:End
goto :eof

rem ============================================================================
:setX_U
 if "%~2" neq "" (
   Echo Define User Enviromnent Variable: %~1=%~2
   set %~1=%~2
 ) else (
   Echo Undefine User Enviromnent Variable: %~1
   set %~1=%~2
 )
 call setx.exe %~1 "%~2"

 goto :eof


rem ============================================================================
:SetNXTcEnvVariables
 CALL ColorSet.cmd Standard
 Echo.
 Echo --------------------------------------------------------------------------
 Echo  NX Debug
 Echo --------------------------------------------------------------------------
 Echo  DON  = NX Debug UGII_CHECKING_LEVEL ON  VALUE:[%UGII_CHECKING_LEVEL%]
 Echo  DOF  = NX Debug UGII_CHECKING_LEVEL OFF
 Echo.
 Echo  DDON = NX Debug UGII_DISPLAY_DEBUG ON   VALUE:[%UGII_DISPLAY_DEBUG%]
 Echo  DDOF = NX Debug UGII_DISPLAY_DEBUG OFF
 Echo.
 Echo  TCON = TC_config_rule_name defines for NX whitch TC config Rule is to be used
 Echo  TCOF = TC_config_rule_name undefine
 Echo.
 Echo  TCSQLDBON = SQL Debug = ON  (set TC_SQL_DEBUG=BJTP) VALUE:[%TC_SQL_DEBUG%]
 Echo  TCSQLDBOF = SQL Debug = OFF
 Echo.
 Echo  TCDEF= set TC def TC_TMP_DIR:[%TC_TMP_DIR%]
 Echo         WhereReferencePOMPref:[%WhereReferencePOMPref%]
 Echo.
 Echo  Q   = Quit (finish this Dialog)
 Echo.
 set doNext=
 set /P doNext="please select option: "


 if /I "%doNext%" equ "Q"     goto :eof
 if /I "%doNext%" equ "DON"   set doNext=Nx_Debug_ON
 if /I "%doNext%" equ "DOF"   set doNext=Nx_Debug_OFF
 if /I "%doNext%" equ "DDON"  set doNext=Nx_DebugDisplay_ON
 if /I "%doNext%" equ "DDOF"  set doNext=Nx_DebugDisplay_OFF

 if /I "%doNext%" equ "TCON"  set doNext=NX_Conf_Rule_ON
 if /I "%doNext%" equ "TCOF"  set doNext=NX_Conf_Rule_OF
 if /I "%doNext%" equ "TCSQLDBON" set doNext=TC_SQLDebug_ON
 if /I "%doNext%" equ "TCSQLDBOF" set doNext=TC_SQLDebug_OF
 if /I "%doNext%" equ "TCDEF"     set doNext=TC_TCDef_ON



 rem run it now
 call :doNextExec

 goto :SetNXTcEnvVariables
goto :eof



rem ============================================================================
:DirOrg
 rem Get Appl Version infos
 Call "%CmdCurRoot_DP%\..\01-BinServer\ApplicationInfo.cmd" GetVerInfo

 rem Organiesiert die Verzeichnisse Neu 05.04.2012 10:45
 set ProgramFilesLocal=%ProgramFiles%
 if defined ProgramFiles(x86) set ProgramFilesLocal=%ProgramFiles(x86)%

 set MyApplBase_TP=JobManagerV3
 set MyApplProd_N=PLMJobManagerV3
 set MyApplProd_NShort=JMgrV3


 rem --------- Allgemeine Variablen
 set PsPadEXE=%CmdCurRoot_DP%\..\03-BinPublic\PSPad editor\PSPad.exe
 set EditorEXE=%PsPadEXE%

 set JobManager_WhatsNew_txt_DPNE=%CmdCurRoot_DP%\..\01-BinServer\Resources\Documents\JobManager_WhatsNew.txt
 set BinPublic_Custom_DP=%JobMgrRoot_DP%\03-BinPublic.Custom

 set JobManagerAutostartCustom_DPNE=%BinPublic_Custom_DP%\JobManagerAutostartCustom.cmd
 set JobManagerAutostartCustomLinkFile_DP=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup
 set JobManagerAutostartCustomLinkFile_DPNE=%JobManagerAutostartCustomLinkFile_DP%\JobManagerV3_AutoStart.lnk

 set JobManagerToolbarCustomLinkFile_DP=%JobMgrRoot_DP%\_JobManagerV3\95-ToolsCustom
 set JobManagerToolbarCustomLinkFile_DPNE=%JobManagerToolbarCustomLinkFile_DP%\81-JobManagerV3 (AutoStart).lnk


 set JobManagerStartCustom_DPNE=%BinPublic_Custom_DP%\JobManagerStartCustom.cmd
 set JobManagerDeployCustom_DPNE=%BinPublic_Custom_DP%\JobManagerDeploy.cmd
goto :eof


rem ##### ------------ Standard Subroutines --------------------- ######
:EditMe
 set toRun=start "Edit Me" "%PsPadEXE%" "%1.ppr"
 Echo.
 Echo --------------------------------------------------------------------------
 Echo Start bearbeiten CMD
 echo via: %toRun%
 %toRun%
 goto :eof


rem ============================================================================
:PathCutLastDir
 rem schneidet das letzte Verzeichniss Uebergeben path ab
 rem P1 = Name der Variablen
 rem P2 = der Übergebene Path
 rem Example : call :PathCutLastDir JobMgrRoot_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

:DateTimeNowGet
 if     "%time:~0,1%"==" " set time-h=0%time:~1,1%
 if not "%time:~0,1%"==" " set time-h=%time:~0,2%

 if     "%time:~3,1%"==" " set time-m=0%time:~4,1%
 if not "%time:~3,1%"==" " set time-m=%time:~3,2%

 set Date-Y=%Date:~6,4%
 set Date-M=%Date:~3,2%
 set Date-D=%Date:~0,2%
 set DateTimeNowFileExt=%Date-Y%-%Date-M%-%Date-D%-%time-h%-%time-m%
 goto :eof
