@echo off
set T=c:\temp
set W10=c:\TEMP\W10
set SOURCES=%W10%\sources
set BOOTFILE=boot.wim
set IMAGEFILE=install.wim
set SCRIPT_SETTINGS=scripts\settings.cmd
set COMPANY_SETTINGS=company\settings.cmd
set LANG=en-US
set FOURGB=4294967296
set MYUSER=
set MYNAME=
set MYPASS=

IF %1.==. GOTO USAGE

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 searching for COMPANY settings ...
if NOT EXIST %COMPANY_SETTINGS% (
  echo INFO: config file %COMPANY_SETTINGS% NOT FOUND
) else (
  echo OK. Loading COMPANY settings ...
  call %COMPANY_SETTINGS%
)

echo.
echo =================================
echo Copying all data to Media ...
echo =================================
echo.

set DRIVE=%1

if /I %DRIVE% == C: (
  echo ERROR: cannot use drive %DRIVE% !
  exit /b
)

if NOT EXIST %DRIVE% (
  echo ERROR: drive %DRIVE% not found!
  exit /b
)

echo using drive [ %DRIVE% ] ...
echo.

rem get drive type (is it fat32 ?) ...
for /f "tokens=5" %%A in ('@fsutil fsinfo volumeinfo %DRIVE%^|findstr /B "File System Name : "') do (@set DRIVETYPE=%%A)
echo INFO: DRIVETPYE = [ %DRIVETYPE% ]

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

echo.
echo IMPORTANT:
echo   How is your target media formatted?
echo     - use "exfat" for BIOS based installations only!
echo     - use "fat32" for UEFI based installations only!
echo   REASON: UEFI cannot read "exfat" filesystems and Windows setup will throw error code: 0x80042565
echo.

rem check SOURCE for files LARGER 4 GB in case FAT32 was chosen ...
if /i "%DRIVETYPE%" EQU "fat32" (
  for /f "tokens=*" %%F in ('"dir /S /B /A-D %W10%"') do if %%~zF GTR %FOURGB% (
    echo ERROR: The file [ %%F : %%~zF bytes ] is greater than 4GB in size - cannot copy this to a FAT32 filesystem!
    exit /b
  )
)

set answer=B
:ask
  set /p answer="BIOS(default) or UEFI setup (B/U)? "
  if /i "%answer:~,1%" EQU "B" (
    echo selected: BIOS
    copy /Y image\autounattend_BIOS_template.xml %DRIVE%\autounattend.xml
    GOTO CONT
  )
  if /i "%answer:~,1%" EQU "U" (
    echo selected: UEFI
    copy /Y image\autounattend_UEFI_template.xml %DRIVE%\autounattend.xml
    GOTO CONT
  )
  echo Please type B for BIOS or U for UEFI setup.
  goto ask
:CONT

rem ask for user ...
if "@%localadmin%" NEQ "@" (
  set MYUSER=%localadmin%
) else (
  echo.
  set /p MYUSER="Username (%MYUSER%)? "
)

rem ask for real name ...
if "@%localadmin_name%" NEQ "@" (
  set MYNAME=%localadmin_name%
) else (
  echo.
  set /p MYNAME="Display Name (%MYNAME%)? "
)

rem ask for a password ...
if "@%localadmin_password%" NEQ "@" (
  set MYPASS=%localadmin_password%
) else (
  echo.
  set /p MYPASS="Password (not set)? "
)

echo.
tools\searchreplace %DRIVE%\autounattend.xml ___MYUSER___ "%MYUSER%"
tools\searchreplace %DRIVE%\autounattend.xml ___MYNAME___ "%MYNAME%"
tools\searchreplace %DRIVE%\autounattend.xml ___MYPASS___ "%MYPASS%"
tools\searchreplace %DRIVE%\autounattend.xml ___MYLANG___ "%LANG%"

if NOT EXIST %SOURCES%\%BOOTFILE% (
  echo DOH! no %BOOTFILE% in [ %SOURCES% ] found
  exit /b
)

if NOT EXIST %SOURCES%\%IMAGEFILE% (
  echo DOH! no %IMAGE% in [ %SOURCES% ] found
  exit /b

)

echo copying all files in [ %W10% ] to drive %DRIVE% ...
robocopy %W10%\ %DRIVE% /MIR /256 /NDL /R:1 /J /XX

:CONT
if EXIST tools (
  echo.
  echo copying folder [ tools ] to drive %DRIVE% ...
  robocopy tools %DRIVE%\tools /COPY:DT /FFT /XO /256 /NJH /NJS /NDL /R:1 /XF .gitignore
)

if EXIST software (
  echo.
  echo copying folder [ software ] to %DRIVE%\tools ...
  robocopy software %DRIVE%\tools\software /COPY:DT /FFT /XO /MIR /256 /NJH /NJS /NDL /R:1 ^
    /XF *.iso /XF *.txt /XF *.cmd /XF *.csv /XF .gitignore /XD .git
)

for %%P in (drivers scripts source optional personal company apps) do (
  if EXIST %%P (
    echo.
    echo copying folder [ %%P ] to %DRIVE%\tools ...
    robocopy %%P %DRIVE%\tools\%%P /COPY:DT /FFT /XO /MIR /256 /NJH /NJS /NDL ^
      /R:1 /XF .gitignore /XD .git
  )
)

echo.
echo copying autoconfig starter script to %DRIVE% ...
copy /Y image\StartAutoConfig.cmd %DRIVE%\

echo.
echo putting copytools script to %DRIVE% ...
copy /Y image\CopyTools.cmd %DRIVE%\

echo.
echo ======
echo READY.
echo ======

GOTO END

:USAGE
echo "usage: %0 <DRIVE>"
echo.

:END

