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

AddConfigHandler MiscLaunchOptions
AddConfigHelp "OnSuspend NN <program to execute> [parameters for program]" "Executes a given program before suspending. NN is a two-digit number between 00 and 99, inclusive - a higher number means the program will be executed later in the suspend process. See the ORDERING section in the SCRIPTLET-API for details."
AddConfigHelp "OnResume NN <program to execute> [parameters for program]" "Executes a given program after resuming. NN is a number between 00 and 99, inclusive - a higher number means the program will be executed earlier in the resume process. See the ORDERING section in the SCRIPTLET-API for details."

MISC_LAUNCH_COUNT=0

MiscLaunchSuspendProg() {
    local tmpf
    MISC_LAUNCH_COUNT=$(($MISC_LAUNCH_COUNT+1))
    tmpf=`mktemp /tmp/tmp.hibernate.XXXXXX`
    AddSuspendHook $1 MiscLaunchAuxFunc$MISC_LAUNCH_COUNT
    shift
    cat <<EOT > $tmpf
MiscLaunchAuxFunc${MISC_LAUNCH_COUNT}() {
    vcat 1 <<EOT2
Executing $@...
EOT2
    $@
    [ \$? -ne 0 ] && return 1
    return 0
}
EOT
    . $tmpf
    rm -f $tmpf
}

MiscLaunchResumeProg() {
    local tmpf
    MISC_LAUNCH_COUNT=$(($MISC_LAUNCH_COUNT+1))
    tmpf=`mktemp /tmp/tmp.hibernate.XXXXXX`
    AddResumeHook $1 MiscLaunchAuxFunc$MISC_LAUNCH_COUNT
    shift
    cat <<EOT > $tmpf
MiscLaunchAuxFunc${MISC_LAUNCH_COUNT}() {
    vcat 1 <<EOT2
Executing $@...
EOT2
    $@
    [ \$? -ne 0 ] && return 1
    return 0
}
EOT
    . $tmpf
    rm -f $tmpf
}

MiscLaunchOptions() {
    case $1 in
	onsuspend)
	    shift
	    if ! IsANumber $1 ; then
		vecho 0 "First argument to OnSuspend must be a number between 00 and 99!"
		exit 1
	    fi
	    MiscLaunchSuspendProg $*
	    ;;
	onresume)
	    shift
	    if ! IsANumber $1 ; then
		vecho 0 "First argument to OnResume must be a number between 00 and 99!"
		exit 1
	    fi
	    MiscLaunchResumeProg $*
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id: misclaunch 1139 2007-07-16 12:33:07Z nigel $
