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

AddConfigHandler ClockOptions
AddConfigHelp "SaveClock restore-only|<boolean>" "Set this to yes to save the system clock before suspending and restore the system clock after resuming. If set to restore-only, the clock will not be saved, only restored - this means suspending is faster, but if your hardware clock drifts significantly, your system clock will drift as well."

ClockSave() {
    case "$DISTRIBUTION" in
	debian|ubuntu)
	    /etc/init.d/hwclock.sh stop > /dev/null 2>&1
	    ;;
	gentoo)
	    /etc/init.d/clock.sh stop > /dev/null 2>&1
	    ;;
	*)
	    if ! /sbin/hwclock $DIRECTISA --systohc ; then
		echo "$EXE: Failed to save system clock - continuing anyway"
	    fi
	    ;;
    esac
    return 0  # clock save failing shouldn't be a show stopper, ever.
}

ClockRestore() {
    case "$DISTRIBUTION" in
	debian|ubuntu)
	    /etc/init.d/hwclock.sh start > /dev/null 2>&1
	    ;;
	gentoo)
	    /etc/init.d/clock.sh start > /dev/null 2>&1
	    ;;
	*)
	    if ! /sbin/hwclock $DIRECTISA --hctosys ; then
		echo "$EXE: Failed to restore system clock."
	    fi
	    ;;
    esac
    # exit code unchanged.
}

ClockOptions() {
    local param
    case $1 in
	directisa)
    		case "$DISTRIBUTION" in
		gentoo)
			echo "$EXE: Options for hwclock should be set in /etc/init.d/clock.sh"
			exit 1
			;;
		debian|ubuntu)
			echo "$EXE: Options for hwclock should be set in /etc/init.d/hwclock.sh"
			exit 1
			;;
		*)
			DIRECTISA="--directisa"
			;;
		esac
		;;
	saveclock)
	    param=`echo $2 | tr '[A-Z]' '[a-z]'`
	    case "$param" in
		restore*) # restore-only
		    AddResumeHook 70 ClockRestore
		    ;;
		*)
		    if BoolIsOn "$1" "$2" ; then
			# put before module unloading and x switching.
			AddSuspendHook 70 ClockSave
			AddResumeHook 70 ClockRestore
		    fi
		    ;;
	    esac
	    ;;
	*)
	    return 1
    esac
    return 0
}

# $Id$
