#!/bin/bash

# This takes a d-i netboot directory and converts it from PXELINUX (for BIOS) +
# GRUB (for EFI) to just use SYSLINUX for EFI. This means that the rest of the
# configuration (pxelinux.cfg etc.) can remain the same.
#
# This requires the packages syslinux-common and syslinux-efi to be installed.

if [[ $# -eq 0 ]]; then
	echo "Usage: $0 <existing d-i netboot directory>"
	exit 1
fi

existing=$1
if [ ! -f "$existing/debian-installer/amd64/pxelinux.0" ]; then
	echo "Not a valid d-i netboot directory"
	exit 1
fi

WD=`mktemp -d`
cp -a $existing/* "$WD"/
cd "$WD"

# remove EFI GRUB (optional, to avoid confusion)
rm -rf debian-installer/amd64/grub/ debian-installer/amd64/bootnetx64.efi 

# remove BIOS pxelinux (optional, to avoid confusion)
rm -f pxelinux.0 ldlinux.c32 debian-installer/amd64/pxelinux.0

# replace syslinux's ldlinux + modules with efi64 versions
cp /usr/lib/SYSLINUX.EFI/efi64/syslinux.efi debian-installer/amd64/
rm -f debian-installer/amd64/boot-screens/*.c32
cp /usr/lib/syslinux/modules/efi64/{ldlinux.e64,libcom32.c32,libutil.c32,vesamenu.c32} debian-installer/amd64/boot-screens/
ln -s debian-installer/amd64/syslinux.efi debian-installer/amd64/boot-screens/ldlinux.e64 .

echo The new EFI netboot environment can be found at "$WD".
echo If everything looks fine, move it to /var/lib/puppet/volatile/tftpboot
