#!/bin/sh
# This script performs a lid event, in the absence of gnome-power-manager.
# But it tries to follow its policy.  To do this, it needs a user.  It chooses
# the user installed by the debian installer.

. /usr/share/debconf/confmodule

isonac() {
	grep -q 'on-line' /proc/acpi/ac_adapter/*/state
}

halaction() {
	dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.$*
}

if grep -q open /proc/acpi/button/lid/*/state; then
	# don't do any special action on lid open
	exit
fi 

if isonac; then
	KEY="/apps/gnome-power-manager/buttons/lid_ac"
else	
	KEY="/apps/gnome-power-manager/buttons/lid_battery"
fi

db_get passwd/username
USER="$RET"

ACTION=$(sudo -u $USER gconftool-2 -g $KEY)

case $ACTION in
shutdown)
	echo "Shutting down"
	halaction Shutdown
;;
suspend)
	echo "Suspending"
	halaction Suspend int32:0
;;
hibernate)
	echo "Hibernating"
	halaction Hibernate
;;
blank)
	echo "Blanking"
	# We secretly do nothing.  The normal lid event processing will blank screen.
        # This event in a user session will lock the screen.  But we don't have to
	# worry about that, since we're not in a session.  So act like 'do nothing'.
;;
nothing)
	echo "Doing nothing"
;;
*)
	echo "Unknown user action $ACTION, doing nothing"
;;
esac
