#!/bin/sh

set -e

case "$1" in
	remove|purge)
		rm -f /boot/efi/EFI/BOOT/iPXE.efi
		if [ -f /etc/grub.d/42_custom ]; then
		    rm -f /etc/grub.d/42_custom
		else
		    if [ -f /etc/grub.d/43_custom ]; then
		    rm -f /etc/grub.d/43_custom
		    else
		    rm -f /etc/grub.d/44_custom
		    fi
		fi
		update-grub
		if [ -f /usr/bin/ipxe-workaround.sh ]; then
		    rm -f /usr/bin/ipxe-workaround.sh
		    systemctl disable --now ipxe-workaround.service
		    rm -f /etc/systemd/system/ipxe-workaround.service
		    systemctl daemon-reload
		fi
		if [ $(efibootmgr -v | awk '/FAI/{print $1}' | sed 's/Boot//' | sed 's/\*//') ]; then
		    efibootmgr -B -b $(efibootmgr -v | awk '/FAI/{print $1}' | sed 's/Boot//' | sed 's/\*//')
		fi
		;;
	upgrade|failed-upgrade|disappear)
		;;
	abort-install|abort-upgrade)
		;;
	*)
		echo "$0 called with unknown argument \`$1'" 1>&2
		exit 1
		;;
esac

exit 0
