@echo off
set W10=c:\TEMP\W10
set SOURCES=%W10%\sources
set BOOTFILE=boot.wim
set IMAGEFILE=install.wim
set IMAGEFILE2=install.swm
set SCRIPT_SETTINGS=scripts\settings.cmd
set CUSTOM_SETTINGS=custom\settings.cmd
set SNAPCONTROL_INI=custom\snapcontrol.ini

IF %1.==. GOTO USAGE

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.

echo disabling Windows defender for PATH [ %DRIVE% ] ...
powershell -inputformat none -outputformat none -NonInteractive -Command Add-MpPreference -ExclusionPath %DRIVE%
echo.

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

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

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

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

rem check for existence of install.wim in case FAT32 was chosen ...
if NOT EXIST %DRIVE%\sources\%IMAGEFILE% (
  if NOT EXIST %DRIVE%\sources\%IMAGEFILE2% (
    echo ERROR ON COPY: no %IMAGEFILE% or %IMAGEFILE2% in [ %DRIVE%\sources ] found!
    echo HINT: FAT32 cannot handle files larger 4 GB!
    exit /b
  )
)

: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 apps custom) 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 removing hidden attribute from tools folder ...
attrib -H %DRIVE%\tools 

echo.
echo removing passwords from settings.cmd config files ...
for %%A in (
  %DRIVE%\tools\%SCRIPT_SETTINGS%
  %DRIVE%\tools\%CUSTOM_SETTINGS%
) do (
  if EXIST %%A (
    tools\searchreplace %%A "^.*SET.*LOCALADMIN_PASSWORD.*=.*" "set localadmin_password=DELETED"
  )
)

echo.
echo removing mail password from snapcontrol.ini
if EXIST %DRIVE%\tools\%SNAPCONTROL_INI% (
  tools\searchreplace %DRIVE%\tools\%SNAPCONTROL_INI% "^.*MAILPASS.*=.*" "MailPass = DELETED"
)

echo.
echo CLEANUP ...
del /F /Q %DRIVE%\tools\custom\ftpsettings.cmd
del /F /Q %DRIVE%\Auto-Saved*.*
del /F /Q %DRIVE%\NTLite.log

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

GOTO END

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

:END

