@ECHO OFF
SETLOCAL DisableDelayedExpansion

FOR /F "tokens=2 delims==" %%a IN ('wmic os get BuildNumber /Value') DO set OSBuildNumber=%%a
IF %OSBuildNumber%==2600 set OSArchitecture=x86
IF %OSBuildNumber%==2600 goto COMMON

FOR /F "tokens=2 delims==" %%a IN ('wmic os get OSLanguage /Value') DO set OSLanguage=%%a
IF %OSLanguage%==2052 set OSLanguageID=CHS
IF %OSLanguage%==1028 set OSLanguageID=CHT
IF %OSLanguage%==1029 set OSLanguageID=CSY
IF %OSLanguage%==1031 set OSLanguageID=DEU
IF %OSLanguage%==1033 set OSLanguageID=ENU
IF %OSLanguage%==3082 set OSLanguageID=ESN
IF %OSLanguage%==1036 set OSLanguageID=FRA
IF %OSLanguage%==1038 set OSLanguageID=HUN
IF %OSLanguage%==1040 set OSLanguageID=ITA
IF %OSLanguage%==1041 set OSLanguageID=JPN
IF %OSLanguage%==1042 set OSLanguageID=KOR
IF %OSLanguage%==1043 set OSLanguageID=NLD
IF %OSLanguage%==1045 set OSLanguageID=PLK
IF %OSLanguage%==1046 set OSLanguageID=PTB
IF %OSLanguage%==2070 set OSLanguageID=PTG
IF %OSLanguage%==1049 set OSLanguageID=RUS
IF %OSLanguage%==1053 set OSLanguageID=SVE
IF %OSLanguage%==1055 set OSLanguageID=TRK

FOR /F "tokens=2 delims==" %%a IN ('wmic cpu get AddressWidth /Value') DO set OSArchitecture=%%a
IF %OSArchitecture%==32 set OSArchitecture=x86
IF %OSArchitecture%==64 set OSArchitecture=x64

ECHO Installing KB2868626 to enable SHA-256 support
IF %OSArchitecture%==x64 GOTO x64

"%~dp0x86\windowsserver2003-kb2868626-x86-%OSLanguage%.exe" /q /n /z
GOTO COMMON

:x64
"%~dp0x64\windowsserver2003.windowsxp-kb2868626-x64-%OSLanguageID%.exe" /q /n /z

:COMMON
ECHO Adding registry entries
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Test /v AllowAnySSL /t REG_DWORD /d 1 /f > NUL
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Test /v WuRedirTestEnable /t REG_DWORD /d 1 /f > NUL
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Test /v WuRedirOverride1 /t REG_SZ /d http://ds.download.windowsupdate.com/v11/2/windowsupdate/redir/v6-win7sp1-wuredir.cab /f > NUL
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Test /v WuRedirOverride2 /t REG_SZ /d http://download.microsoft.com/v11/2/windowsupdate/redir/v6-win7sp1-wuredir.cab /f > NUL
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Test /v WuRedirOverride3 /t REG_SZ /d http://fe2.update.microsoft.com/v11/2/windowsupdate/redir/v6-win7sp1-wuredir.cab /f > NUL
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v SecureProtocols /t REG_DWORD /d 0xA8 /f > NUL
ECHO.

ECHO Updating Root Certificates
"%~dp0common\rootsupd.exe"
ECHO.

ECHO Installing latest Windows Update Agent
"%~dp0%OSArchitecture%\WUA-Downlevel.exe" /quiet /norestart /wuforce
ECHO.

ECHO Replacing wuaueng.dll with patched file
del %SystemRoot%\System32\Dllcache\wuaueng.dll > NUL
ren %SystemRoot%\System32\wuaueng.dll wuaueng.dll.bak > NUL
copy "%~dp0%OSArchitecture%\wuaueng.dll" %SystemRoot%\System32 > NUL
ECHO.
ECHO.
ECHO Restart Windows to complete installation.
ECHO.
ECHO Press any key to exit.
pause >nul
