#!/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

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


if test -e /boot/memtest86+.bin ; then
  KERNELPATH=$( make_system_path_relative_to_its_root "/boot/installer-linux" )
  INITRDPATH=$( make_system_path_relative_to_its_root "/boot/installer-initrd.gz" )
  echo "Cubbli installer image: $KERNELPATH" >&2
  echo "Cubbli initrd image: $INITRDPATH" >&2
  cat << EOF
menuentry 'Text mode Cubbli 16.04 installer' {
EOF
  printf '%s\n' "${prepare_boot_cache}"
  cat << EOF
        linux $KERNELPATH debian-installer/locale=en_GB console-setup/ask_detect=false keyboard-configuration/layoutcode=fi keyboard-configuration/modelcode=pc105 netcfg/get_hostname=cubbli netcfg/choose_interface=auto preseed/url=http://cubbli.cs.helsinki.fi/install/xenial.seed --
	initrd $INITRDPATH
}
EOF
fi
