@Echo off

goto begin
 rem ----------------------- Dokumentation --------------


 Dokumentation:
 Diese Batch Routine
 Verteilt alle erforderlichen Programme Elemente auf die Standorte

 Datum        wer     Info
 18.04.2008   J.Fes   Ersterstellung
 20.11.2008   J.Fes   Verteilung so angepasst das die Softwareverteilung Automatisch per AT Job erfolgen kann
 11.12.2009   J.Fes   Root Path angepasst nach verlegung der Files nach
 04.06.2010   J.Fes   ueberarbeitung des verteil scripte's

:begin rem --------------- begin ------------------------

 rem new 02.02.2010 21:48 J.Fes
 setlocal EnableDelayedExpansion EnableExtensions


 rem -- basisverzeichnisse festhalten
 set MyCurCmd=%0
 set CmdCurRoot_DP=%~dp0
 set CmdCurRoot_DP=%CmdCurRoot_DP:~0,-1%

 set AppRoot_TP=JobManagerV2
 set distributMode=%1
 if  "%distributMode%"=="" set distributMode=Settings

 rem anegepasst 11.12.2009 10:16 J.Fes
 set AppSourceRoot_DP=\\MyServer\MyUnc\%AppRoot_TP%
 call :PathCutLastDir JobMgrRoot_DP %~dp0

 rem set path
 set path=%CmdCurRoot_DP%;%AppSourceRoot_DP%\03-BinPublic;%path%
:DistripMenu
 Echo.
 Echo --------------------------------------------------------------------------
 Echo BSH NX6 Refile Distripmenu
 Echo.
 Echo distributMode = %distributMode%
 Echo.
 Echo ALL = to all Sites
 Echo.
 Echo S1 = Standort 1
 Echo.
 Echo Q   = Quit do nothing
 Echo.
 set /P SiteLocation="Imput function or Site:"

 if /I "%SiteLocation%" equ "ALL" call :DistripToAllSites& goto :DistripMenu
 if /I "%SiteLocation%" equ "Q"   Echo Do Exit & Pause& goto :eof
 rem in allen anderen Faellne distrip to Site:
 call :Verteil BLN %AppSourceRoot_DP% %SiteLocation%& Pause
 goto :DistripMenu

 goto :eof

:DistripToAllSites
 rem ***************** Definition der Zielpfade ******************
 rem ** Ziel NameS_01 Name Long
 call banner Distr:ALL
 call :Verteil BLN %AppSourceRoot_DP% S1
 Echo --------------------------------------------------------------------------
 Echo All:Done distributMode:[%distributMode%]
 Banner Dis:All:Done
 Echo --------------------------------------------------------------------------
 pause

 goto :eof


rem -------------------------------------
:Verteil
 rem P1 = Bezeichnung Standort Quelle (Std1 ..)
 rem P2 = UNC Quelle Standort z.b: 
 rem P3 = Bezeichnung Standort (Dest1 ..)
 set Source_ShortName=%1
 set Source_DP=%2
 set Dest_ShortName=%3
 if /i "%Source_ShortName%" equ "%Dest_ShortName%" (
  Echo Error: Source_ShortName is Dest_ShortName
  Echo        ["%Source_ShortName%"] == ["%Dest_ShortName%"]
  Echo do not copy it on it selfe
  pause
  goto :MyEnd
 )


 rem -- Robocopy LogFile erstellen
 set Log_DP=%~dp0\Verteil_JobServerV2_LOG
 rem Echo Log_DP="%Log_DP%"
 if not exist "%Log_DP%" md "%Log_DP%"
 call :DateTimeNowGet
 set Log_DPNE=%Log_DP%\%Source_ShortName%-to-%Dest_ShortName%-%DateTimeNowFileExt%_Log.txt

 rem -- Result LogFile erstellen
 set ResultLog_DPNE=%Log_DP%\_DistributResult_Log.csv
 rem Header in csv Datei erzeugen
 if not exist "%ResultLog_DPNE%" Echo Dest.ShortName;Date/Time;Result(OK/Err);Host/User;Start;End>>"%ResultLog_DPNE%"

 rem -- Site Mapping ausfuehren zum %Dest_ShortName% wird das passende RfShare_DP ermittelt
 Call "%CmdCurRoot_DP%\SiteMapping.cmd" %Dest_ShortName% RfShare_DP
 if "%SiteMappingResCode%" neq "0" goto :ErrSiteMapping

 set Dest_DP=%RfShare_DP%\JobManagerV2
 call :DateTimeNowGet
 set StartTime=%Date-D%.%Date-M%.%Date-Y% %time-h%:%time-m%
 Echo.
 Echo.
 Echo --------------------------------------------------------------------------
 Banner %Dest_ShortName%
 Echo --------------------------------------------------------------------------
 Title "distribute JobManager from:%Source_ShortName% to:%Dest_ShortName%" (distributMode=%distributMode%)
 Echo.
 Echo distribute (%distributMode%)
 Echo from: %Source_ShortName%
 Echo       %Source_DP%
 Echo to:   %Dest_ShortName%
 Echo       %Dest_DP%

 if not exist "%Dest_DP%" (
   Echo Create JobMgr Root "%Dest_DP%"
   md "%Dest_DP%"
 )

 rem check if i can create Dest_DP
 if not exist "%Dest_DP%" goto :ErrCreate_Dest_DP

 Call :ColorSet OK
 banner Dir:Ok
 rem -- alte Toolbar löschen --- entnommen 10.07.2010 18:46
REM  if exist "%Dest_DP%\_JobManagerV2" (
REM   Echo Toolbar loeschen via rd /s /q  "%Dest_DP%\_JobManagerV2"
REM   rd /s /q  "%Dest_DP%\_JobManagerV2"
REM  )

REM  set distributMode=YES

 if /I "%distributMode%" equ "All" (
  Rem --
  rem call :RobocopyRun 03-BinPublic *.*
  rem call :RobocopyRun 01-BinServer *.*
  call :RobocopyRun 02-BinClient *.*
  rem new 11.06.2010 15:13
  if exist "%Dest_DP%\03-BinPublic.Custom\Verteil_JobserverV2_BSH.cmd" del /q "%Dest_DP%\03-BinPublic.Custom\*.*"

 )
REM  call :RobocopyRun 03-BinPublic.Custom "SiteMapping.cmd NX6Refile_NetConnectRfNx6.cmd" /Lev:1
 call :RobocopyRun 03-BinPublic.Custom *.* /Lev:1
 call :RobocopyRun 03-BinPublic.Custom\PicPick *.* /Lev:1
 
 rem -- Settings verteilen ----------
 call :RobocopyRun 01-BinServer.Custom *.* /Lev:1
 call :RobocopyRun 02-BinClient.Custom *.* /Lev:1
 call :RobocopyRun 90-DATA\BSH_SettingsGlobal\20-JobComputerScripts *.* /Lev:1
 rem -- Toolbar _Nx6Rf Verteilen ----
 call :RobocopyRun _Nx6Rf *.*

 call :DateTimeNowGet
 set EndTime=%Date-D%.%Date-M%.%Date-Y% %time-h%:%time-m%
 Echo --------------------------------------------------------------------------
 Echo distributed OK
 Echo from: %Source_ShortName%
 Echo       %Source_DP%
 Echo to:   %Dest_ShortName%
 Echo       %Dest_DP%
 Echo.
 Echo Log: %Log_DPNE%
 
 rem -- ok everything is fine
 Call :Echo "%Dest_ShortName%;%EndTime%;OK;from:[%ComputerName%] user:%USERDOMAIN%\%UserName%;%StartTime%;%EndTime%"    "%ResultLog_DPNE%"
 goto :MyEnd
 
 
 :ErrCreate_Dest_DP
  Call :ColorSet ERR
  call :DateTimeNowGet
  set EndTime=%Date-D%.%Date-M%.%Date-Y% %time-h%:%time-m%
  Echo --------------------------------------------------------------------------
  Echo distributed ERR:[cannot create RootDir:%Dest_DP%]
  Call :Echo "%Dest_ShortName%;%EndTime%;ERR:[cannot create RootDir:%Dest_DP%];from:[%ComputerName%] user:%USERDOMAIN%\%UserName%;%StartTime%;%EndTime%" "%ResultLog_DPNE%"

  goto :MyEnd

 :ErrSiteMapping
  Call :ColorSet ERR
  call :DateTimeNowGet
  set EndTime=%Date-D%.%Date-M%.%Date-Y% %time-h%:%time-m%
  Echo --------------------------------------------------------------------------
  Echo distributed ERR:[SiteMappingMsg:%SiteMappingMsg%]
  Call :Echo "%Dest_ShortName%;%EndTime%;ERR:[SiteMappingMsg:%SiteMappingMsg%];from:[%ComputerName%] user:%USERDOMAIN%\%UserName%;%StartTime%;%EndTime%" "%ResultLog_DPNE%"
  goto :MyEnd


 :MyEnd
 Banner %SiteLocation%:Done
 goto :eof

:RobocopyRun
 set SubDir_TP=%1
 set FilesToCopy=%2
 set FilesToCopy=%FilesToCopy:"=%
 set RobocopyOpt=%3 %4 %5 %6 %7 %8 %9
 set ExcludeFiles=%MyCurCmd% *.tmp *.bak *TeamViewer.exe ~*.* Verteil_*.txt *_log.txt
 rem 07.06.2010 18:44 "PSPad editor" entnommen
 rem  PsTools PicPick entnommen
 set ExcludeDirs=90-DATEN DesktopIconOrg_LuckieDIPS MWSnap Sysinternals UltraVnc 7-zip GnuWin32 SearchSqlServer Setup Archiv

 Echo Verteile "%SubDir_TP%"
 set toRun="%AppSourceRoot_DP%\03-BinPublic\robocopy.exe" "%Source_DP%\%SubDir_TP%" "%Dest_DP%\%SubDir_TP%" %FilesToCopy% /FFT /TBD /mir /TEE /r:4 /w:1 /np /XD %ExcludeDirs%  /XF %ExcludeFiles% %RobocopyOpt% /log+:"%Log_DPNE%"
 %toRun%
 goto :eof

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

rem --------------------------------------------------------------------
:DateTimeNowGet
 rem call :DateTimeNowGet
 rem Set File_LPNE=%DatRoot_LP%\MyName-%DateTimeNowFileExt%.txt


 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


rem --------------------------------------------------------------------
:Wait
 set waitTime=2
 if not "%1"=="" set waitTime=%1
 CHOICE /C:w /N /T:w,%waitTime% warte %waitTime% [sec] oder "w" fuer weiter druecken.
 goto :eof

rem --------------------------------------------------------------------
:ConnectNetUnc
 rem Example: call :ConnectNetUnc "JF-18" .  \\JF-18\C$ JF-18\Administrator xxx
 rem Example: call :ConnectNetUnc "JF-18" V: \\JF-18\Projekte$ JF-18\Administrator xxx
 setlocal
 set Beschreibung=%1
 set LW=%2
 set UNC=%3
 set Us=%4
 set Pw=%5
 Echo ------------------------------------
 Echo Verbinde   : %Beschreibung%
 Echo Laufwerk   : %LW%
 Echo zu UNC     : %UNC%
 Echo via User   : %US%

 if     "%LW%"=="." goto LWNo
 if not "%LW%"==""  goto LWYes

 :LWno
   net use %UNC% /user:%US% %Pw%>nul
   if not exist %UNC% Echo - Error    : UNC %UNC% konnte nicht verbunden werden & Pause
   if     exist %UNC% Echo ++OK       : UNC %UNC% konnte erfogreich verbunden werden
   goto :ConnectNetUncEnd

 :LWYes
   if exist %LW% Echo Info Lw    : %LW% bereits vorhanden diese zunaechst getrennt& net use %LW% /d /y>nul
   net use %LW% %UNC% /user:%US% %Pw%>nul
   if not exist %LW%  Echo - Error    : Laufwerk %LW% konnte !!NICHT!! mit %UNC% verbunden werden & Pause
   if     exist %LW%  Echo + OK       : Laufwerk %LW% mit %UNC% erfogreiche verbunden
   goto :ConnectNetUncEnd

 :ConnectNetUncEnd
  endlocal

 goto :eof


:Parse_ALL
 rem parst alle File Bezeichner aus
 rem "\" abschneiden
 set Parse_all$temp=%~dp1
 set Parse_all$temp=%Parse_all$temp:~0,-1%
 set %2_DP=%Parse_all$temp%
 set %2_D=%~d1
 rem "\" hinten und vorne abschneiden
 set Parse_all$temp=%~p1
 set Parse_all$temp=%Parse_all$temp:~1,-1%
 set %2_P=%Parse_all$temp%
 set %2_NE=%~nx1
 set %2_N=%~n1
 set %2_E=%~x1
 set %2_DPNE=%1
 set %2_DPN=%~dpn1
 set Parse_all$temp=
goto :eof

rem ============================================================================
:Echo
 rem Echos Text auf die Console und in eine Datei (wenn angegeben)
 setlocal

   rem - TextToEcho erstellen
   set TextToEcho=%1
   set TextToEcho=%TextToEcho:"=%
   if "%TextToEcho%" equ ""  set TextToEcho=.
   if "%TextToEcho%" neq "." set TextToEcho= %TextToEcho%

   rem - Ausgabe in File
   set EchoToFile_DPNE=
   if "%2" neq "" (
    set EchoToFile_DPNE=%2
    set EchoToFile_DPNE=!EchoToFile_DPNE:"=!
    Echo!TextToEcho! >>"!EchoToFile_DPNE!"
   )

   set EchoToFile_DPNE=
   if "%3" neq "" (
    set EchoToFile_DPNE=%3
    set EchoToFile_DPNE=!EchoToFile_DPNE:"=!
    Echo!TextToEcho! >>"!EchoToFile_DPNE!"
   )

   rem - Ausgabe Console
   Echo%TextToEcho%


   rem - Ausgabe in :TracAc
   rem if "%TextToEcho%" neq "." call :TracAc "%TextToEcho%"

 :EchoEnd
 endlocal
 goto :eof

rem ============================================================================
:Wait
 set waitTime=2
 if not "%1"=="" set waitTime=%1
 CHOICE /C:w /N /T:w,%waitTime% warte %waitTime% [sec] oder "w" fuer weiter druecken.
 goto :eof
:ende

rem ============================================================================
:ColorSet
 rem -- cmd Color Farbinfo:
 REM    0 = Schwarz        8 = Dunkelgrau
 REM    1 = Dunkelblau     9 = Blau
 REM    2 = Dunkelgrün     A = Grün
 REM    3 = Blaugrün       B = Zyan
 REM    4 = Dunkelrot      C = Rot
 REM    5 = Lila           D = Magenta
 REM    6 = Ocker          E = Gelb
 REM    7 = Hellgrau       F = Weiß

 rem -- Farben nach Status setzen
 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%"=="" color %JobColorLast%
 goto :eof
