#!/bin/sh
# postinst script for #PACKAGE#

set -e

case "$1" in
    configure)
	cp -a /usr/share/tuxedo/iPXE.efi /boot/efi/EFI/BOOT/
	cp -af /usr/share/tuxedo/42_custom /etc/grub.d
	rm -f /etc/grub.d/43_custom 2>/dev/null
	rm -f /etc/grub.d/44_custom 2>/dev/null
	update-grub
	if [ -e /boot/efi ]; then
	    device=$(lsblk -lf | awk '/efi/{print $1}')
	    if [ $(echo $device | grep nvm) ]; then
		disk="${device%??}"
		part=$(expr substr "$device" \( length "$device" \) 1)
	    else
		disk="${device%?}"
		part=$(expr substr "$device" \( length "$device" \) 1)
	    fi
	    if [ -z $(efibootmgr -v | awk '/iPXE/{print $1}' | sed 's/Boot//' | sed 's/\*//') ]; then
		efibootmgr --create-only --disk /dev/$disk --part $part --label "iPXE WebFAI (wired LAN required)" --loader \\EFI\\BOOT\\iPXE.efi
		order=$(efibootmgr -v | awk '/BootOrder/{print $2}')
		efinum=$(efibootmgr -v | awk '/iPXE/{print $1}' | sed 's/Boot//' | sed 's/\*//')
		efibootmgr -o ${order},${efinum}
	    fi
	fi
	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
	;;
    abort-upgrade|abort-remove|abort-deconfigure)
	;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#

exit 0
