@echo off
set SOURCES=%SystemDrive%\TEMP\W10\sources
set BOOT=%SystemDrive%\TEMP\BOOT
set SCRIPT_SETTINGS=..\scripts\settings.cmd
set CUSTOM_SETTINGS=..\custom\settings.cmd
set LANG=en-US
set localadmin=localadmin
set localadmin_name=Local Administrator
set localadmin_password=

rem ===================
rem CONFIGFILE CHECK
rem ===================

echo searching for SCRIPT settings ...
if NOT EXIST %SCRIPT_SETTINGS% (
  echo INFO: config file %SCRIPT_SETTINGS% NOT FOUND
) else (
  echo OK. Loading SCRIPT settings ...
  call %SCRIPT_SETTINGS%
)
echo.

echo searching for CUSTOM settings ...
if NOT EXIST %CUSTOM_SETTINGS% (
  echo INFO: config file %CUSTOM_SETTINGS% NOT FOUND
) else (
  echo OK. Loading CUSTOM settings ...
  call %CUSTOM_SETTINGS%
)
echo.

rem ======
rem MAIN
rem ======

if NOT EXIST %BOOT%\winsetup.exe (
  echo renaming Windows setup.exe to boot image ...
  move /Y %BOOT%\setup.exe %BOOT%\winsetup.exe
  echo.
)

echo setting language in boot image ...
dism /image:%BOOT% /Set-InputLocale:%LANG% || exit /b 1
dism /image:%BOOT% /Set-SysLocale:%LANG% || exit /b 1
echo.

echo replacing setup.exe with our installer utility ...
copy /Y installer.exe %BOOT%\setup.exe
echo.

rem prepare file for unattended installation ...
echo copying config file for unattended installation ...
copy /Y autounattend_template.xml %BOOT%\autounattend.xml

rem file in users and passwords ...
..\tools\searchreplace %BOOT%\autounattend.xml ___MYUSER___ "%localadmin%"
..\tools\searchreplace %BOOT%\autounattend.xml ___MYNAME___ "%localadmin_name%"
..\tools\searchreplace %BOOT%\autounattend.xml ___MYPASS___ "%localadmin_password%" /S
..\tools\searchreplace %BOOT%\autounattend.xml ___MYLANG___ "%LANG%"
echo.

echo copying config file for disk format [ BIOS ]...
copy /Y format-BIOS.txt %BOOT%
echo copying config file for disk format [ UEFI ]...
copy /Y format-UEFI.txt %BOOT%
echo copying disk formatting script ...
copy /Y format-disk.cmd %BOOT%

rem provide snapshot64.exe ...
if EXIST ..\software\snapshot64_licensed.exe (
  echo copying LICENSED snapshot64.exe to boot image ...
  copy /Y ..\software\snapshot64_licensed.exe %BOOT%\snapshot64.exe
) else (
  echo copying STANDARD snapshot64.exe to boot image ...
  copy /Y ..\software\snapshot64.exe %BOOT%
)

echo copy netuse helper script to boot image ...
copy /Y netuse.cmd %BOOT%

echo unmounting and committing changes to %SOURCES%\boot.wim ...
dism /Unmount-Wim /MountDir:%BOOT% /Commit  || exit /b 1

