#!/bin/sh

. ./settings.sh

# Download & Install Debian Packages
apt update
apt install -y samba genisoimage wimtools cabextract

# configure Packages
cp ./resources/smb.conf /etc/samba/

# setup dirs:
mkdir -p ./non-free/windows
mkdir -p ./temp/waik

# restart services
systemctl restart smbd

# Download Windows Stuff:
WAIKURL="https://download.microsoft.com/download/8/E/9/8E9BBC64-E6F8-457C-9B8D-F6C9A16E6D6A/KB3AIK_EN.iso"
FILENAME="./temp/KB3AIK_EN.iso"
if ! [ -e $FILENAME ]; then
  wget -nc -O $FILENAME $WAIKURL
fi

if [ -e $FILENAME ]; then
  7z x -aos -o./temp/waik $FILENAME
fi

# parse winpestart.bat
cat ./resources/winpestart.bat | sed -e "s/__NFSSERVERIP__/${NFSSERVERIP}/g" > ./temp/winpestart.bat

# Create winpe.iso
if [ -e ./non-free/windows/winpe.iso ]; then
 rm ./non-free/windows/winpe.iso
fi
if [ -e ./temp/waik/WinPE.cab ]; then
 mkwinpeimg --iso --arch=amd64 --waik-dir=./temp/waik --start-script=./temp/winpestart.bat ./non-free/windows/winpe.iso
 #mkwinpeimg --iso --arch=amd64 --waik-dir=./temp/waik ./non-free/windows/winpe.iso
fi

# download Deployment Toolkit
DLURL="https://download.microsoft.com/download/3/3/9/339BE62D-B4B8-4956-B58D-73C4685FC492/MicrosoftDeploymentToolkit_x64.msi"
FILENAME="./non-free/windows/MicrosoftDeploymentToolkit_x64.msi"
if ! [ -e $FILENAME ]; then
  wget -nc -O $FILENAME $DLURL
fi

# copy files to tftp server
cp -av ./non-free/windows ${TFTP}/non-free/

