#!/bin/sh
#
# Special Shutdown for lilo and grub - Tobias Grimm <tg@e-tobi.net>
#

TITLE="PowerOff"

# If we have GRUB:
if dpkg -s grub | grep -q "Status: install ok installed" ; then
    # get list of all titles in /boot/grub/mneu.lst:
    pos=`grep -E "^[^	 ]*title[	 ]+.+" /boot/grub/menu.lst | grep -ni $TITLE | cut -d: -f1`
    pos=`expr $pos - 1`

    # instruct grub to boot this title next time only:
    echo "savedefault --default=$pos --once" | grub
else

    # If we have LILO:
    if dpkg -s lilo | grep -q "Status: install ok installed" ; then
	lilo -R $TITLE
    else
	# No bootloader found
	logger -t runlevel0-poweroff "Could not find lilo or grub...aborting!"
	exit 1
    fi
fi

# And finally shut down:
shutdown -r now
