#!/bin/bash

# This is the ACPI version of the shell version of PMI

# calling convention: pmi <event>

. /etc/default/acpi-support
. /usr/share/acpi-support/device-funcs

DeviceConfig

command="$1"
event="$2"

usage () {
        echo "Usage: $0 query|action <event>" >&2
        echo "       $0 capabilities" >&2
        exit 254
}

query () {
        [ ! -z "$1" ] && event="$1" 
        case "$event" in
                suspend|sleep)
                        if [ "$ACPI_SLEEP" = true ]; then
                                result=0
                        else
                                result=1
                        fi
                ;;
                hibernate)
                        if [ "$ACPI_HIBERNATE" = true ]; then
                                result=0
                        else
                                result=1
                        fi
                ;;
                *)
                        result=1
                        echo "No such event found" >&2
                        ;;
        esac
}
                        
run () {
        case "$event" in
                suspend|sleep)
                        [ -f /var/lock/acpisleep ] && exit 0
                        /etc/acpi/sleep.sh force
                ;;
                hibernate)
		        [ -f /var/lock/acpisleep ] && exit 0
                        /etc/acpi/hibernate.sh force
                ;;
		restart)
		       [ -f /var/lock/acpisleep ] && exit 0
		       shutdown -r now
		;;
		shutdown)
		       [ -f /var/lock/acpisleep ] && exit 0
		       shutdown -h now
		;;
        esac
}
                
capabilities () {
        for i in "hibernate" "suspend"; do
                query $i
                [ $result -eq 0 ] && caps="$caps $i"
        done
        echo $caps
}

case "$command" in
        query)
                query $event
                exit $result
                ;;
        action)
                run $event
                ;;
        capabilities)
                capabilities
                ;;
        *)
                usage
                ;;
esac

exit 0
