@echo off
:: =================================
::	ApplyImage.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 ====	基本系統設定 set 一定要加""避免空白
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

call :ApplyImage

call :Intel_RST

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 "sources_Path=%WinSources_Path%%WinVersion%\sources\"

	set "Drivers_Path=%WinSources_Path%Drivers\Extract\"
	set "Packages_Path=%WinSources_Path%Packages\"
	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"

	set "install_wim=%sources_Path%install.wim"
	set "NetFx3_Path=%sources_Path%sxs\"

	set "CreatePartitions_Path=%WinSources_Path%CreatePartitions\"
	set "Downloads_Path=%WinSources_Path%Drivers\Downloads\"

	rem ====	Check File Path
	call :Checkdir %sources_Path%
	call :Checkdir %Drivers_Path%
	call :Checkdir %Packages_Path%
	call :Checkdir %Recovery_Path%
	call :Checkdir %oobe_Path%

	call :Checkdir %ORiginal_Recovery_Path%
	call :Checkdir %IRST_Recovery_Path%

	call :Checkdir %Packages_Path%%WinVersion%\update\
	call :Checkdir %NetFx3_Path%

	call :Checkdir %CreatePartitions_Path%
	call :Checkdir %Downloads_Path%

	@echo on
	dir "%install_wim%"
	dir "%Drivers_Path%"
	dir "%Packages_Path%"
	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

:ApplyImage
	echo|set /p="[45m"
	echo ApplyImage[0m
	echo|set /p="[0m"
	rem == ApplyImage.bat ==
	rem == Set high-performance power scheme to speed deployment ==
	call powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c

	rem == Apply the image to the Windows partition ==
	rem ==-- dism /Apply-Image /ImageFile:%1 /Index:1 /ApplyDir:W:\

	if not exist W:\Windows\System32 (
		if exist "%install_wim%" (
			rem ==++
			dism /Apply-Image /ImageFile:"%install_wim%" /Index:1 /ApplyDir:W:\
			rem ==++ Drivers
			Dism /Image:W:\ /Add-Driver /Driver:%Drivers_Path% /Recurse
		) else (
			echo.
			echo [91mDownload Windows first[0m
			echo [44mhttps://www.microsoft.com/zh-tw/software-download/windows11[0m
			pause
			exit /b
		)
	) else (
		echo [91mWindows is installed[0m
	)
	exit /b

:Intel_RST
	echo|set /p="[45m"
	echo Intel_RST[0m
	echo|set /p="[0m"
	rem ====	Intel_RST
	@echo off
	if exist W:\Windows\System32\Recovery\Winre.wim (

		call :Checkdir %Driver%\mnt\

		call :RunRoboCopy W:\Windows\System32\Recovery\ "%ORiginal_Recovery_Path%"
		call :RunRoboCopy W:\Windows\System32\Recovery\ "%IRST_Recovery_Path%"

		Dism /Mount-Image /ImageFile:"%IRST_WinreWim%"  /Index:1 /MountDir:%Driver%\mnt
		Dism /Image:%Driver%\mnt /Add-Driver /Driver:%Drivers_Path% /Recurse
		Dism /Unmount-Image /MountDir:%Driver%\mnt /Commit

		rmdir /Q %Driver%\mnt\
	)

	exit /b

:NetFx3
	echo|set /p="[45m"
	echo Framework v3.5[0m
	echo|set /p="[0m"
	rem if not exist W:\WINDOWS\Microsoft.NET\Framework\v3.5 (
		echo NetFx3
		rem cd /D "%NetFx3_Path%"

		For /F "tokens=* usebackq"  %%I in ('dir "%NetFx3_Path%Microsoft-Windows-NetFx3*.cab" /B ') do (

			if exist "%NetFx3_Path%" (
				echo DISM /Image:W:\ /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:%NetFx3_Path%
				DISM /Image:W:\ /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:%NetFx3_Path%
			)
		)
	rem ) else (
		dir W:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe
	rem )

	exit /b

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

	if exist W:\Windows\System32\Recovery\Winre.wim (
		if %SystemDrive% EQU C: (
			echo update %SystemDrive%
			cd /D %Packages_Path%%WinVersion%\update\
			For /F "tokens=* delims="  %%I in ('dir %Packages_Path%%WinVersion%\update\*.msu /B ') do (
				echo %Packages_Path%%WinVersion%\update\%%I
				if exist %Packages_Path%%WinVersion%\update\%%I (
					echo Dism /Image:W:\ /LogPath:AddPackage.log /Add-Package /PackagePath:%Packages_Path%%WinVersion%\update\%%I
					Dism /Image:W:\ /LogPath:AddPackage.log /Add-Package /PackagePath:%Packages_Path%%WinVersion%\update\%%I
				)
			)
		)
	)
	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  上午 05:56           166,784 bcrypt.dll
	rem 2023/05/05  下午 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 必須「以系統管理員身分執行」
		echo You must have administrator rights to continue ...
		pause & exit
	)
	exit /b

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