#! /bin/sh

# A highly-abbreviated form of the script(s) from pm-utils.

# FIXME
check_suspend_pmu()
{
        perl << EOF
sub PMU_IOC_CAN_SLEEP { 0x40044205; }
open PMU, '/dev/pmu' or die "open /dev/pmu: \$!";
\$p = pack 'l', 0;
ioctl PMU, &PMU_IOC_CAN_SLEEP, \$p or die "ioctl: \$!";
(\$v) = unpack 'l', \$p;
exit (\$v ? 0 : 1);
EOF
}

can_suspend () {
    if grep -q mem /sys/power/state; then
	return 0
    elif [ -c /dev/pmu ] && check_suspend_pmu; then
	return 0
    else
	return 1
    fi
}

can_hibernate () {
    [ -f /sys/power/disk ] && grep -q disk /sys/power/state
    return $?
}

case "$1" in
--suspend)
    can_suspend
    return $?
    ;;
--hibernate)
    can_hibernate
    return $?
    ;;
--suspend-hybrid)
    # We assume HIBERNATE_RESUME_POST_VIDEO=no
    [ -w /sys/class/rtc/rtc0/wakealarm ] && can_suspend && can_hibernate
    return $?
    ;;
*)
    echo "Usage: `basename $0` --(suspend|hibernate|suspend-hybrid)" 1>&2
    return 2
    ;;
esac
