@Echo off
SetLocal EnableExtensions EnableDelayedExpansion

Set DistPid=00000000
Set DistType=Windows
Set DistBuild=Y.M.DD
Set PackRoot=
Set PackType=UpdatePack
Set PackBuild=

rem  ।塞  ਡ⨢
If Defined AMD64 (Set X=!AMD64!) Else (Set X=!I386!)
call :Get_Distributive_Type "!X!" & (Set X=)
call :Get_Distributive_Build

rem  ।⥫ ⨯ UpdatePack'a
call :Get_Pack_Type_Guess_Pid 69712000 "UpdatePack-W2k3-Enu" "Standard Edition"
call :Get_Pack_Type_Guess_Pid 69713000 "UpdatePack-W2k3-Enu" "Enterprise Edition"
call :Get_Pack_Type_Guess_Pid 69753000 "UpdatePack-W2k3-Enu" "Web Edition"
call :Get_Pack_Type_Guess_Pid 69763000 "UpdatePack-W2k3-Enu" "Trial Software"
call :Get_Pack_Type_Guess_Pid 69889000 "UpdatePack-W2k3-Rus" "Enterprise Edition"
call :Get_Pack_Type_Guess_Pid 69890000 "UpdatePack-W2k3-Rus" "Standard Edition"
call :Get_Pack_Type_Guess_Pid 76455000 "UpdatePack-WXP-Rus" "Home Edition"
call :Get_Pack_Type_Guess_Pid 76456000 "UpdatePack-WXP-Rus" "Professional"
call :Get_Pack_Type_Guess_Pid 76477000 "UpdatePack-WXP-Enu" "Home Edition"
call :Get_Pack_Type_Guess_Pid 76487000 "UpdatePack-WXP-Enu" "Professional"
call :Get_Pack_Type_Guess_Pid 76588000 "UpdatePack-WXPx64-Enu" "Professional"
call :Get_Pack_Type_Guess_Pid 76867000 "UpdatePack-W2k3x64-Enu" "Trial Software"
call :Get_Pack_Type_Guess_Pid 76868000 "UpdatePack-W2k3x64-Enu" "Enterprise Edition"
call :Get_Pack_Type_Guess_Pid 76869000 "UpdatePack-W2k3x64-Enu" "Standard Edition"

rem  ।塞 ⨯   UpdatePack'a
call :Get_Pack_Type_and_Build "Files\ForceCopy\RUSUPPCK.IN_" && Set PackRoot=.
call :Get_Pack_Type_and_Build "AMD64\ForceCopy\PCK_X64.IN_" && Set PackRoot=.

Echo  䠩 ADDONS.HTM.
If Exist "Temp\htmlinfo.cmd" Del /F /Q Temp\htmlinfo.cmd
If Exist "%~1\htmlinfo" >nul Copy "%~1\htmlinfo" Temp\htmlinfo.cmd
If Exist "Temp\htmlinfo.cmd" call Temp\htmlinfo.cmd "%~1"

EndLocal
Exit /B 0

rem :Get_Distributive_Type "%I386%"
:Get_Distributive_Type
	(Set Pid=) & (Set Product=) & (Set Localization=) & (Set ServicePack=)
	call :CheckFile "%~1\PRODSPEC.INI" || Exit /B 1
	call :LoadParametr "Product=" "%~1\PRODSPEC.INI"
	call :LoadParametr "Localization=" "%~1\PRODSPEC.INI"
	For /L %%I In (1,1,9) Do If Exist "%~1\SP%%I.CAT" Set ServicePack=SP%%I
	call :CheckFile "%~1\SETUPP.INI" || Exit /B 1
	call :LoadParametr "Pid=" "%~1\SETUPP.INI"
	If Not "!Pid!"=="" Set DistPid=!Pid!
	Set DistType=!Product! !ServicePack! !Localization!
	(Set Pid=) & (Set Product=) & (Set Localization=) & (Set ServicePack=)
Exit /B 0

rem :Get_Distributive_Build
:Get_Distributive_Build
	If "%DATE:~8,-1%"=="0" (Set Y=%DATE:~9%) Else (Set Y=%DATE:~8%)
	If "%DATE:~3,-6%"=="0" (Set X=%DATE:~4,-5%) Else (Set X=%DATE:~3,-5%)
	Set DistBuild=!Y!.!X!.%DATE:~0,-8%
	(Set Y=) & (Set X=)
Exit /B 0

rem  :Get_Pack_Type_Guess_Pid "CheckPid" "PackType"
:Get_Pack_Type_Guess_Pid
	If "!DistPid:~0,-3!000"=="%~1" (
		Set PackType=%~2
	)
Exit /B 0

rem :Get_Pack_Type_and_Build "UpdatePack.IN_"
:Get_Pack_Type_and_Build
	If Not Exist "%~1" Exit /B 1
	call :ExpandFile "%~1" "Temp\%~n1.INF" || Exit /B 1
	call :GetPackType "Temp\%~n1.INF"
Exit /B 0

rem :GetPackType "UpdatePack.inf"
:GetPackType
	call :CheckFile "%~1" || Exit /B 1
	Set OptionDesc=!PackType! !PackBuild!
	call :LoadParametr "OptionDesc" "%~1"
	For /F "Tokens=1,2" %%K In ("!OptionDesc!") Do (
		Set PackType=%%K
		Set PackBuild=%%L
	)
	(Set OptionDesc=)
Exit /B

:CheckFile
	If Not Exist "%~1" Echo %~nx0: file "%~1" not found & Exit /B 1
Exit /B 0

:ExpandFile
	>nul Expand "%~1" "%~2"
	If Not Exist "%~2" Echo %~nx0: error expand file "%~1" & Exit /B 1
Exit /B 0

:LoadParametr
	For /F "Skip=2 Tokens=1,* Delims==	 " %%I In ('Find /I "%~1" "%~2"') Do (
		Set %%I=%%~J
	)
Exit /B 0
