#! /bin/sh
set -e

. /usr/lib/grub/grub-mkconfig_lib

# iPXE is only supported on x86
case $(dpkg --print-architecture) in
	*i386|*amd64) ;;
	*) exit 0 ;;
esac

prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | sed -e "s/^/\t/")"

IPXE=/boot/ipxe.lkrn

if test -e "$IPXE" ; then
  IPXEPATH=$( make_system_path_relative_to_its_root "$IPXE" )
  # Remove the .lkrn extension
  IPXEPATH=${IPXEPATH%.lkrn}
  echo "Found iPXE image: $IPXE" >&2
  cat << EOF
menuentry "Network boot (iPXE)" --users "" --class network --id ipxe {
${prepare_boot_cache}
EOF
  save_default_entry | grub_add_tab
  cat << EOF
	if [ "\$grub_platform" = "efi" ]; then
		chainloader $IPXEPATH.efi
	else
		linux16 $IPXEPATH.lkrn
		# If the user provided an iPXE script, load it
		if [ -f $IPXEPATH.ipxe ]; then
			initrd16 $IPXEPATH.ipxe
		fi
	fi
}
EOF
fi
