#!/bin/sh
set -e

if [ -f /usr/lib/grub/grub-mkconfig_lib ]; then
  . /usr/lib/grub/grub-mkconfig_lib
  LX=linux16
elif [ -f /usr/lib/grub/update-grub_lib ]; then
  . /usr/lib/grub/update-grub_lib
  LX=linux
else
  # no grub file, so we notify and exit gracefully
  echo "Cannot find grub config file, exiting." >&2
  exit 0
fi

# We can't cope with loop-mounted devices here.
case ${GRUB_DEVICE_BOOT} in
  /dev/loop/*|/dev/loop[0-9]) exit 0 ;;
esac

# 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" )
  echo "Found iPXE image: $IPXEPATH" >&2
  cat << EOF
menuentry "Network boot (iPXE)" --users "" --class network {
${prepare_boot_cache}
    $LX $IPXEPATH
}
EOF
fi
