@echo off
if defined PROCESSOR_ARCHITEW6432 start /w/b %SystemRoot%\Sysnative\cmd.exe /c "%~0"& goto :eof
setlocal EnableDelayedExpansion

2>nul mode 40,5
cd /d "%~dp0"

2>nul (for /f "tokens=1,2 delims==" %%a in (settings.txt) do (
 if /i "%%a"=="Interface" set out=%%b
 if /i "%%a"=="ViewMode" if /i "%%b"=="Path" set viewver=1==0
 if /i "%%a"=="Reboot" if /i "%%b"=="Off" set __Reboot=1
 if /i "%%a"=="Language" set __Lang=%%b
 if /i "%%a"=="OEM" if /i "%%b"=="Off" set __OEM=1
 if /i "%%a"=="Unattend" if /i "%%b"=="Off" set __unattend=1
 if /i "%%a"=="Setup" if /i "%%b"=="Embedded" set __setup.exe=1
 if /i "%%a"=="EI" if /i "%%b"=="Retail" set __EI=1
 if /i "%%a"=="Verify" if /i "%%b"=="On" set __ChInt=1
 if /i "%%a"=="DISM" if /i "%%b"=="System" set __DISM=1
 if /i "%%a"=="VHD" if /i "%%b"=="Detach" set __VHD=1
 if /i "%%a"=="Shell" set "shell=%%b"
 if /i "%%a"=="setup_x86.exe" set "setup86=%%b"
 if /i "%%a"=="setup_x64.exe" set "setup64=%%b"
))
for %%a in (HKU\.DEFAULT HKCU) do ^
for /f "skip=2 tokens=3" %%i in (
'2^>nul reg query "%%a\Control Panel\International" /v Locale'
) do if not defined out set out=%%i
if defined out (set out=%out:~-3%) else set out=409
call packs\lang.cmd %out%
title %ui000%

set xOS=& set \=%tmp%\winst.tmp
if exist %\%\WSelect.exe >nul 2>&1 copy /y %0 %\%\WSelect.exe|| (call :error "%ui072%"& goto :eof)
call :check_admin_rights|| (call :error "%ui028%"& goto :eof)

if defined shell set shell=& call start "Shell" /min %shell%

set _DiskSet_=C D E F G H I J K L M N O P Q R S T U V W X Y Z
set chcp_ACP=& set chcp_OEMCP=& set "pks=%~dp0packs"
>nul 2>&1 chcp&& for %%a in (ACP OEMCP) do for /f "tokens=3" %%b in (
'2^>nul reg query "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" /v %%a'
) do set "chcp_%%a=>nul chcp %%b"

echo/
:fud_convert
set out=& for %%i in (fud?dir*) do set out=%%i
if not defined out goto next
echo  %out% -^> dirlist.txt
if %out:2=% neq %out% >dirlist.txt rem/
for /f "tokens=*" %%i in (%out%) do ^
set "out=%%~pi"& for %%j in ("!out:~0,-1!") do set "out=%%~pj"&^
if defined out (
 if "!out!"=="\" set "out=%%~pi@\"
 set ask=& for /f "tokens=*" %%a in (dirlist.txt) do if "%%~a"=="!out:~0,-1!" set ask=1
 if not defined ask >>dirlist.txt echo;!out:~0,-1!
)
for %%i in (fud?dir*) do del /q %%i

:next
2>nul md %\%
>%\%\wimlist rem/
echo %ui001%
if exist dirlist.txt ^
for %%d in (%_DiskSet_%) do ^
dir %%d:\ >nul 2>&1&& for /f "tokens=*" %%i in (dirlist.txt) do ^
if "%%~nxi" neq "" if "%%~nxi"=="@" (call :handleE) else ^
for /f "tokens=*" %%j in ('2^>nul dir /b/ad "%%d:%%~i"') do call :handle

if not exist "%CD%\WInstall" goto next
set "dir=%CD%\WInstall"
for %%d in (%dir:~0,1%) do ^
for %%i in ("%dir:~2%") do ^
for /f "tokens=*" %%j in ('2^>nul dir /b/ad "%%d:%%~i"') do call :handle

:next
for /f %%i in (%\%\wimlist) do call :swm_handle& goto next
if exist fudlist.txt for %%d in (%_DiskSet_%) do ^
dir %%d:\ >nul 2>&1&& for /f "tokens=*" %%i in (fudlist.txt) do ^
if exist "%%d:%%i" >>%\%\wimlist echo %%d:%%i

:next
if defined xOS goto view
echo %ui035%
set xOS=64
if %PROCESSOR_ARCHITECTURE%==x86 if not defined PROCESSOR_ARCHITEW6432 set xOS=86
pushd %\%
>nul (
 copy /y "%pks%\7zdec%xOS%.exe"
 7zdec%xOS%.exe x "%pks%\tools%xOS%.7z"|| goto quit
)
popd
set file=& set wimdir=& if not defined viewver set viewver=1==1

for %%i in ("!setup%xOS%!") do set "stpdir=%%~dpi"
if defined stpdir set "stpdir=%stpdir:~0,-1%"& set "ui023=%ui071%"
set setup86=& set "setup64="

:view
echo %ui019%
>%\%\menulist rem/
for /f "tokens=*" %%i in (%\%\wimlist) do ^
if exist "%%i.txt" (
set "valid="
for /f "usebackq tokens=*" %%j in ("%%i.txt") do if not defined valid >>%\%\menulist echo %%j& set valid=1
) else if %viewver% (
set "bld="& set "valid="
for /f "tokens=*" %%j in ('2^>nul %\%\wimlib-imagex.exe info "%%i" 1') do (
set name=%%j
set "nm=!name:~0,17!"& set "val=!name:~24!"
if "!nm!"=="Name:            " (set win=!val!)  else ^
if "!nm!"=="Architecture:    " (set bit=!val!)  else ^
if "!nm!"=="Languages:       " (set lang=!val:~0,-1!) else ^
if "!nm!"=="Major Version:   " (set mj=!val!)   else ^
if "!nm!"=="Minor Version:   " (set mn=!val!)   else ^
if "!nm!"=="Build:           " set bld=!val!
if defined bld >>%\%\menulist echo !win! !bit! !lang! [ver !mj!.!mn!.!bld!]& set bld=& set valid=1
)
if not defined valid >>%\%\menulist echo %ui005%
) else call :ansi_out1
>>%\%\menulist (
echo/
echo %ui002%...
)
echo %ui007%
%\%\Wselect %\%\menulist "%ui008%" "" "%ui009%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& goto quit
set /a ask=%errorlevel%-1
if %ask%==0 (set "ask=") else set "ask=skip=%ask%"
for /f "tokens=* %ask%" %%i in (%\%\wimlist) do set file=%%i& set wimdir=%%~dpi& goto do_setup

echo %ui031%
>%\%\menulist (
 echo %ui006%
 echo/
 echo %ui040%
 echo %ui041%
 echo/
 echo %ui046%
 echo %ui056%
 echo %ui073%
 if exist fudlist.txt echo/& echo fudlist.txt -^> dirlist.txt
)
%\%\Wselect %\%\menulist "%ui039%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14 /ps=1
if errorlevel 10 goto aux_fud_convert
if errorlevel 8 goto maintenance
if errorlevel 7 goto vhd_init
if errorlevel 6 goto aux_disk_part
if errorlevel 4 goto aux_opt_search
if errorlevel 3 goto aux_opt_select
if errorlevel 1 if %viewver% (set viewver=1==0) else set "viewver=1==1"
goto view

:aux_fud_convert
>%\%\menulist (
 echo fudlist.txt -^> dirlist.txt
 echo/
 echo fudlist.txt -^> dirlist.txt + fudlist.txt
)
%\%\Wselect %\%\menulist "Converter" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& goto view
if errorlevel 3 (ren fudlist.txt fud4dir) else ren fudlist.txt fud2dir
goto fud_convert

:aux_disk_part
echo %ui001%

call :disk_info
:adp_init
set out=& for /f "delims=" %%i in (
'%\%\Wselect %\%\disk_info "%ui047%" "$item" "%ui060%" /fixedfont /bg^=#D4D0C8 /menu /hc^=#CC0000 /fs^=13'
) do set out=%%i

if not defined out goto view
for /f "tokens=1,2" %%a in ("%out%") do ^
if "%%a" neq "Disk" (goto adp_init) else set disk=%%b

>%\%\file rem/& for %%a in (schemes\*.dsf) do ^
set out=& for /f "tokens=1*" %%b in (%%a) do ^
if not defined out >>%\%\file echo %%c& set out=1
%\%\Wselect.exe %\%\file "%ui048%" "" "%ui049%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& goto view
set ask=%errorlevel%& set cnt=0
for %%a in (schemes\*.dsf) do set /a cnt=!cnt!+1& if !cnt!==%ask% set fname=%%a
<%fname% set /p p_out=
if "%p_out:###=%"=="%p_out%" goto next
set fsz=1000000000
call :size_icalc "%ui055%"|| goto view
set "p_out=!p_out:###=%size%!"
:next
>%\%\log (
 echo sel dis %disk%
 echo det dis
)
>%\%\file rem/& for /f "delims=" %%a in ('diskpart /s %\%\log') do >>%\%\file echo %%a
>%\%\log rem/& for /f "skip=4 delims=" %%a in (%\%\file) do ^
for /f "tokens=1,2 delims=:" %%b in ("%%a") do if "%%c"=="" >>%\%\log echo %%a
set cnt=0& for /f "delims=" %%a in (%\%\log) do set /a cnt=!cnt!+1& set var[!cnt!]=%%a

%chcp_ACP%
>%\%\file rem/& for /l %%a in (1,1,%cnt%) do (
 >>%\%\file echo !var[%%a]!
 if %%a==1 >>%\%\file echo/
)
%chcp_OEMCP%
for /f "tokens=1*" %%a in ("%p_out%") do >>%\%\file (echo/& echo   %%b)
:next
>>%\%\file (
 echo/
 echo                             [   %ui052%   ]
)
:ao_dp_select
%\%\Wselect.exe %\%\file "%ui050%" "" "%ui051%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=13&& goto view
set /a ask=%cnt%+5-%errorlevel%
if %ask% neq 0 goto ao_dp_select

>%\%\file rem/& for /f "delims=" %%b in (%fname%) do (
 set ln=%%b
 set ln=!ln:###=%size%!
 set ln=!ln:#=%disk%!
 >>%\%\file echo !ln!
)
echo %ui053%
start "%ui053%" /w cmd /c diskpart /s %\%\file ^|^| (echo/^& pause)
goto view

:aux_opt_search
echo %ui001%
>%\%\file rem/
set out=& for /f "tokens=1*" %%a in ('echo lis vol^| diskpart') do ^
if defined out (>>%\%\file echo/%%b) else if "%%a"=="----------" set out=1

set cnt=0& for /f "delims=" %%a in (%\%\file) do ^
set ln=%%a& if "!ln:~6,1!" neq " " set /a cnt=!cnt!+1& set var[!cnt!]= !ln:~6!

if %cnt%==0 for %%a in (%_DiskSet_%) do ^
dir %%a:\ >nul 2>&1&& set out=& for /f "delims=" %%b in ('dir %%a:') do ^
if not defined out set out=1& set /a cnt=!cnt!+1& set var[!cnt!]= %%a:    %%b

echo %ui031%
:ao_select
%chcp_ACP%& >%\%\menulist rem/
for /l %%a in (1,1,%cnt%) do >>%\%\menulist echo !var[%%a]!
>>%\%\menulist (
 echo/
 echo                             [  %ui044%  ]
)
%chcp_OEMCP%
%\%\Wselect.exe %\%\menulist "%ui045%" "" "%ui043%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& goto view
if %errorlevel% gtr %cnt% (goto ao_execute) else ^
if "!var[%errorlevel%]:~0,1!"=="*" (
 set var[%errorlevel%]= !var[%errorlevel%]:~1!
) else set var[%errorlevel%]=*!var[%errorlevel%]:~1!
goto ao_select
:ao_execute
set SrchList=& for /l %%a in (1,1,%cnt%) do ^
if "!var[%%a]:~0,1!"=="*" set SrchList=!SrchList! !var[%%a]:~1,1!
if not defined SrchList goto ao_select

>%\%\wimlist rem/
for %%d in (%SrchList%) do ^
dir %%d:\ >nul 2>&1&& (
echo %ui026% %%d:...
for /f "tokens=*" %%i in ('2^>nul dir /b/s %%d:\*.wim;%%d:\*.esd;%%d:\*.swm') do ^
if %%~zi gtr 1000000000 >>%\%\wimlist echo %%i
)
for %%i in (%\%\wimlist) do if %%~zi==0 goto view
call :swm_handle
2>nul (
 >fudlist.txt rem/
 for /f "delims=" %%i in (%\%\wimlist) do >>fudlist.txt echo %%~pnxi
)
goto view

:vhd_init
%chcp_ACP%
set fname=& for /f "delims=" %%a in (
'%\%\FileToOpen "set fname=" "*.vhd;*.vhdx" "%ui057%" /noquote /maynotexist'
) do %%a
%chcp_OEMCP%
if not defined fname goto view
set out=& if exist "%fname%" set out=1& goto vhd_nocre

for %%a in ("%fname%") do (
 for /f "tokens=3" %%a in ('dir %%~da/-c') do set fsz=%%a
 if "%%~xa"=="" set "fname=%fname%.vhd"
)
set /a fsz=(%fsz:~0,-6%+0)/1024
call :size_icalc "Limit: ~%fsz% Gb"|| goto view
>%\%\log (
 for %%a in ("%fname%") do echo VHD:  %%~a
 echo Size: %size% Gb
 echo/
 echo [  Ok  ]
)
:vhd_cr_ok
%\%\Wselect %\%\log "VHD" "" "%ui058%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& goto view
if %errorlevel% neq 4 goto vhd_cr_ok

set /a size*=1024
if %xOS%==64 if not exist %SystemRoot%\SysWOW64\kernel32.dll (
 >%\%\log echo cre vdisk file="%fname%" maximum=%size%
 goto vhd_att
)
set /a size*=1024
set size=%size:~0,-3%
set /a size*=1024
start /w cmd /c %\%\vhdtool /create "%fname%" %size%000 ^|^| (echo/^& pause)
if not exist "%fname%" goto view
:vhd_nocre
>%\%\log echo sel vdisk file="%fname%"
:vhd_att
>>%\%\log echo attach vdisk
if not defined out 2>nul md %\%\mount& >>%\%\log (
 echo clean
 echo cre par prim
 echo assign mount="%\%\mount"
 echo for quick fs=ntfs label="VHD"
 echo remove
)
echo %ui059%
start "%ui059%" /w cmd /c diskpart /s %\%\log ^|^| (echo/^& pause)
set "vhd_fname=%fname%"
goto view

:size_icalc
set /a ones=0,decs=0,huns=0
:size_calc
if %ones% gtr 9 set /a ones=0,decs+=10
if %decs% gtr 90 set /a decs=0,huns+=100
set /a size=huns+decs+ones
>%\%\log (
 echo 000
 echo ---------------
 echo **1 : %ones%
 echo *1* : %decs%
 echo 1** : %huns%
 echo ---------------
 echo Size: %size% Gb
 echo ---------------
 echo/
 echo      [  Ok  ]
)
%\%\Wselect %\%\log "%ui054%" "" %1 /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& exit /b 1
set ask=%errorlevel%
if %ask%==1 goto size_icalc
if %ask%==10 if %size% gtr 0 exit /b 0
if %ask%==5 set /a huns+=100
if %ask%==4 set /a decs+=10
if %ask%==3 set /a ones+=1
set /a size=huns+decs+ones
if %size% geq %fsz% (
 if %ask%==5 set /a huns-=100
 if %ask%==4 set /a decs-=10
 if %ask%==3 set /a ones-=1
)
goto size_calc

:aux_opt_select
%chcp_ACP%
set file=& for /f "delims=" %%a in (
'%\%\FileToOpen "set file=" "*.esd;*.wim;*.swm" "%ui042%" /noquote'
) do %%a
%chcp_OEMCP%
if not defined file goto view
set ask=& for %%Z in ("%file%") do set wimdir=%%~dpZ& if exist "%%~Z.txt" (
 set valid=& for /f "usebackq tokens=*" %%j in ("%%~Z.txt") do ^
if not defined valid >%\%\menulist echo %%j& set valid=1
) else >%\%\menulist echo %%~Z

:do_setup
for /f "tokens=* %ask%" %%i in (%\%\menulist) do set Distr=%%i& goto next
:next
if defined __OEM (set OEM=%ui021%) else ^
set OEM=%ui021%& if exist "%wimdir%$OEM$" (
  set OEM=$OEM$
) else ^
if exist "%wimdir%..\$OEM$" (
  set OEM=..\$OEM$
) else ^
if exist "%CD%\WInstall\$OEM$" set OEM=_WInst_\$OEM$

if defined __unattend (set unattend=%ui021%) else ^
set unattend=%ui021%& if exist "%wimdir%unattend.xml" (
  set unattend=unattend.xml
) else ^
if exist "%wimdir%..\unattend.xml" (
  set unattend=..\unattend.xml
) else ^
if exist "%CD%\WInstall\unattend.xml" set unattend=_WInst_\unattend.xml

if defined __setup.exe (set setup.exe=%ui023%) else ^
set setup.exe=%ui023%& if exist "%wimdir%setup.exe" set setup.exe=%ui025%

if defined __ChInt (set ChInt=%ui020%) else set ChInt=%ui021%
if defined __Reboot (set Reboot=%ui021%) else set Reboot=%ui020%
set setupdll=%ui037%
if defined __EI (set EI=Retail) else set EI=Volume
call :set_ei %EI%
if defined __Lang (set Lang=%__Lang%) else set Lang=Auto
set Drivers=%ui021%
set newBCD=%ui021%

:pre_setup
echo %ui031%
>%\%\options (
if %viewver% (
 for /f "delims=" %%Z in ("%Distr%") do echo %ui010% %%~Z
) else call :ansi_out2
echo/
echo %ui011% %OEM%
for %%Z in ("%unattend%") do echo %ui012% %%~Z
echo %ui013% %setup.exe%
echo/
echo %ui014% %ChInt%
echo %ui015% %Reboot%
if "%setup.exe%"=="%ui023%" (
 echo %ui016% %EI%
 echo %ui017% %Lang%
 echo %ui036% %setupdll%
)
echo/
echo/
echo %ui024% [ Setup.exe         ]
echo/
echo %ui027% [ Wimlib-imagex.exe ]
)
%\%\Wselect %\%\options "%ui018%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& goto view
set ask=%errorlevel%
if %ask%==1 if %viewver% (set viewver=1==0) else set viewver=1==1
if %ask%==3 call :select_OEM
if %ask%==4 call :select_unattend
if %ask%==5 if exist "%wimdir%setup.exe" if "%setup.exe%"=="%ui025%" (set setup.exe=%ui023%) else set setup.exe=%ui025%
if %ask%==7 if %ChInt%==%ui020% (set ChInt=%ui021%) else set ChInt=%ui020%
if %ask%==8 if %Reboot%==%ui020% (set Reboot=%ui021%) else set Reboot=%ui020%
if %ask%==9 (
  if %EI%==Volume (set EI=Retail) else set EI=Volume
  call :set_ei !EI!
)
if %ask%==10 call :choice %\%\lang.lst "%ui022%" Lang
if %ask%==11 if "%setup.exe%"=="%ui023%" (
 if "%setupdll%"=="%ui037%" (set setupdll=%ui038%) else set setupdll=%ui037%
) else goto setup
if %ask%==13 goto apply
if %ask%==14 goto setup
if %ask%==16 goto apply
goto pre_setup

:setup
if %ChInt%==%ui021% goto next
start /wait cmd /c echo/^& %\%\wimlib-imagex.exe verify "%file%"^|^| (color 4e^& echo/^& pause^& exit /b 1)
if %errorlevel% gtr 0 goto view
:next
set setup=/InstallFrom:"%file%"

if "%OEM%" neq "%ui021%" ^
if %OEM:~0,7%==_WInst_ (
  set setup=%setup% /m:"!OEM:_WInst_=%CD%\WInstall!"
) else ^
set setup=%setup% /m:"%wimdir%%OEM%"

if "%unattend%" neq "%ui021%" ^
if %unattend:~0,7%==_WInst_ (
  set setup=%setup% /Unattend:"!unattend:_WInst_=%CD%\WInstall!"
) else ^
set setup=%setup% /Unattend:"%wimdir%%unattend%"

if %Reboot%==%ui021% set setup=%setup% /NoReboot

if "%setup.exe%" neq "%ui023%" "%wimdir%setup.exe" %setup%&& goto quit|| goto view

if %Lang%==Auto (
for /f "tokens=2 delims=:" %%i in ('2^>nul %\%\wimlib-imagex.exe info "%file%" 1^| find "Languages:"') do call :set_lang %%i
) else call :set_lang %Lang%

if defined stpdir >nul (
 copy /y %\%\ei.cfg "%stpdir%"
 copy /y %\%\lang.ini "%stpdir%"
 goto next
)
echo %ui004%
pushd %\%
>nul 7zdec%xOS%.exe x "%pks%\setup%xOS%.7z"|| goto quit
popd
set stpdir=%\%
:next
if "%setupdll%"=="%ui037%" (
 set dll.new="%stpdir%\winsetup.dll.new"
 set dll.old="%stpdir%\winsetup.dll.old"
) else (
 set dll.new="%stpdir%\winsetup.dll.old"
 set dll.old="%stpdir%\winsetup.dll.new"
)
for /f "tokens=3" %%i in ('2^>nul %\%\wimlib-imagex.exe info "%file%" 1^| find /i "major version: "') do ^
if %%i==10 (set out=%dll.new%) else set out=%dll.old%
>nul 2>&1 copy /y %out% "%stpdir%\winsetup.dll"
"%stpdir%\setup.exe" %setup%|| goto view
goto quit

:handle
for %%a in (wim swm esd) do ^
for %%b in ("%%d:%%~i\%%~j\*.%%a") do ^
if /i "%%~nxb" neq "boot.wim" >>%\%\wimlist echo %%~b
exit /b

:handleE
for %%a in (wim swm esd) do ^
for %%b in ("%%d:%%~pi*.%%a") do ^
if /i "%%~nxb" neq "boot.wim" >>%\%\wimlist echo %%~b
exit /b

:swm_handle
for /f "tokens=*" %%a in (%\%\wimlist) do if /i "%%~xa"==".swm" (
 >>%\%\swm echo %%a
 set cnt=& for /f "tokens=*" %%b in (%\%\wimlist) do if /i "%%~b"=="%%~dpna!cnt!.swm" (
  if not defined cnt (set cnt=2) else set /a cnt=!cnt!+1
 ) else >>%\%\new echo %%b
 del /q %\%\wimlist& ren %\%\new wimlist
 goto swm_handle
)
if exist %\%\swm >>%\%\wimlist (type %\%\swm)& del /q %\%\swm
exit /b

:select_OEM
if not exist "%wimdir%$OEM$" ^
if not exist "%wimdir%..\$OEM$" ^
if not exist "%CD%\WInstall\$OEM$" exit /b
>%\%\file (
echo %ui021%
if exist "%wimdir%$OEM$" echo $OEM$
if exist "%wimdir%..\$OEM$" echo ..\$OEM$
if "!wimdir:%CD%\WInstall\=!"=="%wimdir%" if exist "%CD%\WInstall\$OEM$" echo _WInst_\$OEM$
)
call :choice %\%\file "$OEM$" OEM
exit /b

:select_unattend
if not exist "%wimdir%unattend.xml" ^
if not exist "%wimdir%..\*.xml" ^
if not exist "%CD%\WInstall\*.xml" exit /b
>%\%\file (
echo %ui021%
if exist "%wimdir%unattend.xml" echo unattend.xml
for %%a in ("%wimdir%..\*.xml") do echo ..\%%~nxa
if "!wimdir:%CD%\WInstall\=!"=="%wimdir%" ^
for %%a in ("%CD%\WInstall\*.xml") do echo _WInst_\%%~nxa
)
call :choice %\%\file "unattend.xml" unattend
exit /b

:choice
%\%\Wselect %1 %2 "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& exit /b
set /a var=%errorlevel%-1
if %var%==0 (set var=) else set var=skip=%var%
for /f "tokens=* %var%" %%i in (%1) do set %3=%%i& exit /b

:set_ei
>%\%\ei.cfg (
echo [EditionID]
echo/
echo [Channel]
echo %1
echo/
echo [VL]
if %1==Volume (echo 1) else echo 0
)
exit /b

:set_lang
>%\%\lang.ini (
echo [Available UI Languages]
for %%i in (%*) do echo %%i = 3
echo/
echo [Fallback Languages]
for %%i in (%*) do echo %%i = en-us
)
exit /b

:check_admin_rights
for %%i in (%SystemRoot%\System32\dism.exe) do ^
if exist %%i (
 >nul %%i|| exit /b 1
) else (
 >nul 2>&1 (>%%i rem/&& del /q %%i)|| exit /b 1
)
exit /b

:error
color 4e
echo/
echo %~1
echo/
echo %ui003%
>nul pause
exit /b

:ansi_out1
%chcp_ACP%
for %%Z in (1) do >>%\%\menulist echo %%i
%chcp_OEMCP%
exit /b

:ansi_out2
<nul set /p "=%ui010%"
%chcp_ACP%
for %%Z in ("%file%") do echo  %%~Z
%chcp_OEMCP%
exit /b


:disk_info
set log=%\%\log& set disk=0
>%\%\disk_info.tmp call :di_init
>%\%\disk_info rem/& set cnt=0
for /f "delims=" %%i in (%\%\disk_info.tmp) do (
 set /a cnt=!cnt!+1
 set var[!cnt!]=%%i
)
%chcp_ACP%
for /l %%i in (1,1,%cnt%) do >>%\%\disk_info echo/ !var[%%i]!& set "var[%%i]="
%chcp_OEMCP%
del /q %\%\disk_info.tmp
exit /b

:di_init
>%log% (
 echo sel dis %disk%
 echo det dis
)
set out=& set dev=& for /f "delims=" %%i in ('2^>nul diskpart /s %log%') do ^
if defined out (
 echo %%i
) else (
 set "last=!line!"& set line=%%i
 if not defined dev if "!line: :=!" neq "!line!" (
  set dev=1
  if %disk% gtr 0 echo/ 
  echo Disk %disk%   [!last!]
 )
 if "!line:###=!" neq "!line!" set out=1& echo %%i
)
if not defined dev exit /b
set /a disk+=1
goto di_init


:apply
echo %ui034%

for /l %%a in (1,1,100) do set "Edition[%%a]="
if exist "%file%.txt" ^
for /f "usebackq tokens=1* delims==" %%a in ("%file%.txt") do ^
if +%%a gtr 0 if +%%a lss 100 if "%%b" neq "" set "Edition[%%a]=%%b"

set /a cnt=1& set exist=& >%\%\edlist rem/
for /f "tokens=*" %%a in ('2^>nul %\%\wimlib-imagex.exe info "%file%"') do (
set line=%%a
if defined Edition[!cnt!] (
 if "!line:~0,17!"=="Default Language:" call set "name=%%Edition[!cnt!]%%"& set exist=1
) else ^
if "!line:~0,5!"=="Name:" (set "name=!line:~24!                                        ") else ^
if "!line:~0,13!"=="Architecture:" (
 if "!line:~27,1!"=="_" (set name=!name:~0,40! x86_64) else set "name=!name:~0,40! x86   "
) else ^
if "!line:~0,11!"=="Edition ID:" (set name=!name! [!line:~24!,) else ^
if "!line:~0,10!"=="Languages:" set name=!name! !line:~24,-1!]& set exist=1
if defined exist set exist=& set /a cnt=!cnt!+1& for %%Z in ("!name!") do >>%\%\edlist echo %%~Z
)
echo %ui032%
%\%\Wselect %\%\edlist "%ui029%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& goto pre_setup
set ed=%errorlevel%

call :apply_new_order|| goto pre_setup

for %%a in ("%file%") do if /i "%%~xa" neq ".swm" (set ref=) else set ref=--ref="%%~dpna*.swm"
if %Lang% neq Auto (set lng=%Lang%) else ^
for /f "tokens=3" %%a in ('%\%\wimlib-imagex.exe info "%file%" %ed%^| find "Default Language:"') do set lng=%%a
2>nul mode 84,40
title Windows applying
echo -----------------------------------------------------------------------------------
echo                      *** WInst script for Windows applying ***
echo -----------------------------------------------------------------------------------
echo/
echo Windows applying:
echo -----------------------------------------------------------------------------------
%\%\wimlib-imagex.exe apply "%file%" %ed% %ref% %WinDsk%:\|| (echo/& pause& call :disletter& goto console_restore)
echo -----------------------------------------------------------------------------------
echo Done^^!
echo/
if %firmware%==NONE goto next
call :setting_BCD
if not defined Edition[%ed%] goto next
for %%Z in ("!Edition[%ed%]!") do >%\%\log echo %%~Z
%chcp_ACP%
<%\%\log set /p BootDesc=
%chcp_OEMCP%
set bcdpth=%SysDsk%:\Boot\BCD
if %firmware% neq UEFI (
 echo BIOS
 call :desc_define
)
set bcdpth=%SysDsk%:\EFI\Microsoft\Boot\BCD
if %firmware% neq BIOS (
 echo UEFI
 call :desc_define
)
:next
echo/
echo Finalizing...
echo/
set apply_oem=& set "apply_unattend="
if "%OEM%" neq "%ui021%" ^
if %OEM:~0,7%==_WInst_ (
 set "apply_oem=!OEM:_WInst_=%CD%\WInstall!"
) else ^
set "apply_oem=%wimdir%%OEM%"

if "%unattend%" neq "%ui021%" ^
if %unattend:~0,7%==_WInst_ (
 set "apply_unattend=!unattend:_WInst_=%CD%\WInstall!"
) else ^
set "apply_unattend=%wimdir%%unattend%"

if defined apply_unattend (
 2>nul md %WinDsk%:\Windows\Panther
 copy /y "%apply_unattend%" %WinDsk%:\Windows\Panther\unattend.xml
)
if defined apply_oem xcopy "%apply_oem%\$$\Setup\*.*" %WinDsk%:\Windows\Setup /y/h/r/e
call :proc_drivers
call :disletter
echo/
echo Applying completed^^!
if %Reboot%==%ui020% (
 set "rst=%PATH%;%\%"
 for %%i in (shutdown.exe) do start %%~$rst:i /r /f /t 05
) else (
 echo/
 pause
)
goto quit

:disletter
if %LetterAssigned%==1 (
 echo sel vol %SysDsk%
 echo remove
)| >nul diskpart
exit /b

:console_restore
title %ui000%
2>nul mode 40,5
goto pre_setup

:desc_define
%\%\bcdedit /store %bcdpth%| >%\%\log find "description"& set "NewDesc=%BootDesc%"
for /l %%i in (2,1,30) do (
 set "match="
 for /f "tokens=1*" %%a in (%\%\log) do if "!NewDesc!"=="%%b" set match=1
 if not defined match (
  >nul %\%\bcdedit /store %bcdpth% /set {default} description "!NewDesc!"
  echo New description: "!NewDesc!"
  exit /b
 )
 set "NewDesc=!BootDesc! [%%i]"
)
exit /b

:setting_BCD
<nul set /p "=Backup previous BCD...        "
for %%i in (%SysDsk%:\Boot %SysDsk%:\EFI\Microsoft\Boot) do ^
if exist %%i\BCD (
 2>nul rd /s/q %%i\{WInst}.bak& md %%i\{WInst}.bak
 >nul 2>&1 xcopy /h/y %%i\BCD %%i\{WInst}.bak|| >nul %\%\bcdedit /export %%i\{WInst}.bak\BCD
)
echo done.
if %newBCD%==%ui020% >nul 2>&1 (
 if %firmware% neq BIOS (
  echo f| xcopy /h/y %SysDsk%:\EFI\Microsoft\Boot\{WInst}.bak\BCD %\%\BCD.efi
  rd /q/s %SysDsk%:\EFI
 )
 if %firmware% neq UEFI (
  echo f| xcopy /h/y %SysDsk%:\Boot\{WInst}.bak\BCD %\%\BCD.bios
  rd /q/s %SysDsk%:\Boot
  del /f/a/q %SysDsk%:\bootmgr
 )
)
<nul set /p "=Setting up new boot entry...  "
if defined lng set lng=/l %lng%
%\%\bcdboot.exe %WinDsk%:\Windows /s %SysDsk%: %lng% /f %firmware%|| (echo/& pause)
echo/
if %newBCD%==%ui020% >nul 2>&1 (
 echo f| xcopy %\%\BCD.efi %SysDsk%:\EFI\Microsoft\Boot\{WInst}.bak\BCD /h/y
 echo f| xcopy %\%\BCD.bios %SysDsk%:\Boot\{WInst}.bak\BCD /h/y
 del /q %\%\BCD.*
)
exit /b

:quit
if defined __VHD if defined vhd_fname ^
echo  Detaching VHD... &(
 echo sel vdisk file="%vhd_fname%"
 echo detach vdisk
)| >nul diskpart
rd /q/s %\%
exit


:maintenance
setlocal
set lng=& set Reboot=%ui021%& set Drivers=%ui021%& set newBCD=%ui021%
set "ui065=%ui077%"& set "ui064=%ui075%"& set "ui063=%ui076%"& set "ui100=Fix boot:"
call :apply_new_order|| goto maint_quit
if exist %WinDsk%:\Windows goto next
>%\%\menulist (
 echo %win_part%
 echo/
 echo                                     [  Ok  ]
)
%\%\Wselect %\%\menulist "%ui067%" "" "%ui074%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=13
call :disletter
goto maint_quit
:next
2>nul mode 84,40
title %ui075%
echo/
if %firmware% neq NONE ^
if %newBCD%==%ui020% (call :setting_BCD) else ^
if "%Reboot%"=="%ui021%" (call :setting_BCD) else ^
if "%Reboot:+=%" neq "%Reboot%" call :setting_BCD

if "%Reboot:~1,2%"=="BR" (
 %\%\bootsect /nt60 %WinDsk%: /force
 if "%SysDsk%" neq " " echo/& %\%\bootsect /nt60 %SysDsk%: /force /mbr
)
if "%Reboot:~0,3%"=="Adv" call :AdvBootOpt
call :proc_drivers
call :disletter
echo/
echo Maintenance completed^^!
echo/
pause
2>nul mode 40,5
title %ui000%
:maint_quit
endlocal
goto view

:AdvBootOpt
echo/
echo Setting up Advanced Boot Options:
set out=%\%\bcdedit /set {globalsettings} advancedoptions true /store
if %firmware% neq BIOS %out% %SysDsk%:\EFI\Microsoft\Boot\BCD
if %firmware% neq UEFI %out% %SysDsk%:\Boot\BCD
echo/
<nul set /p "=Writing AdvBootOpt_off.cmd... "
>%WinDsk%:\AdvBootOpt_off.cmd (
 echo @echo off
 echo echo/
 echo bcdedit.exe /deletevalue {globalsettings} advancedoptions^&^& color 0a^|^| color 4e
 echo echo/
 echo pause
)
echo done.
exit /b


:apply_new_order
echo %ui001%
call :disk_info

echo %ui033%
:ano_win
set win_part=& for /f "delims=" %%i in (
'%\%\Wselect %\%\disk_info "%ui061%" "$item" "%ui062% [ WINDOWS = C:\Windows, C:\Program Files, pagefile.sys ]" /fixedfont /bg^=#D4D0C8 /menu /hc^=#CC0000 /fs^=13'
) do set win_part=%%i

if not defined win_part exit /b 1
2>nul set /a cnt=0x%win_part:~10,2%|| goto ano_win

:ano_boot
>%\%\disk_info.boot (
 type %\%\disk_info
 echo/
 echo %ui066%
)
set boot_part=& for /f "delims=" %%i in (
'%\%\Wselect %\%\disk_info.boot "%ui061%" "$item" "%ui062% [ BOOTLOADER = BCD, bootmgr, bootx64.efi ]" /fixedfont /bg^=#D4D0C8 /menu /hc^=#CC0000 /fs^=13'
) do set boot_part=%%i

if not defined boot_part exit /b 1
if "%boot_part%"=="%ui066%" (
 set firmware=NONE
 set "boot_part= "
 set "SysDsk= "
 goto ano_draw
)
2>nul set /a cnt=0x%boot_part:~10,2%|| goto ano_boot

echo %ui031%
>%\%\menulist (
 echo ALL  [BIOS+UEFI]
 echo BIOS
 echo UEFI
)
%\%\Wselect %\%\menulist "Firmware" "" "Firmware:" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=14&& exit /b 1
if errorlevel 3 set firmware=UEFI
if errorlevel 2 set firmware=BIOS
if errorlevel 1 set firmware=ALL

:ano_draw
for /f "delims=" %%a in (%\%\disk_info) do ^
if "%%a"=="%win_part%" (goto next) else ^
for /f %%b in ("%%a") do if "%%b"=="Disk" set WinDsk=%%a
:next
if %firmware%==NONE goto ano_menu
for /f "delims=" %%a in (%\%\disk_info) do ^
if "%%a"=="%boot_part%" (goto ano_menu) else ^
for /f %%b in ("%%a") do if "%%b"=="Disk" set SysDsk=%%a
:ano_menu
>%\%\menulist (
 echo Windows:
 echo/
 echo %WinDsk%
 echo  -------------------------------------------------------------------------------
 echo %win_part%
 echo/
 echo Bootloader:
 echo/
 echo/%SysDsk%
 echo  -------------------------------------------------------------------------------
 echo/%boot_part%
 echo/
 echo Firmware: %firmware%
 echo/
 if %firmware%==NONE (echo/) else echo New BCD:  %newBCD%
 echo Drivers:  %Drivers%
 echo %ui100% %Reboot%
 echo/
 echo                            [ %ui065% ]
)
%\%\Wselect %\%\menulist "%ui064%" "" "%ui063%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=13&& exit /b 1
set ask=%errorlevel%
if %ask%==15 if %newBCD%==%ui020% (set newBCD=%ui021%) else set newBCD=%ui020%
if %ask%==16 call :get_drivers
if %ask%==17 if "%ui065%" neq "%ui077%" (
 if "%Reboot%"=="%ui020%" (set Reboot=%ui021%) else set Reboot=%ui020%
) else call :get_fix
if %ask% neq 19 goto ano_menu

echo %ui001%
call :check_access "%win_part%"  WinDsk|| exit /b 1
if "%ui065%" neq "%ui077%" call :check_win_exist|| exit /b 1
call :check_access "%boot_part%" SysDsk|| exit /b 1

if %firmware% neq NONE (
 echo sel vol %SysDsk%
 echo active
)| >nul diskpart
exit /b

:check_access
set valid=& set LetterAssigned=0
if "%~1"==" " exit /b
for %%a in (%_DiskSet_%) do >nul 2>&1 cd %%a:|| (set letter=%%a& goto next)
:next
set "out=%~1"& set out=!out:~10,2!
for /f "tokens=*" %%a in (
'(echo sel vol %out%^& echo lis vol^)^| diskpart^| find "* "'
) do set line=%%a
if "%line:~15,1%" neq " " set letter=%line:~15,1%& goto chk_mark
(
 echo sel vol %out%
 echo assign letter="%letter%"
)| >nul diskpart
set LetterAssigned=1
:chk_mark
2>nul (>%letter%:\.mark echo mark)
if exist %letter%:\.mark (
 del /q %letter%:\.mark
 set %2=%letter%
 exit /b 0
)
>%\%\menulist (
 echo %~1
 echo/
 if defined valid (
 echo                                     [  Ok  ]
) else (
 echo                                    [ ChkDsk ]
))
%\%\Wselect %\%\menulist "%ui067%" "" "%ui070%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=13
if not defined valid if %errorlevel%==3 (
 start /w/max cmd /c title %ui075%^& chkdsk %letter%:/x/f^& echo/^& pause
 set valid=1
 goto chk_mark
)
exit /b 1

:check_win_exist
if not exist %WinDsk%:\Windows exit /b
>%\%\menulist (
 echo %win_part%
 echo/
 echo                             [ %ui069% ]
)
%\%\Wselect %\%\menulist "%ui067%" "" "%ui068%" /fixedfont /bg=#D4D0C8 /menu /hc=#CC0000 /fs=13&& exit /b 1
if %errorlevel%==3 (
 echo sel vol %WinDsk%
 echo for quick override label="Windows" noerr
)| >nul diskpart& goto check_win_exist
exit /b 1

:get_fix
if %firmware%==NONE (
 if "%Reboot%"=="%ui021%" (set Reboot=PBR) else set Reboot=%ui021%
 exit /b
)
>%\%\menulist (
 echo MBR/PBR
 if %newBCD%==%ui021% echo MBR/PBR + update BCD
 echo/
 echo Advanced Boot Options
)
set Reboot=%ui021%& for /f "delims=" %%i in (
'%\%\Wselect %\%\menulist "%ui075%" "$item" "Fix boot:" /fixedfont /bg^=#D4D0C8 /menu /hc^=#CC0000 /fs^=14'
) do set Reboot=%%i
exit /b

:get_drivers
set out=& set Drivers=%ui021%
if "%ui065%" neq "%ui077%" goto get_drv_apl
>%\%\menulist (
 echo %ui079%
 echo/
 echo %ui080%
)
%\%\Wselect %\%\menulist "%ui075%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& exit /b
if %errorlevel%==1 goto get_drv_add
set Drivers=%ui078%
exit /b
:get_drv_apl
>%\%\menulist (
 echo %ui079%
 echo/
 echo %ui081%
 echo/
 echo %ui084%
)
%\%\Wselect %\%\menulist "%ui075%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& exit /b
if %errorlevel%==3 set Drivers=%ui083%& exit /b
if %errorlevel%==5 set out=1
:get_drv_add
%chcp_ACP%
set Drivers=& for /f "tokens=1*" %%a in (
'%\%\WFolder rem %~d0\ "%ui030%" /noquote /center'
) do set "Drivers=%%b"
%chcp_OEMCP%
if not defined Drivers (set Drivers=%ui021%) else if defined out set "Drivers=%ui085% + %Drivers%"
exit /b

:proc_drivers
if "%Drivers%"=="%ui021%" exit /b
set fname=%WinDsk%:\Windows\Temp\WInst_Add_Drivers.bak
echo/
echo Processing drivers...
echo/
set tdir=& if not defined __DISM (
 echo %ui035:~1%
 set tdir=%WinDsk%:\Windows\Temp\$$$_DISM_$$$.TMP\
 2>nul md !tdir!
 pushd !tdir!
 >nul (
  copy /y "%pks%\7zdec%xOS%.exe"
  7zdec%xOS%.exe x "%pks%\setup%xOS%.7z"|| (popd& goto pdrv_quit)
 )
 popd
)
echo %ui001:~1%
%tdir%Dism.exe /Image:%WinDsk%:\ /Get-Drivers /English| >%\%\drivers find "Published Name :"

if "%Drivers:~1,2%"==":\" goto add_drv
set valid=& if "%Drivers::\=%" neq "%Drivers%" set valid=1
for %%i in (%\%\drivers) do if %%~zi==0 (
 echo/
 echo Drivers not found.
 if not defined valid goto pdrv_quit
)
if "%Drivers%" neq "%ui078%" >%fname% rem/& goto del_drv
>%\%\menulist (
 echo %ui081%
 if exist %fname% echo/& echo %ui082%
)
%\%\Wselect %\%\menulist "%ui075%" "" "" /fixedfont /bg=#D4D0C8 /menu /fs=14&& goto pdrv_quit
if %errorlevel%==1 >%fname% rem/
:del_drv
for /f "delims=" %%i in (%\%\drivers) do >nul find "%%i" %fname%||^
for /f "tokens=4" %%a in ("%%i") do %tdir%Dism.exe /Image:%WinDsk%:\ /Remove-Driver /Driver:%%a
del /q %fname%
if not defined valid goto pdrv_quit
set "Drivers=%Drivers:* + =%"
>%\%\drivers rem/
:add_drv
>nul copy /y %\%\drivers %fname%
%tdir%Dism.exe /Image:%WinDsk%:\ /Add-Driver /Driver:"%Drivers%" /Recurse /ForceUnsigned
for /r "%Drivers%" %%i in (*.msu *.cab) do ^
%tdir%Dism.exe /Image:%WinDsk%:\ /Add-Package /PackagePath:"%Drivers%"& goto pdrv_quit
:pdrv_quit
if not defined __DISM (
 echo/
 echo Deleting temp folder...
 for /l %%i in (1,1,100) do 2>nul rd /q/s %tdir%
)
exit /b

