@echo off
rem script:     abbodi1406, adguard
rem wimlib:     synchronicity

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 )

set "vers=v170625"
title BaseImage ^> ISO - %vers%
echo Preparing...
if not exist "%~dp0bin\wimlib-imagex.exe" goto :eof
IF /I "%PROCESSOR_ARCHITECTURE%" EQU "AMD64" (SET "wimlib=%~dp0bin\bin64\wimlib-imagex.exe") ELSE (SET "wimlib=%~dp0bin\wimlib-imagex.exe")
cd /d "%~dp0"
setlocal EnableDelayedExpansion
color 1f
SET Backup=OFF
SET ENCRYPTEDESD=
SET ERRORTEMP=
SET ENCRYPTED=0
SET PREPARED=0
SET VOL=0
for /f "tokens=3 delims=: " %%b in ('dism /english /online /Get-Intl ^| find /i "System locale"') do set dlang=%%b && call :dlang

if not [%1]==[] (set "ENCRYPTEDESD=%~1"&set "ENCRYPTEDESDN=%~nx1"&goto :check)
set _esd=0
if exist "BaseImage-*.wim" (for /f "delims=" %%i in ('dir /b "BaseImage-*.wim"') do (call set /a _esd+=1))
if !_esd! equ 0 goto :prompt1
if !_esd! gtr 1 goto :prompt2
for /f "delims=" %%i in ('dir /b "BaseImage-*.wim"') do (set "ENCRYPTEDESD=%%i"&set "ENCRYPTEDESDN=%%i"&goto :check)

:prompt1
cls
echo.
echo ===============================================================================
echo %prompt1%
echo ===============================================================================
echo.
set /p ENCRYPTEDESD=
if [%ENCRYPTEDESD%]==[] goto :QUIT
call :setvar "%ENCRYPTEDESD%"
goto :check

:setvar
SET ENCRYPTEDESDN=%~nx1
goto :eof

:prompt2
cls
echo.
echo ===============================================================================
echo %prompt2%
echo ===============================================================================
echo.
set /p ENCRYPTEDESD=
if [%ENCRYPTEDESD%]==[] goto :QUIT
SET ENCRYPTEDESDN=%ENCRYPTEDESD%
goto :check

:check
SET ENCRYPTED=0
if /i %ENCRYPTEDESDN%==install.wim (ren %ENCRYPTEDESD% %ENCRYPTEDESDN%.orig&set ENCRYPTEDESD=%ENCRYPTEDESD%.orig)
bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 >nul 2>&1
IF %ERRORLEVEL% EQU 74 GOTO :MAINMENU
IF %ERRORLEVEL% EQU 18 (
cls
echo.
echo ===============================================================================
echo %check%
echo ===============================================================================
echo.
echo %presskey%
pause >nul
goto :eof
)
IF %ERRORLEVEL% NEQ 0 (
cls
echo.
echo ===============================================================================
echo %check2%
echo ===============================================================================
echo.
echo %presskey%
pause >nul
goto :eof
)

:MAINMENU
cls
set userinp=
echo ===============================================================================
echo %menust1%
echo %menust2%
echo %menust3%
echo ===============================================================================
echo.
set /p userinp= ^> %choice% 
set userinp=%userinp:~0,1%
if %userinp%==0 GOTO :QUIT
if %userinp%==3 GOTO :INFO
if %userinp%==2 (set WIMFILE=install.esd&goto :ISO)
if %userinp%==1 (set WIMFILE=install.wim&goto :ISO)
GOTO :MAINMENU

:ISO
cls
IF %PREPARED%==0 CALL :PREPARE
echo.
echo ===============================================================================
echo %creat% Setup Media Layout...
echo ===============================================================================
IF EXIST ISOFOLDER\ rmdir /s /q ISOFOLDER\
mkdir ISOFOLDER
if %arch%==x86 set "sizew=213.75"
if %arch%==x64 set "sizew=278.04"
if not exist "%~dp0bin\BaseImage-%arch%.esd" (
echo.
echo Download Setup Media Layout ^(size: %sizew% MB^)...
for /f "tokens=3 delims=:. " %%f in ('bitsadmin.exe /CREATE /DOWNLOAD "Download Setup Media Layout %build%" ^| findstr "Created job"') do set GUID=%%f
bitsadmin>nul /transfer %GUID% /download /priority foreground https://s1.rg-adguard.net/dl/decrypt/BaseImage_%arch%.esd "%~dp0bin\BaseImage-%arch%.esd"
)
"%wimlib%" apply "%~dp0bin\BaseImage-%arch%.esd" 1 ISOFOLDER\ >nul 2>&1
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %ind1e%&PAUSE&GOTO :QUIT)
"%wimlib%" extract "%ENCRYPTEDESD%" 1 Windows\system32\Utilman.exe --dest-dir=.\bin\temp --no-acls >nul 2>&1
move "%~dp0bin\temp\Utilman.exe" "%~dp0ISOFOLDER\sources\$OEM$\$1\Windows\System32\Utilman_old.exe" >nul 2>&1
echo.
echo ===============================================================================
echo %creat% boot.wim...
echo ===============================================================================
echo.
"%wimlib%" export "%~dp0bin\BaseImage-%arch%.esd" 2 ISOFOLDER\sources\boot.wim --compress=maximum
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %ind2e%&PAUSE&GOTO :QUIT)
echo.
"%wimlib%" export "%~dp0bin\BaseImage-%arch%.esd" 3 ISOFOLDER\sources\boot.wim --boot
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %ind2e%&PAUSE&GOTO :QUIT)
rd /s /q .\bin\temp
echo.
echo ===============================================================================
echo %creat% %WIMFILE%...
echo ===============================================================================
echo.
if /i %WIMFILE%==install.esd (
"%wimlib%" export "%ENCRYPTEDESD%" 1 ISOFOLDER\sources\%WIMFILE% --compress=LZMS
) else (
"%wimlib%" export "%ENCRYPTEDESD%" 1 ISOFOLDER\sources\%WIMFILE% --compress=maximum
)
"%wimlib%" info %~dp0ISOFOLDER\sources\%WIMFILE% 1 "Windows 10 %editionid% Insider Preview" "Windows 10 %editionid% Insider Preview" 1>nul 2>nul
"%wimlib%" info %~dp0ISOFOLDER\sources\%WIMFILE% 1 --image-property FLAGS=%editionid% 1>nul 2>nul
SET ERRORTEMP=%ERRORLEVEL%
IF %ERRORTEMP% NEQ 0 (echo.&echo %ind2e%&PAUSE&GOTO :QUIT)
echo.
echo ===============================================================================
echo %creat% ISO...
echo ===============================================================================
for /f "tokens=5,6,7,8,9,10 delims=: " %%G in ('bin\wimlib-imagex.exe info ISOFOLDER\sources\%WIMFILE% %sourcetime% ^| find /i "Last Modification Time"') do (set mmm=%%G&set "isotime=%%H/%%L,%%I:%%J:%%K")
call :setdate %mmm%
bin\cdimage.exe -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 %ind3e%
echo.
echo %presskey%
pause >nul
IF EXIST "%ENCRYPTEDESD%.bak" (
del /f /q "%ENCRYPTEDESD%" >nul 2>&1
ren "%ENCRYPTEDESD%.bak" %ENCRYPTEDESDN%
)
exit
)
rmdir /s /q ISOFOLDER\
echo.
echo %presskey%
pause >nul
GOTO :QUIT

:INFO
IF %PREPARED%==0 CALL :PREPARE
cls
echo ===============================================================================
echo                    BaseImage.wim file info
echo ===============================================================================
echo.
echo        OS: Windows 10 %editionid%
echo      Arch: %arch%
echo  Language: %langid%
echo   Version: %_ver%.%svcbuild%
if defined branch echo    Branch: %branch%
echo.
echo %presscont%
pause >nul
GOTO :MAINMENU

:PREPARE
SET PREPARED=1
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| findstr /b "Build"') do set build=%%i
if %build% LEQ 9600 GOTO :E_W81
for /f "tokens=2* delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Display Name"') do set "_os=%%j"
if ["%_os%"]==[""] (for /f "tokens=1* delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| findstr /b "Name"') do set "_os=%%j")
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Major"') do set _ver1=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Minor"') do set _ver2=%%i
set _ver=%_ver1%.%_ver2%.%build%
for /f "tokens=2 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Architecture"') do set arch=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Edition"') do set editionid=%%i
for /f "tokens=3 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Default"') do set langid=%%i
for /f "tokens=4 delims=: " %%i in ('bin\wimlib-imagex.exe info "%ENCRYPTEDESD%" 1 ^| find /i "Service Pack Build"') do set svcbuild=%%i
type .\ISOFOLDER\sources\ei.cfg 2>nul | find /i "Volume" 1>nul && set VOL=1
"%wimlib%" extract "%ENCRYPTEDESD%" 1 Windows\explorer.exe --dest-dir=.\bin\temp --no-acls >nul 2>&1
bin\7z.exe l .\bin\temp\explorer.exe >.\bin\temp\version.txt 2>&1
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
if /i %arch%==x86 (set _ss=x86) else (set _ss=amd64)
"%wimlib%" extract "%ENCRYPTEDESD%" 1 Windows\WinSxS\Manifests\%_ss%_microsoft-windows-coreos-revision* --dest-dir=.\bin\temp --no-acls >nul 2>&1
for /f "tokens=6,7 delims=_." %%i in ('dir /b .\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 "%ENCRYPTEDESD%" 1 ^| find /i "Last Modification Time"') do (set mmm=%%G&set yyy=%%L&set ddd=%%H-%%I%%J)
call :setmmm !mmm!
)
set _label2=
if /i %branch%==WinBuild (
"%wimlib%" extract "%ENCRYPTEDESD%" 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!

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%==x86_64 set arch=x64&set archl=X64

if /i %editionid%==Enterprise set DVDLABEL=J_CENA_%archl%FREV_%langid%_DV5&set DVDISO=%_label%ENTERPRISE_VOL_%archl%FRE_%langid%.ISO
if defined branch exit /b

: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

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

:QUIT
IF EXIST ISOFOLDER\ rmdir /s /q ISOFOLDER\
IF EXIST "%ENCRYPTEDESD%.bak" (
del /f /q "%ENCRYPTEDESD%" >nul 2>&1
ren "%ENCRYPTEDESD%.bak" %ENCRYPTEDESDN%
)
exit