#!/bin/sh
#
# Regenerate Debian Edu ltsp.ipxe configuration
#
# This needs to take into account:
#   - LTSP images
#   - Debian Installer images

set -e

# Grab dist value for both testing and stable release cases.
if grep -q / /etc/debian_version ; then
	# Likely a Debian testing/unstable system this script is executed on.
	dist_codename=$(cat /etc/debian_version | cut -d/ -f1)
	dist_version=${dist_codename}
else
	# A stable release of Debian.
	dist_codename=$(lsb_release -sc)
	dist_version=$(lsb_release -sr)
fi

[ "$archs" ]      || archs="amd64"
[ "$theme" ]      || theme="$(ls -L /etc/alternatives/desktop-theme/plymouth 2>/dev/null | grep script | cut -d'.' -f 1)"

if [ -f /etc/debian-edu/config ] ; then
	. /etc/debian-edu/config
fi

tftpdir=/srv/tftp

# Start from a clean state after any configuration changes have been made.
if [ -d $tftpdir/ltsp ] ; then
	rm -rf $tftpdir/debian-edu
	rm -f $tftpdir/ltsp/ltsp.ipxe
	# re-generate clean ipxe menu file in case of LTSP client support.
	# FIXME: also check if chroot folders exist!!!
	if [ -d /srv/ltsp/images ] ; then
		ltsp ipxe
	fi
fi

[ -d $tftpdir ] || mkdir $tftpdir
[ -d $tftpdir/debian-edu ] || mkdir $tftpdir/debian-edu

if [ -d "/srv/tftp/debian-installer/${dist_version}" ]; then
	di_versions_supported="$(cd /srv/tftp/debian-installer/; ls -1 | grep -E '^[0-9]+$' | sort -r -h)"
else
	di_versions_supported=""
fi

menufile=$tftpdir/debian-edu/install.cfg
echo "Generating $menufile"
(
	if [ true = "$graphicdi" ]; then
		gtkvideo="vga=788"
	fi
	for arch in $archs ; do
		for di_ver in ${di_versions_supported}; do
			if [ -d "${tftpdir}/debian-installer/${di_ver}/${arch}" ]; then
				cat <<EOF

# Based upon locale, keymap and desktop values used during main-server installation; auto URL added.
:${arch}_${di_ver}
set params auto url=http://www/debian-edu-install.dat hostname=$hostname domain=$domain $installconfig $gtkvideo --- quiet
kernel /debian-installer/${di_ver}/${arch}/linux initrd=initrd.gz \${params}
initrd /debian-installer/${di_ver}/${arch}/initrd.gz
boot || goto failed
EOF
			fi
		done
	done
) > $menufile

if [ ! -z "$theme" ] ; then
	cp /usr/share/pixmaps/$theme-syslinux.png $tftpdir/debian-edu/debian-edu-pxe.png
fi

# Generate/modify the iPXE menu file
if [ -n "${di_versions_supported}" ] && [ -f $tftpdir/ltsp/ltsp.ipxe ] ; then
	# ltsp is installed already, modify existing ipxe menu
	if ! grep -q main-server $tftpdir/ltsp/ltsp.ipxe ; then
		echo "Modifying $tftpdir/ltsp/ltsp.ipxe"

		# Configure background picture for iPXE versions that support that.
		sed -i ${tftpdir}/ltsp/ltsp.ipxe \
		    -e "$(grep -n -E '^# Configure iPXE for LTSP$' ${tftpdir}/ltsp/ltsp.ipxe | tail -1 | cut -f1 -d":") a \\
\\
console --picture /debian-edu/debian-edu-pxe.png ||\\
"

		# Support server OS's version of the Debian Installer, but also earlier / later versions.
		sed -i ${tftpdir}/ltsp/ltsp.ipxe \
		    -e "$(grep -n -E "^item --key [0-9]+ .*" ${tftpdir}/ltsp/ltsp.ipxe | tail -1 | cut -f1 -d":") a item\\
item --gap                        Installation:\\
item --key i amd64_${dist_version} Install Debian Edu ${dist_version} / amd64\\
# BEGIN: Debian-Installer other versions\\
# END: Debian-Installer other versions"
		cat $menufile >> $tftpdir/ltsp/ltsp.ipxe
	fi
elif [ -n "${di_versions_supported}" ]; then
	# generate ipxe menu on a plain main server for PXE installations
	mkdir -p $tftpdir/ltsp/
	cp /usr/lib/ipxe/undionly.kpxe $tftpdir/ltsp/
	cp /usr/lib/ipxe/snponly.efi $tftpdir/ltsp/
	cp /boot/memtest86+ia32.bin $tftpdir/ltsp/memtest.0
	cp /boot/memtest86+x64.efi $tftpdir/ltsp/memtest.efi
	echo "Generating $tftpdir/ltsp/ltsp.ipxe"
	cat <<EOF > ${tftpdir}/ltsp/ltsp.ipxe
#!ipxe
#
# Configure iPXE for network installations

console --picture /debian-edu/debian-edu-pxe.png ||

# Set the default image (img) based on arch, or to root-path if it's not empty
cpuid --ext 29 && set img amd64_${dist_version} || set img i386_${dist_version}

goto start

:start
# To completely hide the menu, set menu-timeout to -1
isset \${menu-timeout} || set menu-timeout 5000
iseq "\${menu-timeout}" "-1" && goto \${img} ||
menu Debian Edu iPXE boot menu || goto \${img}
item
item --gap                        Debian Edu installation:
item --key i amd64_${dist_version} Install Debian Edu ${dist_version} / amd64
item
# BEGIN: Debian-Installer other versions
# END: Debian-Installer other versions
item
item --gap                        Other options:
item --key m memtest              Memory test
item --key c config               Enter iPXE configuration
item --key s shell                Drop to iPXE shell
item --key d disk                 Boot from the first local disk
item
item --key x exit                 Exit iPXE and continue BIOS boot
choose --timeout \${menu-timeout} --default \${img} img || goto cancel
goto \${img}

:memtest
iseq \${platform} pcbios && kernel memtest.0 || kernel memtest.efi
# Boot "fails" on normal memtest exit with Esc, so show the menu again
boot ||
goto start

:config
config
goto start

:shell
echo Type 'exit' to get back to the menu
shell
goto start

:disk
# Boot the first local HDD
sanboot --no-describe --drive 0x80 || goto failed

:exit
exit 1

:cancel
echo You cancelled the menu, dropping to a shell
goto shell

:failed
echo Booting failed, dropping to a shell
goto shell
EOF
	if ! grep -q main-server $tftpdir/ltsp/ltsp.ipxe ; then
		cat $menufile >> $tftpdir/ltsp/ltsp.ipxe
	fi
fi

# Insert other Debian-Installer versions, if any.
if [ -n "${di_versions_supported}" ] && [ "${dist_version}" != "${di_versions_supported}" ]; then
	# We have more D-I netboot versions installed, so inject them into the iPXE boot menu
	for di_ver in ${di_versions_supported}; do
		for arch in ${archs}; do
			if [ "${di_ver}" = "${dist_version}" ]; then
				# we already have this, see above
				:
			else
				if [ -d $tftpdir/debian-installer/${di_ver}/${arch} ]; then
					sed -i "/^# END: Debian-Installer other versions/i \
item ${arch}_${di_ver}         Install Debian Edu ${di_ver} / ${arch}\
" ${tftpdir}/ltsp/ltsp.ipxe
				fi
			fi
		done
	done
else
	sed -E -i "${tftpdir}/ltsp/ltsp.ipxe" -e "/# (BEGIN|END): Debian-Installer other versions/d"
fi

# Make sure /srv/tftp/ltsp/ltsp.ipxe is Debian Edu specific.
sed -i 's#ltsp/ltsp.img#ltsp/${img}/ltsp.img#' /srv/tftp/ltsp/ltsp.ipxe
# Add "quiet" and "splash" parameters to hide kernel logs on a boot screen.
sed -i 's/set cmdline ${cmdline_method} ${cmdline_ltsp} ${cmdline_client}$/set cmdline ${cmdline_method} ${cmdline_ltsp} ${cmdline_client} quiet splash/' /srv/tftp/ltsp/ltsp.ipxe
