@echo off

rem script:	   abbodi1406, @rgadguard
rem wimlib:	   synchronicity
rem Thanks to: @Ratiborus58, @NecrosoftCore, @DiamondMonday, @WzorNET

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

call bin\update.cmd
title Decrypt UUPs -^> ISO - %v_ver_decrypt%
echo Preparing...
if not exist "%~dp0bin\wimlib-imagex.exe" goto :eof
set "dism=%~dp0bin\dism.exe"
IF /I "%PROCESSOR_ARCHITECTURE%" EQU "AMD64" (SET "wimlib=%~dp0bin\bin64\wimlib-imagex.exe") ELSE (SET "wimlib=%~dp0bin\wimlib-imagex.exe")
cd /d "%~dp0"
setlocal EnableExtensions
setlocal EnableDelayedExpansion
color 0a
set bootedit=temp\boot.txt
SET UUP=
SET ERRORTEMP=
SET VOL=0
SET uups_esd_num=0
set uupa=0
set dual=0
SET "ramdiskoptions={7619dcc8-fafe-11d9-b411-000476eba25f}"
for /f "tokens=3 delims=: " %%b in ('dism /english /online /Get-Intl ^| find /i "System locale"') do set dlang=%%b && call :dlang
rmdir /s /q ISOFOLDER\ >nul 2>nul
rmdir /s /q %~dp0bin\temp\ >nul 2>nul
rmdir /s /q %~dp0temp\ >nul 2>nul
mkdir bin\temp >nul 2>nul
mkdir temp >nul 2>nul

if not [%1]==[] (if exist "%~1\*.esd" (set "UUP=%~1"&goto :check) else (set "fold=%~1"&goto :uup_fold))
if exist "%CD%\UUPs\*.esd" set "fold=%CD%\uups"&goto :uup_fold
if exist "%CD%\UUP\*" set "fold=%CD%\uup"&goto :uup_fold

:prompt
cls
set fold=
echo.
call :as
echo %txt01%
call :as
echo.
set /p fold=
if [%fold%]==[] goto :QUIT
goto :uup_fold

:check
set "fold="
call :uup_esd %UUP%
if %uups_esd_num% equ 0 (
	echo.
	call :as
	echo %txt02%
	call :as
	echo.
	echo %exitd%
	pause >nul
	goto :QUIT
)
if %uups_esd_num% gtr 1 (
	for /L %%i in (1, 1, %uups_esd_num%) do call :uups_esd %%i
	goto :MULTIMENU
)
call :uups_esd 1
set "MetadataESD=%UUP%\%uups_esd1%"&set "arch=%arch1%"&set "editionid=%edition1%"&set "langid=%langid1%"&set "nameos=!name1!"
goto :MAINMENU

:MULTIMENU
cls
set "fold=0"
set userinp=
call :as
echo %txt03%
call :as
echo.
for /L %%i in (1, 1, %uups_esd_num%) do (
	echo %%i. !name%%i!
)
echo.
call :as
echo %txt04%
call :as
set /p userinp= ^> %txt05%: 
set userinp=%userinp:~0,6%
if %userinp%==0 goto :QUIT
if /i %userinp%==alliso set "uupa=1"&set "uupb=iso"&set WIMFILE=install.wim&goto :ALL
if /i %userinp%==allisz set "uupa=1"&set "uupb=iso"&set WIMFILE=install.esd&goto :ALL
if /i %userinp%==aiowim set "uupa=2"&set WIMFILE=install.wim&goto :AIO
if /i %userinp%==aioesd set "uupa=2"&set WIMFILE=install.esd&goto :AIO
if /i %userinp%==allwim set "uupa=1"&set "uupb=install"&set WIMFILE=install.wim&goto :ALL
if /i %userinp%==allesd set "uupa=1"&set "uupb=install"&set WIMFILE=install.esd&goto :ALL
for /L %%i in (1, 1, %uups_esd_num%) do (
	if %userinp%==%%i set "MetadataESD=%UUP%\!uups_esd%%i!"&set "arch=!arch%%i!"&set "editionid=!edition%%i!"&set "langid=!langid%%i!"&set "nameos=!name%%i!"&goto :MAINMENU
)
goto :MULTIMENU

:MAINMENU
cls
set userinr=
call :as
echo %txt17%: %nameos%
call :as
echo.
echo.       1 - %txt07%
echo.       2 - %txt08%
echo.       3 - %txt20%
echo.       4 - %txt21%
echo.       5 - %txt06%
echo.
call :as
if [%fold%]==[] (echo %txt04%) else (echo %txt09%)
call :as
set /p userinr= ^> %txt10%: 
set userinr=%userinr:~0,1%
if %userinr%==0 GOTO :QUIT
if not [%fold%]==[] (if %userinr%==9 if [%fold%]==[0] (GOTO :MULTIMENU) else (GOTO :uup_fold))
if %userinr%==5 GOTO :INFO
if %userinr%==4 (set WIMFILE=install.esd&goto :Single)
if %userinr%==3 (set WIMFILE=install.wim&goto :Single)
if %userinr%==2 (set WIMFILE=install.esd&goto :ISO)
if %userinr%==1 (set WIMFILE=install.wim&goto :ISO)
GOTO :MAINMENU

:uup_fold
set userinp=
set "b=0"
set "c=0"
set "d=0"
for %%A in ("%fold%") do set fold=%%~fA
set "UUP=%fold%"
call :uup_esd %fold%
dir /b /a:d /o:d /t:c "%fold%">>temp\uups_fol.txt 2>nul
for /f "tokens=3 delims=: " %%i in ('find /v /n /c "" temp\uups_fol.txt') do set uups_fol_num=%%i
cls
if %uups_fol_num% neq 0 (
	set /a c+=%uups_fol_num%
	echo.
	call :as
	echo %txt22% %fold%
	call :as
	echo.
	for /f "tokens=* delims=" %%a in ('dir /b /a:d /o:d /t:c "%fold%"') do (
		if /i amd64==%%a set /a d+=1
		if /i x86==%%a set /a d+=1
		set /a b+=1
		echo   f!b!. %%a
	)
)
if %uups_esd_num% neq 0 (
	set /a c+=%uups_esd_num%
	echo.
	call :as
	echo %txt23% %fold%
	call :as
	echo.
	for /L %%i in (1, 1, %uups_esd_num%) do (
		call :uups_esd %%i
		echo   e%%i. !name%%i!
	)
)
if /i %c%==0 (
	echo.
	call :as
	echo %txt24% %fold%
	call :as
)
echo.
call :as
echo %txt25%
echo %txt26%
if %d%==2 (echo %txt27%)
call :as
set /p userinp= ^> %txt10%: 
set userinp=%userinp:~0,6%
if %uups_fol_num% neq 0 (
	set "f=0"
	for /f "tokens=* delims=" %%a in ('dir /b /a:d /o:d /t:c "%fold%"') do (
		set /a f+=1
		if /i %userinp%==f!f! set "fold=%fold%\%%a"&goto :uup_fold
	)
)
if %d%==2 (if /i %userinp%==d goto :dCheck)
if %uups_esd_num% neq 0 (
	for /L %%i in (1, 1, %uups_esd_num%) do (
		if /i %userinp%==e%%i (
			call :uups_esd %%i
			set "MetadataESD=!UUP!\!uups_esd%%i!"&set "arch=!arch%%i!"&set "editionid=!edition%%i!"&set "langid=!langid%%i!"&set "nameos=!name%%i!"
			goto :MAINMENU
		)
	)
		if /i %userinp%==alliso set "uupa=1"&set "uupb=iso"&set WIMFILE=install.wim&goto :ALL
		if /i %userinp%==allisz set "uupa=1"&set "uupb=iso"&set WIMFILE=install.esd&goto :ALL
		if /i %userinp%==aiowim set "uupa=2"&set WIMFILE=install.wim&goto :AIO
		if /i %userinp%==aioesd set "uupa=2"&set WIMFILE=install.esd&goto :AIO
		if /i %userinp%==allwim set "uupa=1"&set "uupb=install"&set WIMFILE=install.wim&goto :ALL
		if /i %userinp%==allesd set "uupa=1"&set "uupb=install"&set WIMFILE=install.esd&goto :ALL
)
if /i %userinp%==b set "fold=%fold%\.."&goto :uup_fold
if /i %userinp%==q goto :QUIT
if %userinp%==0 goto :prompt
GOTO :uup_fold

:uup_esd
if exist "temp\*.txt" del /f /q "temp\*.txt" >nul 2>&1
for /d %%A in (Starter,StarterN,Cloud,CloudN,CloudE,CloudEN,CoreCountrySpecific,CoreSingleLanguage,Core,CoreN,ProfessionalCountrySpecific,ProfessionalSingleLanguage,Professional,ProfessionalN,ProfessionalEducation,ProfessionalWorkstation,Education,EducationN,Enterprise,EnterpriseN,EnterpriseEval,EnterpriseNEval,EnterpriseS,EnterpriseSN,EnterpriseSEval,EnterpriseSNEval,EnterpriseG,EnterpriseGN,PPIPro) do (
	dir /b %1\%%A_*.esd>>temp\uups_esd.txt 2>nul
)
for /f "tokens=3 delims=: " %%i in ('find /v /n /c "" temp\uups_esd.txt') do set uups_esd_num=%%i
exit /b

:ALL
for /L %%i in (1, 1, %uups_esd_num%) do (
	set "MetadataESD=%UUP%\!uups_esd%%i!"&set "arch=!arch%%i!"&set "editionid=!edition%%i!"&set "langid=!langid%%i!"&set "numin=%%i"
	if /i %uupb%==iso call :ISO
	if /i %uupb%==install call :Single
)
if /i %uupb%==install ("%wimlib%" optimize %WIMFILE%)
rmdir /s /q ISOFOLDER\ 1>nul 2>nul
rmdir /s /q bin\temp\ 1>nul 2>nul
rmdir /s /q temp\ 1>nul 2>nul
echo.
echo %exitd%
pause >nul
GOTO :QUIT

:AIO
set "MetadataESD=%UUP%\%uups_esd1%"&set "arch=%arch1%"&set "editionid=%edition1%"&set "langid=%langid1%"

:ISO
if %dual%==0 (
	cls
	set "ISOFOLDER=ISOFOLDER"
	call :PREPARE
)
if %dual%==1 (
	echo.
	call :as
	echo %txt28% ^(%arch%^)
	call :as
)
call :convert
echo.
call :as
echo %creat% Setup Media Layout . . .
call :as
IF EXIST %ISOFOLDER% rmdir /s /q %ISOFOLDER%\
mkdir %ISOFOLDER%
echo.
"%wimlib%" apply "%MetadataESD%" 1 %ISOFOLDER%\ >nul 2>&1
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %txt14%&PAUSE&GOTO :QUIT)
del %ISOFOLDER%\MediaMeta.xml >nul 2>&1
echo.
call :as
echo %creat% boot.wim . . .
call :as
for /f "tokens=3 delims=<>" %%i in ('bin\imagex.exe /info "%MetadataESD%" 2 ^| findstr /i HIGHPART') do set "installhigh=%%i"
for /f "tokens=3 delims=<>" %%i in ('bin\imagex.exe /info "%MetadataESD%" 2 ^| findstr /i LOWPART') do set "installlow=%%i"
%dism% /export-image /SourceImageFile:%MetadataESD% /SourceIndex:2 /DestinationImageFile:%~dp0%ISOFOLDER%\sources\boot.wim /compress:max /bootable
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo Errors were reported during export.&echo.&echo Press any key to exit.&pause >nul&GOTO :QUIT)
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 1 --image-property LASTMODIFICATIONTIME/HIGHPART=%installhigh% --image-property LASTMODIFICATIONTIME/LOWPART=%installlow% 1>nul 2>nul
for /f "tokens=4,5,6,7,8,9 delims=: " %%G in ('bin\wimlib-imagex info "%MetadataESD%" 2 ^| find "Creation Time"') do (set mmm=%%G&set yyy=%%L&set ddd=%%H&set "timeout=%%I:%%J:%%K"
	call :setmmm1 !mmm! 1	
)
for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex info "%MetadataESD%" 2 ^| find "Last Modification Time"') do (set mmm=%%G&set yyy=%%L&set ddd=%%H&set "timeout=%%I:%%J:%%K"
	call :setmmm1 !mmm! 2
)
copy /y %ISOFOLDER%\sources\boot.wim temp\winre.wim >nul
::bin\nircmdc.exe setfiletime "temp\winre.wim" "%datetime1%" "%datetime2%" "%datetime2%"
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 1 --image-property FLAGS=9 1>nul 2>nul
for /f "tokens=6 delims=[]. " %%G in ('ver') do set winbuild=%%G
if %winbuild% LEQ 7600 (copy /y .\bin\reagent.xml .\%ISOFOLDER%\sources >nul 2>&1) else (call :Boot)
if /i %build% GEQ 21990 call :fix11
echo.
call :as
echo %creat% %WIMFILE% . . .
call :as
echo.
for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| find /i "Last Modification Time"') do (set mmm=%%G&set "isotime=%%H/%%L,%%I:%%J:%%K")
call :setdate %mmm%
if /i %uupa%==2 (
for /L %%i in (1, 1, %uups_esd_num%) do (
		set "MetadataESD=%UUP%\!uups_esd%%i!"
		call :install %%i %ISOFOLDER%^\sources^\
	)
) else call :install 1 %ISOFOLDER%^\sources^\
echo.
call :as
echo %txt13% %WIMFILE% . . .
call :as
echo.
if /i %uupa%==2 (
	for /L %%i in (1, 1, %uups_esd_num%) do call :add_winre %%i %ISOFOLDER%^\sources^\
) else call :add_winre 1 %ISOFOLDER%^\sources^\
echo.
call :as
echo %optimate% %WIMFILE%...
call :as
echo.
"%wimlib%" optimize %ISOFOLDER%\sources\%WIMFILE%
if %dual%==1 (
	del /f /q "temp\*" >nul 2>&1
	exit /b
)
echo.
call :as
echo %creat% ISO . . .
call :as
bin\cdimage -bootdata:2#p0,e,b"ISOFOLDER\boot\etfsboot.com"#pEF,e,b"ISOFOLDER\efi\Microsoft\boot\efisys.bin" -o -m -u2 -udfver102 -t%isotime% -g -l%DVDLABEL% ISOFOLDER %DVDISO%
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (
	echo.
	echo %txt16%
	echo.
	echo %exitd%
	pause >nul
	exit
)
if /i %uupa%==1 goto :eof
rmdir /s /q ISOFOLDER\ 1>nul 2>nul
rmdir /s /q bin\temp\ 1>nul 2>nul
rmdir /s /q temp\ 1>nul 2>nul
echo.
echo %exitd%
pause >nul
GOTO :QUIT

:Single
cls
if /i %uupa%==0 (
	IF EXIST "%CD%\%WIMFILE%" (
		echo.
		call :as
		echo %txt18%
		call :as
		echo.
		echo %exitd%
		pause >nul
		GOTO :QUIT
	)
)
call :convert
echo.
call :as
echo %creat% %WIMFILE% . . .
call :as
echo.
call :install 1
echo.
call :as
echo %txt13% %WIMFILE% . . .
call :as
if not exist "temp\winre.wim" %dism% /export-image /SourceImageFile:%MetadataESD% /SourceIndex:2 /DestinationImageFile:%~dp0temp\winre.wim /compress:max /bootable
if /i %uupa%==1 (
	call :add_winre %numin%
) else call :add_winre 1
echo.
call :as
echo %optimate% %WIMFILE%...
call :as
echo.
"%wimlib%" optimize %WIMFILE%
echo.
echo %done%
echo.
if /i %uupa%==1 goto :eof
echo %exitd%
pause >nul
GOTO :QUIT

:INFO
CALL :PREPARE
cls
call :as
echo                     %txt06%
call :as
echo.
echo        OS: %_os%
echo      Arch: %arch%
echo  Language: %langid%
echo   Version: %_ver1%.%_ver2%.%build%.%svcbuild%
echo    Branch: %branch%
echo.
echo %exitc% . . .
pause >nul
GOTO :MAINMENU

:PREPARE
"%wimlib%" info "%MetadataESD%" 3 | find /i "Display Name" 1>nul && (
	for /f "tokens=2* delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| find /i "Display Name"') do set "_os=%%j"
) || (
	for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| findstr /b "Name"') do set "_os=%%j"
)
for /f "tokens=4 delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| find /i "Service Pack Build"') do set svcbuild=%%i
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| findstr /b "Build"') do set build=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| find /i "Major"') do set _ver1=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex info "%MetadataESD%" 3 ^| find /i "Minor"') do set _ver2=%%i
"%wimlib%" extract "%MetadataESD%" 1 sources\ei.cfg --dest-dir=.\temp --no-acls >nul 2>&1
type .\temp\ei.cfg 2>nul | find /i "Volume" 1>nul && set VOL=1
"%wimlib%" extract "%MetadataESD%" 1 sources\SetupPlatform.dll --dest-dir=.\temp --no-acls >nul 2>&1
bin\7z.exe l .\temp\SetupPlatform.dll >.\temp\version.txt 2>&1
for /f "tokens=4,5 delims=. " %%i in ('"findstr /B "FileVersion" .\temp\version.txt" 2^>nul') do set version=%%i.%%j
for /f "tokens=7 delims=.) " %%i in ('"findstr /B "FileVersion" .\temp\version.txt" 2^>nul') do set datetime=%%i
for /f "tokens=6 delims=.( " %%i in ('"findstr /B "FileVersion" .\temp\version.txt" 2^>nul') do set branch=%%i
if /i %arch%==x86 (set _ss=x86) else (set _ss=amd64)
if /i %arch%==arm64 (set _ss=arm64)
"%wimlib%" extract "%MetadataESD%" 3 Windows\WinSxS\Manifests\%_ss%_microsoft-windows-coreos-revision* --dest-dir=.\temp --no-acls >nul 2>&1
for /f "tokens=6,7 delims=_." %%i in ('dir /b /o:d .\temp\*.manifest') do set revision=%%i.%%j
if not %version%==%revision% (
	set version=%revision%
	for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex.exe info "%MetadataESD%" 3 ^| find /i "Last Modification Time"') do (set mmm=%%G&set yyy=%%L&set ddd=%%H-%%I%%J)
	call :setmmm !mmm!
)
call bin\revision.cmd
set _label2=
if /i %branch%==WinBuild (
	"%wimlib%" extract "%MetadataESD%" 3 \Windows\System32\config\SOFTWARE --dest-dir=.\temp --no-acls >nul
	reg load HKLM\TEMP .\temp\SOFTWARE >nul
	for /f "skip=2 tokens=3,4,5,6,7 delims=. " %%i in ('"reg query "HKLM\TEMP\Microsoft\Windows NT\CurrentVersion" /v BuildLabEx" 2^>nul') do if not errorlevel 1 set _label2=%%i.%%j.%%m.%%l_CLIENT
	for /f "skip=2 tokens=3 delims= " %%i in ('reg query "HKLM\TEMP\Microsoft\Windows NT\CurrentVersion" /v BuildBranch') do set branch=%%i
	reg unload HKLM\TEMP >nul
)
if defined _label2 (set _label=%_label2%) else (set _label=%version%.%datetime%.%branch%_CLIENT)
for %%b in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do set _label=!_label:%%b=%%b!

for %%b in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do set langid=!langid:%%b=%%b!

if /i %arch%==x86 set archl=X86
if /i %arch%==x64 set archl=X64
if /i %arch%==arm64 set archl=ARM64

if /i %uupa%==2 set DVDLABEL=CCSA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%COMBINED_UUP_%archl%FRE_%langid%.ISO&exit /b

if /i %editionid%==Starter set DVDLABEL=CSA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%STARTER_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==StarterN set DVDLABEL=CSNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%STARTERN_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==Cloud set DVDLABEL=CWCA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CLOUD_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==CloudN set DVDLABEL=CWCNNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CLOUDN_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==CloudE set DVDLABEL=CWCA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CLOUDE_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==CloudEN set DVDLABEL=CWCNNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CLOUDEN_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==CoreCountrySpecific set DVDLABEL=CCHA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CHINA_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==CoreSingleLanguage set DVDLABEL=CSLA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%SINGLELANGUAGE_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==Core set DVDLABEL=CCRA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%CORE_OEMRET_%archl%FRE_%langid%.ISO
if /i %editionid%==CoreN set DVDLABEL=CCRNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%COREN_OEMRET_%archl%FRE_%langid%.ISO
if /i %editionid%==ProfessionalSingleLanguage set DVDLABEL=CPRSLA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROSINGLELANGUAGE_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==ProfessionalCountrySpecific set DVDLABEL=CPRCHA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROCHINA_OEM_%archl%FRE_%langid%.ISO
if /i %editionid%==Professional (IF %VOL%==1 (set DVDLABEL=CPRA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALVL_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPRA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PRO_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==ProfessionalN (IF %VOL%==1 (set DVDLABEL=CPRNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALNVL_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPRNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PRON_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==ProfessionalEducation (IF %VOL%==1 (set DVDLABEL=CPREA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALEDUCATION_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPREA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROEDUCATION_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==ProfessionalEducationN (IF %VOL%==1 (set DVDLABEL=CPRENA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALEDUCATIONN_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPRENA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROEDUCATIONN_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==ProfessionalWorkstation (IF %VOL%==1 (set DVDLABEL=CPRWA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALWORKSTATION_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPRWA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROWORKSTATION_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==ProfessionalWorkstationN (IF %VOL%==1 (set DVDLABEL=CPRWNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%PROFESSIONALWORKSTATIONN_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CPRWNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PROWORKSTATIONN_OEMRET_%archl%FRE_%langid%.ISO))
if /i %editionid%==Education (IF %VOL%==1 (set DVDLABEL=CEDA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%EDUCATION_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CEDA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%EDUCATION_RET_%archl%FRE_%langid%.ISO))
if /i %editionid%==EducationN (IF %VOL%==1 (set DVDLABEL=CEDNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%EDUCATIONN_VOL_%archl%FRE_%langid%.ISO) else (set DVDLABEL=CEDNA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%EDUCATIONN_RET_%archl%FRE_%langid%.ISO))
if /i %editionid%==Enterprise set DVDLABEL=CENA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISE_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseN set DVDLABEL=CENNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISEN_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseG set DVDLABEL=CEGA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISEG_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseGN set DVDLABEL=CEGNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISEGN_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseEval set DVDLABEL=CEEA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISEEVAL_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseNEval set DVDLABEL=CEENA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISENEVAL_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseS set DVDLABEL=CESA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISES_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseSN set DVDLABEL=CESNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISESN_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseSubscription set DVDLABEL=CESA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISESUBSCRIPTION_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseSubscriptionN set DVDLABEL=CESNA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISESUBSCRIPTIONN_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseSEval set DVDLABEL=CESEA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISESEVAL_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==EnterpriseSNEval set DVDLABEL=CESNEA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISESNEVAL_VOL_%archl%FRE_%langid%.ISO
if /i %editionid%==PPIPro set DVDLABEL=CPPIA_%archl%FRE_%langid%_DV5&set DVDISO=%_label%PPIPRO_OEM_%archl%FRE_%langid%.ISO
if defined branch exit /b

:Boot
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 1 "Microsoft Windows PE (%arch%)" "Microsoft Windows PE (%arch%)" 1>nul 2>nul
"%wimlib%" update %ISOFOLDER%\sources\boot.wim 1 --command="delete '\Windows\system32\winpeshl.ini'" 1>nul 2>nul
md .\temp\mount
%dism% /Quiet /Mount-Wim /Wimfile:%ISOFOLDER%\sources\boot.wim /Index:1 /MountDir:"%~dp0temp\mount"
%dism% /Quiet /Image:"%~dp0temp\mount" /Set-TargetPath:X:\$windows.~bt\
%dism% /Quiet /Unmount-Wim /MountDir:"%~dp0temp\mount" /Commit
rmdir /s /q .\temp\mount
"%wimlib%" extract %ISOFOLDER%\sources\install.wim 1 Windows\system32\xmllite.dll --dest-dir=%ISOFOLDER%\sources --no-acls >nul 2>&1
echo delete '^\Windows^\system32^\winpeshl.ini'>%bootedit%
echo add '%ISOFOLDER%^\setup.exe' '^\setup.exe'>>%bootedit%
echo add '%ISOFOLDER%^\sources^\inf^\setup.cfg' '^\sources^\inf^\setup.cfg'>>%bootedit%
echo add '%ISOFOLDER%^\sources^\background_cli.bmp' '^\sources^\background.bmp'>>%bootedit%
for %%A in (
	alert.gif
	api-ms-win-core-apiquery-l1-1-0.dll
	api-ms-win-downlevel-advapi32-l1-1-0.dll
	api-ms-win-downlevel-advapi32-l1-1-1.dll
	api-ms-win-downlevel-advapi32-l2-1-0.dll
	api-ms-win-downlevel-advapi32-l2-1-1.dll
	api-ms-win-downlevel-advapi32-l3-1-0.dll
	api-ms-win-downlevel-advapi32-l4-1-0.dll
	api-ms-win-downlevel-kernel32-l1-1-0.dll
	api-ms-win-downlevel-kernel32-l2-1-0.dll
	api-ms-win-downlevel-ole32-l1-1-0.dll
	api-ms-win-downlevel-ole32-l1-1-1.dll
	api-ms-win-downlevel-shlwapi-l1-1-0.dll
	api-ms-win-downlevel-shlwapi-l1-1-1.dll
	api-ms-win-downlevel-user32-l1-1-0.dll
	api-ms-win-downlevel-user32-l1-1-1.dll
	api-ms-win-downlevel-version-l1-1-0.dll
	appcompat.xsl
	appcompat_bidi.xsl
	appcompat_detailed_bidi_txt.xsl
	appcompat_detailed_txt.xsl
	appraiser.dll
	ARUNIMG.dll
	arunres.dll
	autorun.dll
	cmisetup.dll
	compatctrl.dll
	compatprovider.dll
	cryptosetup.dll
	diager.dll
	diagnostic.dll
	diagtrack.dll
	diagtrackrunner.exe
	dism.exe
	dismapi.dll
	dismcore.dll
	dismcoreps.dll
	dismprov.dll
	ext-ms-win-advapi32-encryptedfile-l1-1-0.dll
	folderprovider.dll
	hwcompat.dll
	hwcompat.txt
	hwexclude.txt
	idwbinfo.txt
	imagingprovider.dll
	input.dll
	lang.ini
	locale.nls
	logprovider.dll
	MediaSetupUIMgr.dll
	ndiscompl.dll
	nlsbres.dll
	ntdsupg.dll
	offline.xml
	pnpibs.dll
	reagent.admx
	reagent.dll
	reagent.xml
	rollback.exe
	schema.dat
	segoeui.ttf
	setup.exe
	setupcompat.dll
	SetupCore.dll
	SetupHost.exe
	SetupMgr.dll
	SetupPlatform.cfg
	SetupPlatform.dll
	SetupPlatform.exe
	SetupPrep.exe
	SmiEngine.dll
	spflvrnt.dll
	spprgrss.dll
	spwizeng.dll
	spwizimg.dll
	spwizres.dll
	sqmapi.dll
	testplugin.dll
	unattend.dll
	unbcl.dll
	upgloader.dll
	upgrade_frmwrk.xml
	uxlib.dll
	uxlibres.dll
	vhdprovider.dll
	w32uiimg.dll
	w32uires.dll
	warning.gif
	wdsclient.dll
	wdsclientapi.dll
	wdscore.dll
	wdscsl.dll
	wdsimage.dll
	wdstptc.dll
	wdsutil.dll
	wimprovider.dll
	win32ui.dll
	WinDlp.dll
	winsetup.dll
	wpx.dll
	xmllite.dll
) do (
	if exist "%ISOFOLDER%\sources\%%A" echo add '%ISOFOLDER%^\sources^\%%A' '^\sources^\%%A'>>%bootedit%
)
	for %%A in (
	appraiser.dll.mui
	arunres.dll.mui
	cmisetup.dll.mui
	compatctrl.dll.mui
	compatprovider.dll.mui
	dism.exe.mui
	dismapi.dll.mui
	dismcore.dll.mui
	dismprov.dll.mui
	folderprovider.dll.mui
	imagingprovider.dll.mui
	input.dll.mui
	logprovider.dll.mui
	MediaSetupUIMgr.dll.mui
	nlsbres.dll.mui
	pnpibs.dll.mui
	reagent.adml
	reagent.dll.mui
	rollback.exe.mui
	setup.exe.mui
	setup_help_upgrade_or_custom.rtf
	setupcompat.dll.mui
	SetupCore.dll.mui
	setupplatform.exe.mui
	SetupPrep.exe.mui
	smiengine.dll.mui
	spwizres.dll.mui
	upgloader.dll.mui
	uxlibres.dll.mui
	vhdprovider.dll.mui
	vofflps.rtf
	w32uires.dll.mui
	wdsclient.dll.mui
	wdsimage.dll.mui
	wimprovider.dll.mui
	WinDlp.dll.mui
	winsetup.dll.mui
) do (
	if exist "%ISOFOLDER%\sources\%langid%\%%A" echo add '%ISOFOLDER%^\sources^\%langid%^\%%A' '^\sources^\%langid%^\%%A'>>%bootedit%
)
"%wimlib%" export "%MetadataESD%" 2 %ISOFOLDER%\sources\boot.wim "Microsoft Windows Setup (%arch%)" "Microsoft Windows Setup (%arch%)" --boot
"%wimlib%" update %ISOFOLDER%\sources\boot.wim 2 < %bootedit% 1>nul 2>nul
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 2 --image-property FLAGS=2 1>nul 2>nul
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 1 --image-property LASTMODIFICATIONTIME/HIGHPART=%installhigh% --image-property LASTMODIFICATIONTIME/LOWPART=%installlow% 1>nul 2>nul
"%wimlib%" info %ISOFOLDER%\sources\boot.wim 2 --image-property LASTMODIFICATIONTIME/HIGHPART=%installhigh% --image-property LASTMODIFICATIONTIME/LOWPART=%installlow% 1>nul 2>nul
"%wimlib%" optimize %ISOFOLDER%\sources\boot.wim
del %bootedit% >nul 2>&1
del %ISOFOLDER%\sources\xmllite.dll >nul 2>&1
goto :eof

:convert
if exist "%UUP%\*.cab" (
	echo.
	call :as
	echo %txt11% . . .
	call :as
	echo.
	if exist "%UUP%\*.xml.cab" if exist "%UUP%\Metadata\*" move /y "%UUP%\*.xml.cab" "%UUP%\Metadata\" 1>nul 2>nul
	for /f "delims=" %%i in ('dir /b "%UUP%\*.cab"') do call :uups_cab %%i
	for /f "delims=" %%i in ('dir /AD /b "%UUP%\"') do call :uups_fol %%i
	if exist "%UUP%\Metadata\*.xml.cab" copy /y "%UUP%\Metadata\*.xml.cab" "%UUP%\" 1>nul 2>nul
)
goto :eof

:multi_edition_name_check
if /i "%1" equ "Cloud" set "desc=S (Cloud)"
if /i "%1" equ "CloudN" set "desc=S N (Cloud N)"
if /i "%1" equ "Clode" set "desc=Lean"
if /i "%1" equ "CoreSingleLanguage" set "desc=Home Single Language"
if /i "%1" equ "CoreCountrySpecific" set "desc=Home China"
if /i "%1" equ "Core" set "desc=Home"
if /i "%1" equ "CoreN" set "desc=Home N"
if /i "%1" equ "Professional" set "desc=Pro"
if /i "%1" equ "ProfessionalN" set "desc=Pro N"
if /i "%1" equ "ProfessionalEducation" set "desc=Pro Education"
if /i "%1" equ "ProfessionalEducationN" set "desc=Pro Education N"
if /i "%1" equ "ProfessionalWorkstation" set "desc=Pro for Workstations"
if /i "%1" equ "ProfessionalWorkstationN" set "desc=Pro N for Workstations"
if /i "%1" equ "ProfessionalSingleLanguage" set "desc=Pro Single Language"
if /i "%1" equ "ProfessionalCountrySpecific" set "desc=Pro China Only"
if /i "%1" equ "PPIPro" set "desc=Team"
if /i "%1" equ "Education" set "desc=Education"
if /i "%1" equ "EducationN" set "desc=Education N"
if /i "%1" equ "Enterprise" set "desc=Enterprise"
if /i "%1" equ "EnterpriseN" set "desc=Enterprise N"
if /i "%1" equ "EnterpriseEval" set "desc=Enterprise Evaluation"
if /i "%1" equ "EnterpriseNEval" set "desc=Enterprise N Evaluation"
if /i "%1" equ "EnterpriseG" set "desc=Enterprise G"
if /i "%1" equ "EnterpriseGN" set "desc=Enterprise G N"
if /i "%1" equ "EnterpriseS" set "desc=Enterprise S"
if /i "%1" equ "EnterpriseSN" set "desc=Enterprise S N"
if /i "%1" equ "EnterpriseSEval" set "desc=Enterprise S Evaluation"
if /i "%1" equ "EnterpriseSNEval" set "desc=Enterprise S N Evaluation"
if /i "%1" equ "ServerRdsh" set "desc=Enterprise for Remote Sessions"
if /i "%1" equ "Starter" set "desc=Starter"
if /i "%1" equ "StarterN" set "desc=Starter N"
exit /b

:install
if /i %WIMFILE%==install.wim (
if exist "temp\*.ESD" (
		"%wimlib%" export "%MetadataESD%" 3 %2%WIMFILE% --ref="%UUP%\*.esd" --ref="temp\*.esd" --compress=maximum
	) else (
		"%wimlib%" export "%MetadataESD%" 3 %2%WIMFILE% --ref="%UUP%\*.esd" --compress=maximum
	)
)
if /i %WIMFILE%==install.esd (
	if exist "temp\*.ESD" (
		"%wimlib%" export "%MetadataESD%" 3 %2%WIMFILE% --ref="%UUP%\*.esd" --ref="temp\*.esd"
	) else (
		"%wimlib%" export "%MetadataESD%" 3 %2%WIMFILE% --ref="%UUP%\*.esd"
	)
)
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %txt15%&PAUSE&GOTO :QUIT)
call :multi_edition_name_check !edition%1!
"%wimlib%" info %2%WIMFILE% %1 "Windows 10 !desc!" "Windows 10 !desc!" --image-property DISPLAYNAME="Windows 10 !desc!" --image-property DISPLAYDESCRIPTION="Windows 10 !desc!" 1>nul 2>nul
goto :eof

:fix11
del /F /S /Q "%ISOFOLDER%\sources\appraiserres.dll" 1>nul 2>nul
if not exist "temp\boot\*" mkdir "temp\boot"
set "tt1=%random%"
bin\wimlib-imagex extract %ISOFOLDER%\sources\boot.wim 2 \Windows\System32\config\SYSTEM --dest-dir=temp\boot --no-acls --no-attributes 1>nul 2>nul
reg load HKLM\!tt1!_wSYSTEM "temp\boot\SYSTEM" 1>nul 2>nul
reg add "HKLM\!tt1!_wSYSTEM\Setup\LabConfig" /v "BypassTPMCheck" /t REG_DWORD /d "1" /f 1>nul 2>nul
reg add "HKLM\!tt1!_wSYSTEM\Setup\LabConfig" /v "BypassSecureBootCheck" /t REG_DWORD /d "1" /f 1>nul 2>nul
reg add "HKLM\!tt1!_wSYSTEM\Setup\LabConfig" /v "BypassRAMCheck" /t REG_DWORD /d "1" /f 1>nul 2>nul
reg unload HKLM\!tt1!_wSYSTEM 1>nul 2>nul
bin\wimlib-imagex update %ISOFOLDER%\sources\boot.wim 2 --command "add 'temp\boot\SYSTEM' '\Windows\System32\config\SYSTEM'" 1>nul 2>nul
if exist "temp\boot\*" rmdir /s /q "temp\boot"
exit /b

:add_winre
for /f "tokens=3 delims=<>" %%i in ('bin\imagex.exe /info "%2%WIMFILE%" %1 ^| findstr /i HIGHPART') do set "installhigh=%%i"
for /f "tokens=3 delims=<>" %%i in ('bin\imagex.exe /info "%2%WIMFILE%" %1 ^| findstr /i LOWPART') do set "installlow=%%i"
"%wimlib%" update %2%WIMFILE% %1 --command="add 'temp\winre.wim' '\windows\system32\recovery\winre.wim'" 1>nul 2>nul
"%wimlib%" info %2%WIMFILE% %1 --image-property LASTMODIFICATIONTIME/HIGHPART=%installhigh% --image-property LASTMODIFICATIONTIME/LOWPART=%installlow% 1>nul 2>nul
goto :eof	

:uups_cab
set pack=%~n1
if exist "%CD%\temp\%pack%.ESD" goto :eof
md temp\%pack%
echo CAB ^> DIR ^> ESD ^(%pack%.cab^)
expand.exe "%UUP%\%pack%.cab" -f:* temp\%pack%\ >nul
"%wimlib%" capture "temp\%pack%" "temp\%pack%.ESD" --compress=LZX:15 --check --no-acls --norpfix "%pack%" "%pack%" >nul
rd /s /q temp\%pack%
goto :eof

:uups_fol
set pack=%1
if /i [%1]==[Metadata] goto :eof
if exist "%CD%\temp\%pack%.ESD" goto :eof
echo DIR ^> ESD ^(%pack%^)
"%wimlib%" capture "%UUP%\%pack%" "temp\%pack%.ESD" --compress=LZX:15 --check --no-acls --norpfix "%pack%" "%pack%" >nul
goto :eof

:uups_esd
for /f "usebackq  delims=" %%b in (`find /n /v "" temp\uups_esd.txt ^| find "[%1]"`) do set uups_esd=%%b
if %1 GEQ 1 set uups_esd=%uups_esd:~3%
if %1 GEQ 10 set uups_esd=%uups_esd:~1%
if %1 GEQ 100 set uups_esd=%uups_esd:~1%
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| findstr /b "Edition"') do set "edition%1=%%i"
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| find /i "Default"') do set "langid%1=%%i"
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| find /i "Architecture"') do set "arch%1=%%i"
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| findstr /b "Build"') do set build%1=%%i
for /f "tokens=4 delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| find /i "Service Pack Build"') do set svcbuild%1=%%i
if /i !arch%1!==x86_64 set "arch%1=x64"
if /i !arch%1!==ARM64 set "arch%1=arm64"
for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex info "%UUP%\%uups_esd%" 3 ^| findstr /b "Name"') do set "name=%%j"
set "name%1=!name! - !build%1!.!svcbuild%1! / !arch%1! / !langid%1!"
set "uups_esd%1=%uups_esd%"
goto :eof

:setdate
if /i %1==Jan set "isotime=01/%isotime%"
if /i %1==Feb set "isotime=02/%isotime%"
if /i %1==Mar set "isotime=03/%isotime%"
if /i %1==Apr set "isotime=04/%isotime%"
if /i %1==May set "isotime=05/%isotime%"
if /i %1==Jun set "isotime=06/%isotime%"
if /i %1==Jul set "isotime=07/%isotime%"
if /i %1==Aug set "isotime=08/%isotime%"
if /i %1==Sep set "isotime=09/%isotime%"
if /i %1==Oct set "isotime=10/%isotime%"
if /i %1==Nov set "isotime=11/%isotime%"
if /i %1==Dec set "isotime=12/%isotime%"
exit /b

:setmmm
if /i %1==Jan set "datetime=%yyy:~2%01%ddd%"
if /i %1==Feb set "datetime=%yyy:~2%02%ddd%"
if /i %1==Mar set "datetime=%yyy:~2%03%ddd%"
if /i %1==Apr set "datetime=%yyy:~2%04%ddd%"
if /i %1==May set "datetime=%yyy:~2%05%ddd%"
if /i %1==Jun set "datetime=%yyy:~2%06%ddd%"
if /i %1==Jul set "datetime=%yyy:~2%07%ddd%"
if /i %1==Aug set "datetime=%yyy:~2%08%ddd%"
if /i %1==Sep set "datetime=%yyy:~2%09%ddd%"
if /i %1==Oct set "datetime=%yyy:~2%10%ddd%"
if /i %1==Nov set "datetime=%yyy:~2%11%ddd%"
if /i %1==Dec set "datetime=%yyy:~2%12%ddd%"
exit /b

:setmmm1
if /i %1==Jan set "datetime%2=%ddd%-01-%yyy% %timeout%"
if /i %1==Feb set "datetime%2=%ddd%-02-%yyy% %timeout%"
if /i %1==Mar set "datetime%2=%ddd%-03-%yyy% %timeout%"
if /i %1==Apr set "datetime%2=%ddd%-04-%yyy% %timeout%"
if /i %1==May set "datetime%2=%ddd%-05-%yyy% %timeout%"
if /i %1==Jun set "datetime%2=%ddd%-06-%yyy% %timeout%"
if /i %1==Jul set "datetime%2=%ddd%-07-%yyy% %timeout%"
if /i %1==Aug set "datetime%2=%ddd%-08-%yyy% %timeout%"
if /i %1==Sep set "datetime%2=%ddd%-09-%yyy% %timeout%"
if /i %1==Oct set "datetime%2=%ddd%-10-%yyy% %timeout%"
if /i %1==Nov set "datetime%2=%ddd%-11-%yyy% %timeout%"
if /i %1==Dec set "datetime%2=%ddd%-12-%yyy% %timeout%"
exit /b

:dCheck
set combine=0
call :uup_esd %fold%\amd64
set _amd64=%uups_esd_num%
call :uup_esd %fold%\x86
set _x86=%uups_esd_num%
if /i %_amd64% neq 0 (if /i %_x86% neq 0 (goto :DUALMENU))
cls
color 0c
echo 
echo %txt29%
echo %txt30%
echo 
pause>nul
color 0a
goto :uup_fold

:DUALMENU
set userint=
cls
echo.
call :as
echo   %youcreatdu%
call :as
echo.
echo  %menudu1%
echo  %menudu2%
echo  %menudu3%
echo.
echo  %menudu4%
call :as _
echo.
set /p userint= ^> %denter% 
set userint=%userint:~0,1%
if %userint%==4 goto :uup_fold
if %userint%==3 (set WIMFILE=install.wim&set combine=1&goto :dGo)
if %userint%==2 (set WIMFILE=install.wim&goto :dGo)
if %userint%==1 (set WIMFILE=install.esd&goto :dGo)
GOTO :DUALMENU

:dGo
cls
call :dPREPARE 1 amd64 x64
call :dPREPARE 2 x86 x86

IF NOT EXIST bin\temp\ mkdir bin\temp
bin\7z.exe l .\ISOFOLDER\x64\sources\SetupPlatform.dll >.\bin\temp\version.txt 2>&1
IF %ERRORLEVEL% NEQ 0 (
	echo.
	call :as
	echo %errsetup%
	call :as
	echo.
	echo %presskey%
	pause >nul
	exit
)
for /f "tokens=4,5 delims=. " %%i in ('"findstr /B "FileVersion" .\bin\temp\version.txt" 2^>nul') do set version=%%i.%%j
for /f "tokens=7 delims=.) " %%i in ('"findstr /B "FileVersion" .\bin\temp\version.txt" 2^>nul') do set datetime=%%i
for /f "tokens=6 delims=.( " %%i in ('"findstr /B "FileVersion" .\bin\temp\version.txt" 2^>nul') do set branch=%%i
"%wimlib%" extract "ISOFOLDER\x64\sources\%WIMFILE%" 1 Windows\WinSxS\Manifests\*_microsoft-windows-coreos-revision* --dest-dir=.\bin\temp --no-acls >nul 2>&1
for /f "tokens=6,7 delims=_." %%i in ('dir /b /o:d bin\temp\*.manifest') do set revision=%%i.%%j
if %version% neq %revision% (
set version=%revision%
for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex.exe info "ISOFOLDER\x64\sources\%WIMFILE%" 1 ^| find /i "Last Modification Time"') do (set mmm=%%G&set yyy=%%L&set ddd=%%H-%%I%%J)
	call :setmmm !mmm!
)
call bin\revision.cmd
set _label2=
if /i %branch%==WinBuild (
	"%wimlib%" extract "ISOFOLDER\x64\sources\%WIMFILE%" 1 \Windows\System32\config\SOFTWARE --dest-dir=.\bin\temp --no-acls >nul
	reg load HKLM\TEMP .\bin\temp\SOFTWARE >nul
	for /f "skip=2 tokens=3,4,5,6,7 delims=. " %%i in ('"reg query "HKLM\TEMP\Microsoft\Windows NT\CurrentVersion" /v BuildLabEx" 2^>nul') do if not errorlevel 1 set _label2=%%i.%%j.%%m.%%l_CLIENT
	for /f "skip=2 tokens=3 delims= " %%i in ('reg query "HKLM\TEMP\Microsoft\Windows NT\CurrentVersion" /v BuildBranch') do set branch=%%i
	reg unload HKLM\TEMP >nul
)
if defined _label2 (set _label=%_label2%) else (set _label=%version%.%datetime%.%branch%_CLIENT)
for %%b in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do set _label=!_label:%%b=%%b!

if /i %UUPlang1%==%UUPlang2% (set langid=%UUPlang1%) else (set langid=%UUPlang1:~0,2%-%UUPlang2:~0,2%)
for %%b in (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z) do set langid=!langid:%%b=%%b!

set archl=X86-X64

if /i %DVDLABEL1% equ %DVDLABEL2% (
	set DVDLABEL=%DVDLABEL1%_%archl%FRE_%langid%_DV9
	set DVDISO=%_label%%DVDISO1%_%archl%FRE_%langid%.ISO
) else (
	set DVDLABEL=CCSA_%archl%FRE_%langid%_DV9
	set DVDISO=%_label%%DVDISO1%_%UUParch1%FRE-%DVDISO2%_%UUParch2%FRE_%langid%.ISO
)

if %combine%==0 goto :BCD
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x86\sources\install.wim ^| findstr /c:"Image Count"') do set imagesi=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x64\sources\install.wim ^| findstr /c:"Image Count"') do set imagesx=%%i
for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x86\sources\install.wim 1 ^| findstr /b "Name"') do set "_osi=%%j x86"
for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x64\sources\install.wim 1 ^| findstr /b "Name"') do set "_osx=%%j x64"
IF NOT %imagesi%==1 FOR /L %%g IN (2,1,%imagesi%) DO (
	for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x86\sources\install.wim %%g ^| findstr /b "Name"') do set "_osi%%g=%%j x86"
)
IF NOT %imagesx%==1 FOR /L %%g IN (2,1,%imagesx%) DO (
	for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex.exe info ISOFOLDER\x64\sources\install.wim %%g ^| findstr /b "Name"') do set "_osx%%g=%%j x64"
)
echo.
call :as
echo %iunif% install.wim...
call :as
echo.
echo %ix86d%
"%wimlib%" info ISOFOLDER\x86\sources\install.wim 1 "%_osi%" "%_osi%"
IF NOT %imagesi%==1 FOR /L %%g IN (2,1,%imagesi%) DO (
	"%wimlib%" info ISOFOLDER\x86\sources\install.wim %%g "!_osi%%g!" "!_osi%%g!"
)
echo.
echo %ix64d%
"%wimlib%" export ISOFOLDER\x64\sources\install.wim 1 ISOFOLDER\x86\sources\install.wim "%_osx%" "%_osx%"
IF NOT %imagesx%==1 FOR /L %%g IN (2,1,%imagesx%) DO (
	"%wimlib%" export ISOFOLDER\x64\sources\install.wim %%g ISOFOLDER\x86\sources\install.wim "!_osx%%g!" "!_osx%%g!"
)
echo.
echo Replacing \x86\sources\install.wim -^> \x64\sources\install.wim
del ISOFOLDER\x64\sources\install.wim >nul 2>&1
copy /y ISOFOLDER\x86\sources\install.wim ISOFOLDER\x64\sources\install.wim

:BCD
echo.
call :as
echo %ibcd%...
call :as
echo.
call bin\bcd.cmd
call :as
echo %creat% ISO...
call :as
for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex.exe info ISOFOLDER\x64\sources\%WIMFILE% 1 ^| find /i "Last Modification Time"') do (set mmm=%%G&set "isotime=%%H/%%L,%%I:%%J:%%K")
call :setdate %mmm%
bin\cdimage.exe -bootdata:3#p0,e,b"ISOFOLDER\boot\etfsboot.com"#pEF,e,b"bin\efi\efisys.bin"#pEF,e,b"ISOFOLDER\efi\Microsoft\boot\efisys.bin" -o -m -u2 -udfver102 -t%isotime% -g -l%DVDLABEL% ISOFOLDER %DVDISO%
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (
	echo.
	echo %txt16%
	echo.
	echo %exitd%
	pause >nul
	exit
)
rmdir /s /q ISOFOLDER\ 1>nul 2>nul
rmdir /s /q bin\temp\ 1>nul 2>nul
rmdir /s /q temp\ 1>nul 2>nul
echo.
echo %exitd%
pause >nul
GOTO :QUIT

:dPREPARE
set "UUP=%fold%\%2"
call :uup_esd %UUP%
for /L %%i in (1, 1, %uups_esd_num%) do (
	call :uups_esd %%i
)
set "uupa=2"&set "ISOFOLDER=ISOFOLDER\%3"&set dual=1&call :AIO
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info "ISOFOLDER\%3\sources\%WIMFILE%" ^| findstr /c:"Image Count"') do set UUPmulti%1=%%i
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex.exe info "ISOFOLDER\%3\sources\%WIMFILE%" 1 ^| find /i "Architecture"') do set "UUParch%1=%%i"
for /f "tokens=3 delims=<>" %%i in ('bin\imagex.exe /info "ISOFOLDER\%3\sources\%WIMFILE%" 1 ^| find /i "<DEFAULT>"') do set UUPlang%1=%%i
if /i !UUParch%1!==x86_64 (set "UUParch%1=X64") else (set "UUParch%1=X86")

IF !UUPmulti%1! GEQ 2 (
	bin\wimlib-imagex.exe info "ISOFOLDER\%3\sources\%WIMFILE%" | findstr /b "Edition" | find "N" 1>nul && set UUPeditionn%1=1
	bin\wimlib-imagex.exe info "ISOFOLDER\%3\sources\%WIMFILE%" | findstr /b "Edition" | find "o" 1>nul && set UUPeditiona%1=1
	bin\wimlib-imagex.exe info "ISOFOLDER\%3\sources\%WIMFILE%" | findstr /b "Edition" | find "e" 1>nul && set UUPeditiona%1=1
	if !UUPeditionn%1!==1 set DVDLABEL%1=CCSNA&set DVDISO%1=COMBINEDN_UUP_OEMRET
	if !UUPeditiona%1!==1 set DVDLABEL%1=CCSA&set DVDISO%1=COMBINED_UUP_OEMRET
	if !UUPeditiona%1!==1 (if !UUPeditionn%1!==1 (set DVDLABEL%1=CCSNA&set DVDISO%1=COMBINED_N_UUP_OEMRET))
	exit /b
)

for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex info "ISOFOLDER\%3\sources\%WIMFILE%" 1 ^| findstr /b "Edition"') do set "UUPedition%1=%%i"
if /i !UUPedition%1!==Core set DVDLABEL%1=CCRA&set DVDISO%1=CORE_OEMRET
if /i !UUPedition%1!==CoreN set DVDLABEL%1=CCRNA&set DVDISO%1=COREN_OEMRET
if /i !UUPedition%1!==CoreSingleLanguage set DVDLABEL%1=CSLA&set DVDISO%1=SINGLELANGUAGE_OEM
if /i !UUPedition%1!==CoreCountrySpecific set DVDLABEL%1=CCHA&set DVDISO%1=CHINA_OEM
if /i !UUPedition%1!==Professional (IF !ESDvol%1!==1 (set DVDLABEL%1=CPRA&set DVDISO%1=PROFESSIONALVL_VOL) else (set DVDLABEL%1=CPRA&set DVDISO%1=PRO_OEMRET))
if /i !UUPedition%1!==ProfessionalN (IF !ESDvol%1!==1 (set DVDLABEL%1=CPRNA&set DVDISO%1=PROFESSIONALNVL_VOL) else (set DVDLABEL%1=CPRNA&set DVDISO%1=PRON_OEMRET))
if /i !UUPedition%1!==Education (IF !ESDvol%1!==1 (set DVDLABEL%1=CEDA&set DVDISO%1=EDUCATION_VOL) else (set DVDLABEL%1=CEDA&set DVDISO%1=EDUCATION_RET))
if /i !UUPedition%1!==EducationN (IF !ESDvol%1!==1 (set DVDLABEL%1=CEDNA&set DVDISO%1=EDUCATIONN_VOL) else (set DVDLABEL%1=CEDNA&set DVDISO%1=EDUCATIONN_RET))
if /i !UUPedition%1!==Enterprise set DVDLABEL%1=CENA&set DVDISO%1=ENTERPRISE_VOL
if /i !UUPedition%1!==EnterpriseN set DVDLABEL%1=CENNA&set DVDISO%1=ENTERPRISEN_VOL
if /i !UUPedition%1!==PPIPro set DVDLABEL%1=CPPIA&set DVDISO%1=PPIPRO_OEM
if /i !UUPedition%1!==Cloud set DVDLABEL%1=CWCA&set DVDISO%1=CLOUD_OEM
if /i !UUPedition%1!==CloudN set DVDLABEL%1=CWCNNA&set DVDISO%1=CLOUDN_OEM
if /i !UUPedition%1!==EnterpriseG set DVDLABEL%1=CEGA&set DVDISO%1=ENTERPRISEG_VOL
if /i !UUPedition%1!==EnterpriseGN set DVDLABEL%1=CEGNA&set DVDISO%1=ENTERPRISEGN_VOL
if /i !UUPedition%1!==EnterpriseS set DVDLABEL%1=CES&set DVDISO%1=ENTERPRISES_VOL
if /i !UUPedition%1!==EnterpriseSN set DVDLABEL%1=CESNN&set DVDISO%1=ENTERPRISESN_VOL
if /i !UUPedition%1!==ProfessionalEducation (IF %VOL%==1 (set DVDLABEL%1=CPREA&set DVDISO%1=PROFESSIONALEDUCATION_VOL) else (set DVDLABEL%1=CPREA&set DVDISO%1=PROEDUCATION_OEMRET))
if /i !UUPedition%1!==ProfessionalEducationN (IF %VOL%==1 (set DVDLABEL%1=CPRENA&set DVDISO%1=PROFESSIONALEDUCATIONN_VOL) else (set DVDLABEL%1=CPRENA&set DVDISO%1=PROEDUCATIONN_OEMRET))
if /i !UUPedition%1!==ProfessionalWorkstation (IF %VOL%==1 (set DVDLABEL%1=CPRWA&set DVDISO%1=PROFESSIONALWORKSTATION_VOL) else (set DVDLABEL%1=CPRWA&set DVDISO%1=PROWORKSTATION_OEMRET))
if /i !UUPedition%1!==ProfessionalWorkstationN (IF %VOL%==1 (set DVDLABEL%1=CPRWNA&set DVDISO%1=PROFESSIONALWORKSTATIONN_VOL) else (set DVDLABEL%1=CPRWNA&set DVDISO%1=PROWORKSTATIONN_OEMRET))
if /i !UUPedition%1!==ProfessionalSingleLanguage set DVDLABEL%1=CPRSLA&set DVDISO%1=PROSINGLELANGUAGE_OEM
if /i !UUPedition%1!==ProfessionalCountrySpecific set DVDLABEL%1=CPRCHA&set DVDISO%1=PROCHINA_OEM
exit /b

:dlang
if /i %dlang%==ru-RU call bin\lang-uup.cmd -ru && exit /b
call bin\lang-uup.cmd -en
exit /b

:as
if [%1]==[_] (echo ____________________________________________________________________________) else (echo ============================================================================)
exit /b

:QUIT
IF EXIST ISOFOLDER\ rmdir /s /q ISOFOLDER\
IF EXIST bin\temp\ rmdir /s /q bin\temp\
IF EXIST temp\ rmdir /s /q temp\
exit