#!/bin/bash

#the script needs superuser rights to work correctly for now. 
#TO DO: maybe there is a way for the script to work as non-root?
[[ $UID -ne 0 ]] && exec sudo ./"$0"

#creates some folders that are used during the process. These can be changed, but needed be to changed across the script. 
mkdir /mnt/iso /opt/mintiso

#Downloads latest* mint-iso from the fh-aachen mirror. 
#TO DO: Maybe there is a way to use a more generic url here? As it stands now the url has to be changed when a new Linux Mint version is released down the line. Maybe variables are a possibility?
wget http://mirror.bauhuette.fh-aachen.de/linuxmint-cd/stable/19.3/linuxmint-19.3-cinnamon-64bit.iso

#mounts the downloaded iso into a folder. * is added as a wildcard for the version number.
mount -o loop linuxmint-*-cinnamon-64bit.iso /mnt/iso

#the contents of the iso have to be copied over to another folder, since the ISO is mounted read-only. all the steps below require root. 
cp -rT /mnt/iso/ /opt/mintiso/
cd /opt/mintiso/

#copies the computertruhe.seed from codeberg-repo so always the latest preseed-file is used. 
curl https://codeberg.org/stefan230/Setup-Skripte/raw/branch/master/mint/computertruhe.seed > /opt/mintiso/preseed/computertruhe.seed

#configures the bootloader to use the "custom" computertruhe-preseed. 
sed -i 's/linuxmint.seed/computertruhe.seed/g' isolinux/isolinux.cfg

#creates the custom-iso with the given parameters.
#TO DO: the outputpath right now is not generic enough. Maybe find a better name for the iso.  
mkisofs -D -r -V "UNATTENDED_MINT" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o /home/stefan/linuxmint-unattended.iso /opt/mintiso

#umounts the iso and clears up the space we used to create the iso.
umount /mnt/iso
cd
rm -r /mnt/iso/
rm -r /opt/mintiso/

#from here we are basically done and the iso can be "burned" on an USB ready for install. 
echo "Die Custom ISO wurde fertiggestellt und kann nun verwendet werden. Die ISO ist unter /home/stefan/linuxmint-unattended.iso zu finden."
