# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler GrubConfigOptions
AddConfigHelp "ChangeGrubMenu <boolean>" "Change grub's config file to show system is suspended before suspending and restore after resume."
AddConfigHelp "GrubMenuFile <filename>" "Filename of grub's config file. Default is /boot/grub/grub.conf."
AddConfigHelp "AlternateGrubMenuFile <filename>" "Filename of the grub config file to put in place when the machine is suspended. If this is not specified, the script will add a small header to the existing grub menu."

AddOptionHandler GrubCmdlineOptions
AddShortOption 'g'
AddLongOption 'restore-grub'
AddOptionHelp '-g, --restore-grub' 'Restores the grub menu to normal (use if a resume was not completed sucessfully) and exits the script. A suspend is not performed.'

GRUB_MENU_FILE="/boot/grub/grub.conf"

ChangeGrubMenu() {
    if [ x"$CHANGE_GRUB_MENU" = "x1" ] ; then
	if [ -f $GRUB_MENU_FILE ] ;then
	    # given file exists so change menu

	    vecho 2 "Changing grub menu..."

	    # Make a backup, and abort if we fail to.
	    if ! cp "$GRUB_MENU_FILE" "$GRUB_MENU_FILE.hibernate.bak" ; then
		vecho 0 "Could not make backup of grub menu. Not changing!"
		return 1 # abort if not forced
	    fi

	    # If we have an alternate file, use that. Else alter the current one
	    if [ -n "$ALT_GRUB_MENU_FILE" ] ; then
		if ! cp -f "$ALT_GRUB_MENU_FILE" "$GRUB_MENU_FILE" ; then
		    vecho 0 "Could not replace grub menu file with alternate."
		    return 1 # abort if not forced
		fi
	    else
		echo  >> "$GRUB_MENU_FILE"
		echo "title _____________________________________________________________________" >> "$GRUB_MENU_FILE"
		echo "configfile dummy" >> "$GRUB_MENU_FILE"
		echo "title WARNING: "`uname -s -r` "is suspended via Software Suspend!" >> "$GRUB_MENU_FILE"
		echo "configfile dummy" >> "$GRUB_MENU_FILE"
	    fi
	    # Call sync to ensure it is written to disk (do we still need
	    # double/triple syncs?)
	    sync ; sync
	else
	    CHANGE_GRUB_MENU=0
	    vecho 1 "Grub config file not found. Grub scriptlet disabled."
	fi
    fi
}

RestoreGrubMenu() {
    if [ x"$CHANGE_GRUB_MENU" = "x1" ] ; then
	if [ -f $GRUB_MENU_FILE.hibernate.bak ] ; then
	    vecho 2 "Restoring grub menu..."
	    mv -f "$GRUB_MENU_FILE.hibernate.bak" "$GRUB_MENU_FILE"
	fi
    fi
}

GrubConfigOptions() {
    case $1 in
	changegrubmenu)
	    BoolIsOn "$1" "$2" && CHANGE_GRUB_MENU=1 || return 0
	    ;;
	grubmenufile)
	    GRUB_MENU_FILE="$2"
	    return 0
	    ;;
	alternategrubmenufile)
	    if [ ! -r "$2" ] ; then
		vecho 0 "AlternateGrubMenuFile $2 is not readable."
		vecho 0 "This configuation option will be ignored."
		return 0
	    fi
	    ALT_GRUB_MENU_FILE="$2"
	    return 0
	    ;;
	*)
	    return 1
    esac
    if [ -z "$GRUB_HOOKED" ] ; then
	AddSuspendHook 11 ChangeGrubMenu
	AddResumeHook 11 RestoreGrubMenu
	GRUB_HOOKED=1
    fi
    return 0
}

GrubCmdlineOptions() {
    case $1 in
	-g|--restore-grub)
	    AddSuspendHook 00 RestoreGrubStandalone
	    ;;
	*)
	    return 1
    esac
    return 0
}

RestoreGrubStandalone() {
    # we want to restore always
    CHANGE_GRUB_MENU=1
    RestoreGrubMenu
    vecho 0 "Grub menu restored."

    # 3 to indicate hibernate to be silent on exiting
    return 3
}


# $Id: grub 628 2005-01-04 17:48:04Z dagobah $
