# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet
# Vbetool scriptlet requires Matt J Garrett's vbetool package to be useful.
# http://www.srcf.ucam.org/~mjg59/vbetool/
#
# This scriptlet Copyright (C) Cameron Patrick, 2005, and may be
# used under the same terms as hibernate-script itself.

AddConfigHandler VbetoolOptions
AddConfigHelp "EnableVbetool <boolean>" "Save and restore video state using vbetool before and after suspending."

# hopefully this is a good choice and nothing else uses it :-/
VBETOOL_VT=61

VbetoolSuspend() {
    # if we're switched off, don't do nuffin
    [ x"$VBETOOL_ENABLED" = "x1" ] || return 0

    # Make sure we have a vbetool.
    if ! command -v vbetool > /dev/null 2>&1 ; then
	VBETOOL_ENABLED=0
	vecho 1 "'vbetool' utility not found. Vbetool disabled."
	return 0
    fi

    # save our previous VT and switch to a new one
    VBETOOL_ORIG_VT=`fgconsole 2>/dev/null` || VBETOOL_ORIG_VT=1
    chvt $VBETOOL_VT
    tput clear

    VBETOOL_FILE=`mktemp /tmp/tmp.hibernate.XXXXXX`

    # save the vbe state
    vbetool vbestate save > $VBETOOL_FILE || return 1

    return 0
}

VbetoolResume() {
    # if we're switched off, don't do nuffin
    [ x"$VBETOOL_ENABLED" = "x1" ] || return 0

    # switch the screen back on
    vbetool vbestate restore < $VBETOOL_FILE
    rm -f $VBETOOL_FILE

    # change back to our original VT
    chvt $VBETOOL_ORIG_VT
}

VbetoolOptions() {
    case $1 in
        enablevbetool)
            BoolIsOn "$1" "$2" && VBETOOL_ENABLED=1 || return 0
            ;;
        *)
            return 1
    esac

    if [ -z "$VBETOOL_HOOKED" ] ; then
	AddSuspendHook 97 VbetoolSuspend
	AddResumeHook 97 VbetoolResume
	VBETOOL_HOOKED=1
    fi

    return 0
}

