#! /bin/sh
#
# Add recovery partition option to grub

set -e

. /usr/share/debconf/confmodule

GRUB_CONFIG="/target/boot/grub/menu.lst"
KOPT="auto=true priority=critical vga=normal quiet cdrom-detect/try-hdd=true file=/cdrom/install/recovery-preseed.cfg"

if [ ! -e "${GRUB_CONFIG}" ]; then
   echo "E: ${GRUB_CONFIG} not found!"
   exit 1
fi

# If oem-config is enabled, ensure we enable it when we do recovery install
db_get oem-config/enable || true
if [ "${RET}" = "true" ]
then
    KOPT="${KOPT} oem-config/enable=true"
fi

# Subtract by one since grub starts counting at zero (aka zero-indexed).
db_get recovery-installer/target_partition
TARGET_PARTITION=$(( ${RET} - 1 ))

echo "
title		System Restore
root		(hd0,${TARGET_PARTITION})
kernel		/install/vmlinuz ${KOPT}
initrd		/install/initrd.gz
" >> "${GRUB_CONFIG}"
