#!/bin/sh

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

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

#SuSE and ALTLinux only support powersave
if [ -f "/etc/altlinux-release" ] || [ -f "/etc/SuSE-release" ] ; then
	if [ -x /usr/bin/powersave ] ; then
	        $POWERSAVED_STANDBY
		RET=$?
	else
		# TODO: add support
		unsupported
	fi

#FreeBSD uses zzz to suspend for both ACPI and APM
elif [ "x`uname -s`" = "xFreeBSD" ] ; then
	if [ -x /usr/sbin/acpiconf ] ; then
		/usr/sbin/acpiconf -s 1
		RET=$?
	else
		unsupported
	fi

#Other distros just need to have *any* tools installed
else
	if [ -x "/usr/bin/powersave" ] ; then
	    $POWERSAVED_STANDBY
	    RET=$?
	elif [ -w "/sys/power/state" ] ; then
	    # Use the raw kernel sysfs interface
	    echo "standby" > /sys/power/state
	    RET=$?
	else
	    # TODO: add other scripts support
	    unsupported
	    fi
	fi

#Refresh devices as a resume can do funny things
for type in button battery ac_adapter
do
	devices=`hal-find-by-capability --capability $type`
	for device in $devices
	do
		dbus-send --system --print-reply --dest=org.freedesktop.Hal \
			  $device org.freedesktop.Hal.Device.Rescan
	done
done

exit $RET
