#! /bin/sh
set -e

prefix="/usr"
exec_prefix="/usr"
datarootdir="/usr/share"

. "${datarootdir}/grub2/grub-mkconfig_lib"

# iPXE is only supported on x86 and this package is only intended for 64 bit installations
if [ $(uname -m ) != x86_64 ]
then
  exit 0
fi

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

if [ -d /sys/firmware/efi ] && [ "x${GRUB_USE_LINUXEFI}" = "xtrue" ]; then
  IPXE=/boot/ipxe.efi
  IPXE_LOADER_CMD=chainloader
else
  IPXE=/boot/ipxe.lkrn
  IPXE_LOADER_CMD=linux16
fi

if test -e "$IPXE" ; then
  IPXEPATH=$( make_system_path_relative_to_its_root "$IPXE" )
  echo "Found iPXE image: $IPXE" >&2
  cat << EOF
menuentry "Network boot (iPXE)" --users "" --class network {
${prepare_boot_cache}
	$IPXE_LOADER_CMD $IPXEPATH
}
EOF
fi
