setlocal enabledelayedexpansion CALL %~dp0\..\conf\adm\conf.bat SET /A SYNC_IN_PROGRESS=0 SET /A RUN_ON_SHUTDOWN=0 REM exit if another sync is running reg query "HKLM\Software\FWS\Updates" /v SyncInProgress 1>NUL 2>NUL IF %ERRORLEVEL% EQU 0 ( FOR /F "tokens=3" %%A IN ('reg query "HKLM\Software\FWS\Updates" /v SyncInProgress') DO SET /A SYNC_IN_PROGRESS=%%A ) IF %SYNC_IN_PROGRESS% EQU 1 goto :EOF REM or if updates are already scheduled for next shutdown reg query "HKLM\Software\FWS\Updates\User" /v RunOnShutdown 1>NUL 2>NUL IF %ERRORLEVEL% EQU 0 FOR /F "tokens=3" %%A IN ('reg query "HKLM\Software\FWS\Updates\User" /v RunOnShutdown') DO SET /A RUN_ON_SHUTDOWN=%%A IF %RUN_ON_SHUTDOWN% EQU 0 ( reg query "HKLM\Software\FWS\Updates" /v RunOnShutdown 1>NUL 2>NUL IF %ERRORLEVEL% EQU 0 FOR /F "tokens=3" %%A IN ('reg query "HKLM\Software\FWS\Updates" /v RunOnShutdown') DO SET /A RUN_ON_SHUTDOWN=%%A ) IF %RUN_ON_SHUTDOWN% EQU 1 ( echo %DATE% %TIME% - Updates already scheduled on next shutdown > %SystemDrive%\FWSUpdates\status\sync_log.txt goto :EOF ) REM remove the lock during startup so it'll work if rebooting during a sync reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" /v ResetWPKGSyncStatus /t REG_SZ /d "%SYSTEMROOT%\System32\reg.exe delete \"HKLM\Software\FWS\Updates\" /v SyncInProgress /f" /f REM Now start the sync echo %DATE% %TIME% - Starting new Sync > %SystemDrive%\FWSUpdates\status\sync_log.txt reg add "HKLM\Software\FWS\Updates" /v SyncInProgress /t REG_DWORD /d 1 /f REM Lets map a network drive SET DRIVE=unknown FOR %%D IN (e f g h i j k l m n o p q r s t u v w x y z) do ( cd %%D: IF ERRORLEVEL 1 SET DRIVE=%%D IF ERRORLEVEL 1 goto drivefound ) echo %DATE% %TIME% - couldn't find any free drive letter > %SystemDrive%\FWSUpdates\status\sync_log.txt goto :EOF :drivefound net use %DRIVE%: %REPO_SMB% /PERSISTENT:NO /USER:%USER% %PASS% 1>NUL 2>NUL REM Check if mapping worked IF %ERRORLEVEL% NEQ 0 ( echo %DATE% %TIME% - Error mapping %REPO_SMB% (using CIFS^), lets try webdav >> %SystemDrive%\FWSUpdates\status\sync_log.txt net use %DRIVE%: %REPO_WEB% /PERSISTENT:NO /USER:%USER% %PASS% 1>NUL 2>NUL IF !ERRORLEVEL! NEQ 0 ( echo %DATE% %TIME% - Error mapping %REPO_WEB% (using WEBDAV^), giving up >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto cleanup ) ) REM and if wpkg.js exists IF NOT EXIST "%DRIVE%:\wpkg.js" ( echo %DATE% %TIME% - %DRIVE%:\wpkg.js not found >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto cleanup ) REM Then Mirror the content of the repository on the local drive robocopy /MIR /R:10 /LOG:%SystemDrive%\FWSUpdates\status\log_robocopy.txt %DRIVE%:\ %SystemDrive%\FWSUpdates\repository\ 1>NUL 2>NUL REM Now, we have to check the exit status of the robocopy command REM Fatal Error if %ERRORLEVEL% EQU 16 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done, with errors, mismatches and new files not copied if %ERRORLEVEL% EQU 15 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy failed, with mismatches and new files not copied if %ERRORLEVEL% EQU 14 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done, with errors and mismatches if %ERRORLEVEL% EQU 13 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy failed with mismatches if %ERRORLEVEL% EQU 12 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto :xfererror ) REM Copy done with errors and new files not copied if %ERRORLEVEL% EQU 11 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto :xfererror ) if %ERRORLEVEL% EQU 10 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done with errors if %ERRORLEVEL% EQU 9 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy failed if %ERRORLEVEL% EQU 8 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done, with mismatches and new files not copied if %ERRORLEVEL% EQU 7 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Mismatches and new files not copied if %ERRORLEVEL% EQU 6 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done with mismatches if %ERRORLEVEL% EQU 5 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto :xfererror ) REM Mismatches if %ERRORLEVEL% EQU 4 ( echo %DATE% %TIME% - Robocopy exited with status %ERRORLEVEL% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto xfererror ) REM Copy done, new files detected if %ERRORLEVEL% EQU 3 goto postcopy REM New files detected if %ERRORLEVEL% EQU 2 goto postcopy REM Copy done, we have new files, updates available if %ERRORLEVEL% EQU 1 goto postcopy REM No error, nothing copied, no updates if %ERRORLEVEL% EQU 0 goto postcopy :xfererror echo %DATE% %TIME% - Error occured during sync. See log_robocopy.txt for details >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto cleanup :postcopy echo %DATE% %TIME% - Copy finished successfuly, now checking if changes must be applied >> %SystemDrive%\FWSUpdates\status\sync_log.txt REM FOR /F %%A IN ('cscript %SystemDrive%\FWSUpdates\repository\wpkg.js /query:m ^| find /V "productkey" ^| find /C "ID:"') DO SET /A CHANGES=%%A SET /A CHANGES=0 SET LIST= FOR /F "tokens=2 delims= " %%A IN ('cscript %SystemDrive%\FWSUpdates\repository\wpkg.js /query:m ^| find /V "productkey" ^| find "ID:"') DO ( IF !CHANGES! EQU 0 ( SET LIST=%%A ) ELSE ( SET LIST=!LIST!,%%A ) SET /A CHANGES=!CHANGES!+1 ) echo %DATE% %TIME% - Found %CHANGES% changes >> %SystemDrive%\FWSUpdates\status\sync_log.txt FOR /F "delims=" %%A in ('cscript /nologo %~dp0\now.vbs') DO SET /A NOW=%%A reg add "HKLM\Software\FWS\Updates" /v PendingChanges /t REG_DWORD /d %CHANGES% /f reg add "HKLM\Software\FWS\Updates" /v PendingChangesList /t REG_SZ /d "%LIST%" /f IF %CHANGES% GTR 0 ( reg query "HKLM\Software\FWS\Updates" /v ChangesAvailableSince 1>NUL 2>NUL IF ERRORLEVEL 1 ( reg add "HKLM\Software\FWS\Updates" /v ChangesAvailableSince /t REG_DWORD /d %NOW% /f SET /A SINCE=%NOW% SET /A MAX=%NOW%+%MAX_REPORT%*86400 ) ELSE ( FOR /F "tokens=3 delims= " %%A IN ('reg query "HKLM\Software\FWS\Updates" /v ChangesAvailableSince') DO SET /A SINCE=%%A SET /A MAX=!SINCE!+%MAX_REPORT%*86400 IF %NOW% GTR !MAX! reg add "HKLM\Software\FWS\Updates" /v RunOnShutdown /t REG_DWORD /d 1 /f ) SET /A DELTA=%NOW%-!SINCE! goto copylogs ) ELSE ( reg delete "HKLM\Software\FWS\Updates" /v ChangesAvailableSince /f goto copylogs ) :copylogs REM map another network drive to push logs SET LOGDRIVE=unknown FOR %%D IN (e f g h i j k l m n o p q r s t u v w x y z) do ( cd %%D: IF ERRORLEVEL 1 SET LOGDRIVE=%%D IF ERRORLEVEL 1 goto logdrivefound ) echo %DATE% %TIME% - couldn't find any free drive letter for logs > %SystemDrive%\FWSUpdates\status\sync_log.txt goto :EOF :logdrivefound net use %LOGDRIVE%: %LOGS% /PERSISTENT:NO /USER:%USER% %PASS% 1>NUL 2>NUL REM Check if mapping worked IF %ERRORLEVEL% NEQ 0 ( echo %DATE% %TIME% - Error mapping %LOGS% >> %SystemDrive%\FWSUpdates\status\sync_log.txt goto cleanup ) IF NOT EXIST "%LOGDRIVE%:\keys" mkdir "%LOGDRIVE%:\keys" IF NOT EXIST "%LOGDRIVE%:\logs" mkdir "%LOGDRIVE%:\logs" IF NOT EXIST "%LOGDRIVE%:\databases" mkdir "%LOGDRIVE%:\databases" IF EXIST "%SystemDrive\FWSUpdates\prodkeys\%COMPUTERNAME%.txt" copy /Y "%SystemDrive\FWSUpdates\prodkeys\%COMPUTERNAME%.txt" "%LOGDRIVE%:\keys" IF EXIST "%TEMP%\wpkg-%COMPUTERNAME%.log" copy /Y "%TEMP%\wpkg-%COMPUTERNAME%.log" "%LOGDRIVE%:\logs\%COMPUTERNAME%.log" IF EXIST "%SYSTEMROOT%\system32\wpkg.xml" copy /Y "%SYSTEMROOT%\system32\wpkg.xml" "%LOGDRIVE%:\databases\%COMPUTERNAME%.xml" net use /D %LOGDRIVE%: 1>NUL 2>NUL goto cleanup :cleanup reg delete "HKLM\Software\FWS\Updates" /v SyncInProgress /f net use /D %DRIVE%: 1>NUL 2>NUL