#!/bin/sh

. ./settings.sh

WGET="-nv -nc"
Z7="-aos"

if ! [ -e ./temp ]; then
 mkdir -p ./temp
fi

echo "--- FreeDOS -- ---------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------"
CZURL="https://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/distributions/1.3/official/FD13-LiveCD.zip"
FILENAME=fdfullcd.zip

if ! [ -e ./temp/$FILENAME ]; then
  echo "Downloading FreeDOS..."
   wget $WGET -O ./temp/$FILENAME $CZURL
fi
if [ -e ./temp/$FILENAME ]; then
  echo "Preparing FreeDOS..."
   7z x $Z7 -o${TFTP}/free ./temp/${FILENAME}
fi

echo "--- Clonezilla ---------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------"
CZURL="https://sourceforge.net/projects/clonezilla/files/clonezilla_live_stable/3.0.3-22/clonezilla-live-3.0.3-22-amd64.iso/download"
FILENAME=clonezilla-64bit.iso

if ! [ -e ./temp/$FILENAME ]; then
  echo "Downloading Clonezilla..."
   wget $WGET -O ./temp/$FILENAME $CZURL
fi
if [ -e ./temp/$FILENAME ]; then
  echo "Preparing Clonezilla..."
   mkdir -p ${TFTP}/free/cz
   7z x $Z7 -o${TFTP}/free/cz ./temp/${FILENAME}
fi

echo "--- Rescuezilla --------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------"
RZURL="https://github.com/rescuezilla/rescuezilla/releases/download/2.4.2/rescuezilla-2.4.2-64bit.jammy.iso"
FILENAME=rescuezilla-64bit.iso

if ! [ -e ./temp/$FILENAME ]; then
  echo "Downloading Rescuezilla..."
   wget $WGET -O ./temp/$FILENAME $RZURL
fi
if [ -e ./temp/$FILENAME ]; then
  echo "Preparing Rescuezilla..."
   mkdir -p ${TFTP}/free/rz
   7z x $Z7 -o${TFTP}/free/rz ./temp/${FILENAME}
fi

echo "--- Knoppix V9.1 ------------------------------------------------------------------"
echo "------------------------------------------------------------------------------------"
KNX9URL="http://ftp.uni-kl.de/pub/linux/knoppix-dvd/KNOPPIX_V9.1DVD-2021-01-25-DE.iso"
FILENAME=KNOPPIX_V9.1DVD.iso

if ! [ -e ./temp/$FILENAME ]; then
  echo "Downloading Knoppix..."
   wget $WGET -O ./temp/$FILENAME $KNX9URL
fi
if [ -e ./temp/$FILENAME ]; then
  echo "Preparing Knoppix..."
   mkdir -p ${TFTP}/free/knx
   7z x $Z7 -o${TFTP}/free/knx ./temp/${FILENAME}
   cp ./resources/knx-miniroot.gz ${TFTP}/free/knx/boot/isolinux/miniroot.gz
fi

echo "--- Debian Installer ---------------------------------------------------------------"
echo "------------------------------------------------------------------------------------"
DISTS="bullseye bookworm"
ARCHS="amd64 i386"

for DIST in $DISTS;
do
 for ARCH in ${ARCHS};
 do
  DEBURL="https://deb.debian.org/debian/dists/${DIST}/main/installer-${ARCH}/current/images/netboot/netboot.tar.gz"
  FILENAME="netboot-${DIST}-${ARCH}.tar.gz"
  if ! [ -e ./temp/${FILENAME} ]; then
    echo "Downloading Debian installer ${DIST}/${ARCH}..."
    wget $WGET -O ./temp/${FILENAME} $DEBURL
  fi
  if [ -e ./temp/${FILENAME} ]; then
    echo "Create directory in ${TFTP}/free/deb-${DIST}..."
     mkdir -p ${TFTP}/free/deb-${DIST}
    echo "Unpacking files..."
     tar xfzC ./temp/${FILENAME} ${TFTP}/free/deb-${DIST}
  fi
 done
done

./configure-debian-system.sh
