#!/bin/sh -e

NAME=grub-reboot
VERSION=0.01

if ! test -n "$1" || test "$1" = "-h" || test "$1" = "--help" ; then
  echo $NAME
  echo
  echo "Reboots into the specified OS entry in menu.lst"
  echo
  echo "Usage: $0 entry [options to grub]"
  echo "       (where \"entry\" is the entry number in menu.lst)"
  echo
  exit
fi

if test "$1" = "-v" || test "$1" = "--version" ; then
  echo $NAME $VERSION
  exit
fi

if test "`whoami`" != "root" ; then
  echo "You must be root"
  exit
fi

default="$1" ; shift
grub --batch $@ <<EOT
savedefault --once --default=$default
quit
EOT

echo
echo -n "Do you want to reboot now? [y/N] "
read REBOOT
case $REBOOT in
  y*|Y*) reboot ;;
esac
