#!/bin/bash

# This script do the following:
#	Download Debian Stable (Stretch)
#	Download Debian Testing (Buster)
#	Download Debian oldStable (Jessie)
#	Download Debian oldoldStable (Wheezy)
#	Download Debian Unstable (Sid)
#	Make an PXE's config file (aka menu.cfg)

if command -v in.tftpd > /dev/null; then
	source /etc/default/tftpd-hpa
else
	TFTP_DIRECTORY="/srv/tftp"
fi

DEBIAN_INSTALLER_DIR="${TFTP_DIRECTORY}/installer/debian"
DEBIAN_CONFIG_PXE="${DEBIAN_INSTALLER_DIR}/menu.cfg.example"

# Create directories and config file
mkdir -p "${DEBIAN_INSTALLER_DIR}"
rm -f "${DEBIAN_CONFIG_PXE}"
touch "${DEBIAN_CONFIG_PXE}"

for DISTRO in stretch buster jessie wheezy sid; do # For ALL Debian's version
	for ARCH in amd64 i386; do            # For all classic architecture

		# Create and go into directory
		mkdir -p ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH}
		pushd ${DEBIAN_INSTALLER_DIR}/${DISTRO}/${ARCH} > /dev/null

		# Download files
		#wget http://ftp.fr.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/linux -O linux
		#wget http://ftp.fr.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/initrd.gz -O initrd.gz
		wget --quiet http://ftp.fr.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/linux -O linux
		wget --quiet http://ftp.fr.debian.org/debian/dists/${DISTRO}/main/installer-${ARCH}/current/images/netboot/debian-installer/${ARCH}/initrd.gz -O initrd.gz
		popd > /dev/null

		# Config file
		/bin/cat >> "${DEBIAN_CONFIG_PXE}" << EOF
label ${DISTRO}${ARCH}
  menu label Debian GNU/Linux ${DISTRO} ^${ARCH} bits
  kernel installer/debian/${DISTRO}/${ARCH}/linux
  append vga=normal initrd=installer/debian/${DISTRO}/${ARCH}/initrd.gz -- quiet
EOF
	done

	# Config file
	/bin/cat >> "${DEBIAN_CONFIG_PXE}" << EOF
label separator
  menu label ---
EOF
done

# Config file
/bin/cat >> "${DEBIAN_CONFIG_PXE}" << EOF
label mainmenu
  menu label ^Back...
  menu exit
EOF

exit 0
