#!/bin/sh

POWERSAVED_SUSPEND2RAM="dbus-send --system --dest=com.novell.powersave \
                        --print-reply /com/novell/powersave \
                        com.novell.powersave.action.SuspendToRam"

alarm_not_supported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.AlarmNotSupported >&2
	echo Waking the system up is not supported >&2
	exit 1
}

unsupported() {
	echo org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported >&2
	echo No suspend method found >&2
	exit 1
}

read seconds_to_sleep

#PMU systems cannot use /sys/power/state yet, so use a helper to issue an ioctl
if [ "$HAL_PROP_LAPTOP_PANEL_ACCESS_METHOD" == "pmu" ]; then
	hal-system-power-pmu sleep
	if [ $? -ne 0 ]; then
		echo "org.freedesktop.Hal.Device.SystemPowerManagement.NotSupported" >&2
		exit 1
	fi
	exit 0
fi

if [ -x "/usr/bin/powersave" ] ; then
	$POWERSAVED_SUSPEND2RAM
	RET=$?
elif [ -w "/sys/power/state" ] ; then
	# Use the raw kernel sysfs interface
	echo "mem" > /sys/power/state
	RET=$?
else
	# TODO: add other scripts support
	unsupported
fi

exit $RET
