@echo off
:: =================================
::	ApplyImage_boot.bat 2025-02-28 10:20:56	2025-03-15 15:22:50
::  https://learn.microsoft.com/zh-tw/windows-hardware/manufacture/desktop/capture-and-apply-windows-using-a-single-wim?view=windows-11
:: =================================

rem ====	򥻨tγ]w set @wn[""קKť
set "Driver=%~d0"
set "FilePath=%~p0"

rem ====----	IsAdmin
call :IsAdmin

cd /D "%Driver%%FilePath%"
rem ==--
rem ====	parameters

if /i "%~1"=="" (
	exit /b
)
if /i "%~2"=="" (
	exit /b
)
if /i "%~3"=="" (
	exit /b
)

:: set "Disk_No=4"
:: set "Disk_Name=970EVO"
:: set "WinVersion=Win11_2024_10.0.26100"

set "Disk_No=%~1"
set "Disk_Name=%~2"
set "WinVersion=%~3"

call :SetFilePath

call :DiskPartition

rem call :ApplyImage

rem call :Intel_RST

rem call :NetFx3

rem call :PackageUpdate

call :winboot

call :WinRecovery

call :WinReAgentc

call :OOBE

cd /D "%Driver%%FilePath%"

goto End

:SetFilePath
	echo|set /p="[45m"
	echo SetFilePath[0m
	echo|set /p="[0m"
	rem ====	File Path
	set "WinSources_Path=%Driver%%FilePath%..\..\WinSources\"

	set "Recovery_Path=%WinSources_Path%Recovery\"

	set "ORiginal_Recovery_Path=%Recovery_Path%%WinVersion%\ORiginal\Recovery\"
	set "IRST_Recovery_Path=%Recovery_Path%%WinVersion%\IRST\Recovery\"

	set "ORiginal_WinreWim=%ORiginal_Recovery_Path%Winre.wim"
	set "IRST_WinreWim=%IRST_Recovery_Path%Winre.wim"

	dir "%Recovery_Path%"
	@echo off
	pause
	exit /b

:DiskPartition
	echo|set /p="[45m"
	echo DiskPartition[0m
	echo|set /p="[0m"

	rem ====	mountvol.exe
	if exist S:\ (
		mountvol.exe S: /d
	)

	if exist R:\ (
		mountvol.exe R: /d
	)

	if exist W:\ (
		mountvol.exe W: /d
	)

	rem ====	diskpart.exe
	set "DiskPart_txt=Disk%Disk_No%_%Disk_Name%_boot.txt"
	diskpart.exe /s "%DiskPart_txt%"

	exit /b

:WinBoot
	echo|set /p="[45m"
	echo WinBoot[0m
	echo|set /p="[0m"

	if exist W:\Windows\System32\bootsect.exe (
		cd /D W:\Windows\System32\
		echo W:\Windows\System32\bootsect.exe /nt60  S:
		W:\Windows\System32\bootsect.exe /nt60  S:
	)

	rem ====	bcdboot.exe
	rem 2025/02/27  W 05:56           166,784 bcrypt.dll
	rem 2023/05/05  U 08:23           147,800 bcrypt.dll
	rem == Copy boot files to the System partition ==
	if %SystemDrive% EQU X: (
		if exist W:\Windows\System32\bcdboot.exe (
			echo W:\Windows\System32\bcdboot.exe W:\Windows /s S:
			W:\Windows\System32\bcdboot.exe W:\Windows /s S:
		)
	)

	if %SystemDrive% EQU C: (
		if exist %Windir%\System32\bcdboot.exe (
			if exist W:\Windows (
				echo %Windir%\System32\bcdboot.exe W:\Windows /s S:
				%Windir%\System32\bcdboot.exe W:\Windows /s S:
			)
		)
	)
	exit /b

:WinRecovery
	echo|set /p="[45m"
	echo WinRecovery[0m
	echo|set /p="[0m"
	if exist "%IRST_WinreWim%" (
		call :RunRoboCopy "%IRST_Recovery_Path%" R:\Recovery\WindowsRE\
	) else (
		if exist "%ORiginal_WinreWim%" (
			call :RunRoboCopy %ORiginal_Recovery_Path% R:\Recovery\WindowsRE\
		)
	)
	exit /b

:WinReAgentc
	echo|set /p="[45m"
	echo WinReAgentc[0m
	echo|set /p="[0m"
	rem ====	ReAgentc.exe
	rem == Register the location of the recovery tools ==
	if exist R:\Recovery\WindowsRE\Winre.wim (
		if %SystemDrive% EQU X: (
			echo %SystemDrive%\Windows\System32\ReAgentc.exe
			echo W:\Windows\System32\ReAgentc.exe /Setreimage /Path R:\Recovery\WindowsRE /Target W:\Windows
			W:\Windows\System32\ReAgentc.exe /Setreimage /Path R:\Recovery\WindowsRE /Target W:\Windows
			rem == Verify the configuration status of the images. ==
			W:\Windows\System32\ReAgentc.exe /Info /Target W:\Windows
		)

		if %SystemDrive% EQU C: (
			echo %Windir%\System32\ReAgentc.exe
			echo %Windir%\System32\ReAgentc.exe /Setreimage /Path R:\Recovery\WindowsRE /Target W:\Windows
			%Windir%\System32\ReAgentc.exe /Setreimage /Path R:\Recovery\WindowsRE /Target W:\Windows
			rem == Verify the configuration status of the images. ==
			%Windir%\System32\ReAgentc.exe /Info /Target W:\Windows
		)
	)
	exit /b

:OOBE
	echo|set /p="[45m"
	echo OOBE[0m
	echo|set /p="[0m"
	rem ====	OOBE
	if exist W:\Windows\System32\Config\Software (
	@echo on
		reg load HKLM\OFFLINE  W:\Windows\System32\Config\Software
		reg add HKLM\OFFLINE\Microsoft\Windows\CurrentVersion\OOBE /v BypassNRO /t REG_DWORD /d 1 /f
		reg unload HKLM\OFFLINE
		@echo off
	)
	exit /b

:RunRoboCopy
	set "p1=%~1"
	set "p2=%~2"
	set "p3=%~3"
	set "p4=%~4"
	echo robocopy %p1%  "%p2%\" %p3% %p4% /NJH /NJS /NDL
	robocopy %p1%  "%p2%\" %p3% %p4% /NJH /NJS /NDL
	exit /b

:Checkdir
	rem ====----	https://stackoverflow.com/questions/18639663/how-to-pass-command-line-parameters-with-space-in-batch-file

	if exist "%*" (
		exit /b
	)
	if not exist "%*" (
		mkdir "%*"
		exit /b
	)
	if exist "%*" (
		echo %* Created
		exit /b
	)
	exit /b

:IsAdmin
	reg.exe query "HKU\S-1-5-19\Environment" >nul
	if not %ERRORLEVEL% EQU 0 (
		echo uHtκ޲zv
		echo You must have administrator rights to continue ...
		pause & exit
	)
	exit /b

:End
	cd /D "%Driver%%FilePath%"
	pause
	exit
	%ComSpec% /k