@echo off
rem this script is doing most of the work ...

set T=%SystemDrive%\TEMP
set TOOLS=%SystemDrive%\tools
set SCRIPTS=%TOOLS%\scripts
set LOG=%SCRIPTS%\autoconfig.txt
set SCRIPT_SETTINGS=%TOOLS%\scripts\settings.cmd
set COMPANY_SETTINGS=%TOOLS%\company\settings.cmd

del /F /Q %LOG% 1>nul 2>nul
if not "%1"=="STDOUT_TO_FILE" %0 STDOUT_TO_FILE %* 1>%LOG% 2>&1
shift /1

rem show window with logfile while running scripts ...
start %TOOLS%\logmonitor.exe %LOG%

rem ===================
rem DEFAULTS
rem ===================

set LANG=en-US
set windows_updates=0
set windows_targetrelease=2009
set logon_logo=0
set install_firefox=0
set install_aerolite=0
set bgcol_R=3
set bgcol_G=131
set bgcol_B=135
set explorer_quick_access=0
set preserve_savedgames_folder=1
set searchdomains=localdomain,fritz.box
set gitserver=github.com
set gitrepo=my_repo_at_github
set start_desktopinfo=0
set use_nettime=0

echo ####### %0 #######
date /t
time /t
echo.

rem ===================
rem CONFIGFILE CHECKS
rem ===================

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

rem show all the settings ...
echo.
echo ++++++++++++++++++
echo LANG=%LANG%
echo windows_updates=%windows_updates%
echo windows_targetrelease=%windows_targetrelease%
echo logon_logo=%logon_logo%
echo install_firefox=%install_firefox%
echo install_aerolite=%install_aerolite%
echo bgcol_R=%bgcol_R%
echo bgcol_G=%bgcol_G%
echo bgcol_B=%bgcol_B%
echo explorer_quick_access=%explorer_quick_access%
echo preserve_savedgames_folder=%preserve_savedgames_folder%
echo searchdomains=%searchdomains%
echo gitserver=%gitserver%
echo gitrepo=%gitrepo%
echo start_desktopinfo=%start_desktopinfo%
echo set use_nettime=%use_nettime%
echo ++++++++++++++++++
echo.

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

echo creating %T% ...
mkdir %T% 1>nul 2>nul

echo cd to %SCRIPTS% ...
cd /D %SCRIPTS%

echo #######################
echo ### UNPACK and COPY ###
echo #######################
echo.

rem 7-zip is essential for other scripts ...
call unpack-7zip.cmd
echo.

rem put vnc in place ...
call unpack-vnc.cmd
echo.

rem unpack all the other zipfiles ...
call unpack-zipfiles.cmd
echo.

rem copy some other stuff ...
call copy-executables.cmd
echo.

rem unpack powershell packages and register them ...
call unpack-pspackages.cmd
echo.

echo #######################
echo ### MODIFICATIONS   ###
echo #######################
echo.

rem block updates - this must be done EARLY!
if %windows_updates% == 0 (
  call disable-updates.cmd 
  echo.
)

echo allow execution of any powershell scripts ...
powershell -Command "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope LocalMachine"
powershell -Command ^
"$T=Get-ExecutionPolicy ; if ($T -eq 'Bypass') { ^
  write-host 'POWERSHELL: Execution Policy was set to BYPASS' ^
} ^
  else { ^
  write-host 'ERROR: CANNOT SET Execution Policy to BYPASS !' ^
}"
echo.

echo enabling OS feature "NetFx3" ...
dism /online /enable-feature /featurename:NetFx3 /All /Source:%WINDIR%\sources\sxs /LimitAccess /NoRestart
echo.

call modify-path.cmd 
echo.

call modify-windowsupdate.cmd
echo.

call modify-explorer.cmd 
echo.

call modify-powersettings.cmd
echo.

call modify-taskbar.cmd
echo.

if %use_nettime% == 1 (
  call install-nettime.cmd
  echo.
) else (
  call modify-timeservers.cmd
  echo.
)

call modify-searchdomains.cmd
echo.

rem optimize the TCP stack for datacenter use!
echo ####### tcpoptimize.ps1 #######
powershell -Command .\tcpoptimize.ps1
echo ####### tcpoptimize.ps1 #######
rem cleanup (we don't need the logfile backup) ...
deltree %Systemdrive%\BACKUP
echo.

echo #######################
echo ### UNINSTALL tasks ###
echo #######################
echo.

rem REMOVE all STOCK apps!
powershell -command .\uninstall-apps.ps1
echo.

echo #####################
echo ### INSTALL tasks ###
echo #####################
echo.

rem must be done early, because it cleans the "Tools" menu ...
call install-shortcuts.cmd
echo.

call install-tweaks.cmd
echo.

call install-git.cmd
echo.

call install-vcredist.cmd
echo.

powershell -command .\install-windowsterminal.ps1
echo.

call install-openshell.cmd
echo.

if %install_firefox% == 1 (
  call install-firefox.cmd
  echo.
)

call install-win32diskimager.cmd
echo.

call install-irfanview.cmd
echo.

rem INSTALL the APPS WE want to have!
call install-apps.cmd
echo.

call mkinstall-links.cmd
echo.

echo #####################
echo ### DISABLE tasks ###
echo #####################
echo.

call disable-automaticactivation.cmd
echo.

call disable-accountpicture.cmd 
echo.

call disable-logonbackground.cmd 
echo.

call disable-meetnow.cmd
echo.

call disable-autologon.cmd
echo.

call disable-systemproxy.cmd
echo.

powershell -command .\disable-soundscheme.ps1
echo.

rem stop all the beeping ...
sc config beep start=disabled

echo #####################
echo ### CLEANUP tasks ###
echo #####################
echo.

rem this must be done as late as possible ... stupid os problem #1
powershell -Command .\cleanup-tiles.ps1
echo.

rem ... stupid os problem #2
call cleanup-startmenu.cmd
echo.

rem ... stupid os problem #3
call cleanup-programlist.cmd
echo.

rem ... stupid os problem #4
call unpin-taskbar-icons.cmd
echo.

rem ... remove stupid public folders
call cleanup-publicdir.cmd
echo.

echo add read access rights to all users on public icons ...
icacls %PUBLIC%\Desktop\*.lnk /grant Users:R
echo.

echo removing read-only flag on tools folder ...
attrib /S /D -R %TOOLS%\*
echo.

echo removing unwanted printers ...
call remove-printer.cmd "Microsoft XPS Document Writer"
call remove-printer.cmd "Fax"
echo.

echo #####################
echo ### FINALIZING    ###
echo #####################
echo.

rem enable updates again (doing it as late as possible) ...
if %windows_updates% == 1 (
  call enable-updates.cmd 
  echo.
)

rem should be done as late as possible ...
call install-logonscript.cmd
echo.

rem LAST action!
echo setting permissions for [ %TOOLS% ] ...
cd /D %SystemDrive%\
takeown /F %TOOLS% /R /A /D Y 1>nul
icacls %TOOLS% /inheritance:r
icacls %TOOLS% /grant Administrators:F /t /c /l 1>nul
icacls %TOOLS% /grant Users:RX /t /c /l 1>nul
echo.

date /t
time /t
echo ####### %0 #######
echo READY.
echo.

echo sleep 5 seconds ...
ping 127.0.0.1 -n 5 >nul 2>&1

echo killing logmonitor ...
taskkill /f /im logmonitor.exe

echo killing explorer ...
taskkill /f /im explorer.exe

echo asking the user to remove the USB stick ...
prompt-user "+++ INSTALLATION FINISHED. +++ Please REMOVE the installation MEDIA (USB Stick) NOW! IMPORTANT HINT: Disable the update blocker to make the Microsoft Store work!"

echo rebooting ...
shutdown -g -t 0

