#!/bin/sh

tmp=`mktemp`

target_dir=$1
if [ "$target_dir" = "" ]; then
  target_dir="/"
fi

dialog --title "Boot loader configuration" --yesno "Install GRUB boot loader?" 6 39

if [ $? -eq 0 ]; then
  while [ 0 -eq 0 ]; do
    dialog --colors --inputbox "\ZbSpecify the target device:\Zn\n\n\Zb\Z4/dev/hda\Zn for MBR on the first harddrive\n\Zb\Z4/dev/hdb1\Zn for the first primary partition on the second harddrive" 13 75 "/dev/hda" 2>$tmp
    if [ $? -ne 0 ]; then
      break
    fi
    grub_device=`cat $tmp`
    grub-install --recheck --no-floppy --root-directory=$1 $grub_device &> /dev/null
    grub_result=$?
    if [ $grub_result -ne 0 ]; then
      dialog --title 'Error' --yesno "GRUB installation failed!\nReturn code=$grub_result\n\nTry again?" 8 39
      if [ $? -ne 0 ]; then
        break
      fi
    else
      dialog --title 'Success' --msgbox "GRUB installation succeeded!" 6 39
      break
    fi
  done
fi

rm -f $tmp
