#!/usr/bin/env bash
# Debian ISO Remastering for DanOS - https://github.com/ITmail-dk/qmade/
# sudo apt install -y wget git xorriso isolinux p7zip-full fakeroot binutils

# DVD ISO Base offline
#DEBIAN_ISO_URL=https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/
#GET_ISO_NAME=$(curl -s https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/ | grep -oP 'debian-\d+(?:\.\d+)+-amd64-DVD-1\.iso' | head -n 1)

# NetInstall ISO
DEBIAN_ISO_URL=https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/
GET_ISO_NAME=$(curl -s https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/ | grep -oP 'debian-\d+(?:\.\d+)+-amd64-netinst\.iso' | head -n 1)
#
PRESEED_ISO_NAME=DanOS-Debian
#WORK_DIR=$(pwd) #BRUGES IKKE MERE
ISO_WORK_TMP=iso-extract

# Setup the environment for creating the new ISO version.
function setup() {
  sudo apt update && sudo apt install -y wget git xorriso isolinux p7zip-full fakeroot binutils
  if [ -f source-debian.iso ]; then
    rm source-debian.iso
  fi
  if [ ! -f source-debian.iso ]; then
    wget -O source-debian.iso $DEBIAN_ISO_URL/"$GET_ISO_NAME"
  fi
  xorriso -osirrox on -indev source-debian.iso -extract / $ISO_WORK_TMP
  sudo chmod -R +w $ISO_WORK_TMP
  sudo cp preseed-user.cfg $ISO_WORK_TMP/preseed.cfg || exit 1
  sudo cp -rfup src/ iso-extract/ || exit 1
  sudo cp src/grub/splash.png $ISO_WORK_TMP/isolinux/splash.png || exit 1
  sudo cp install-danos.sh $ISO_WORK_TMP/install.sh || exit 1
  sudo sed -i 's/append vga/append auto=true vga/' $ISO_WORK_TMP/isolinux/gtk.cfg
  sudo sed -i '/spkgtk\.cfg/d; /spk\.cfg/d' $ISO_WORK_TMP/isolinux/menu.cfg
  sudo sed -i 's/--- quiet/--- quiet file=\/cdrom\/preseed.cfg/' $ISO_WORK_TMP/isolinux/gtk.cfg
  #sudo sed -i '0,/--- quiet/ s/--- quiet/auto=true file=\/cdrom\/preseed.cfg --- quiet/' $ISO_WORK_TMP/boot/grub/grub.cfg
  sudo sed -i '0,/--- quiet/ s/--- quiet/auto=true priority=high file=\/cdrom\/preseed.cfg --- quiet/' $ISO_WORK_TMP/boot/grub/grub.cfg
  sudo sed -i '/set theme=\/boot\/grub\/theme\/1/a set default=0' $ISO_WORK_TMP/boot/grub/grub.cfg
  sudo sed -i '/set theme=\/boot\/grub\/theme\/1/a set timeout=7' $ISO_WORK_TMP/boot/grub/grub.cfg
  if [ -f source-debian.iso ]; then
    rm source-debian.iso
  fi
  #sudo mkdir -p $ISO_WORK_TMP/var/cache/apt/archives
  #sudo apt reinstall --download-only -y -o Dir::Cache="./" -o Dir::Cache::archives="iso-extract/src/packages/" \
  #  wget curl git xdg-utils xdg-user-dirs build-essential htop dkms cmake plymouth plymouth-themes power-profiles-daemon wayland-protocols wayland-utils wlr-randr libwlroots-0.18
  #sudo chmod -R +r iso-extract/src/packages/
  make
}

# Make a new ISO file after removing the old one if it exists.
function make() {
  if [ -d $ISO_WORK_TMP ]; then
    sudo cp -rfup src/ iso-extract/ || exit 1
    sudo cp -fu install-danos.sh $ISO_WORK_TMP/install.sh || exit 1
    sudo cp preseed-user.cfg $ISO_WORK_TMP/preseed.cfg || exit 1
    if [ -f $PRESEED_ISO_NAME.iso ]; then rm $PRESEED_ISO_NAME.iso; fi
    xorriso -as mkisofs -o $PRESEED_ISO_NAME.iso -V "DANOS_DEBIAN" -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin -c isolinux/boot.cat -b isolinux/isolinux.bin -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot -isohybrid-gpt-basdat $ISO_WORK_TMP
    ls -lah ./*.iso

  else
    echo "$ISO_WORK_TMP does not exist, runing setup..."
    setup
  fi
}

function usb() {
  lsblk
  echo -en "Enter the name of the USB Disk so sda, sdb etc..: "
  read -r USB_DISK
  clear
  sudo dd bs=4M status=progress conv=fsync oflag=direct if="$(pwd)"/$PRESEED_ISO_NAME.iso of=/dev/"$USB_DISK"
  echo "ISO to USB All done."
}

# Run the function by, function_name
"$@" make
