#!/bin/sh

# lid.sh
# Copyright 2003 Rob Mahurin <rob@utk.edu>
# Available for use under the General Public License
# No warranty etc.
   
XSC=/usr/bin/xscreensaver-command
XSET=/usr/bin/X11/xset 
# XSET=/bin/false  # uncomment this or something similar to not use DPMS

die() {
    echo "Unable to determine state of lid."
    exit 1
}

lockall() {
    for i in /tmp/.X11-unix/X* ; do
        num=$(basename $i | cut -b2-)
        export DISPLAY=:${num}.0
#        echo "Locking display " $DISPLAY
#        $XSC -lock
    done
}

RADEONTOOL=/usr/bin/radeontool
[ -x $RADEONTOOL ] || exit 0

STATEFILE=/proc/acpi/button/lid/LID/state
[ -f $STATEFILE ] || die;
STATE=$(awk '{ print $2 }' < $STATEFILE)

case "$STATE" in
    "closed")
        echo "Lid is closed.  " 
	echo "Turning off backlight." && $RADEONTOOL light off
        lockall
	[ -x $XSET ] && echo DPMS off && $XSET dpms force off
        ;;
    "open")
        echo "Lid is open.  Activating backlight."
	[ -x $XSET ] && echo DPMS on && $XSET dpms force on
        $RADEONTOOL light on
        ;;
    *)
        $RADEONTOOL light on
        die
        ;;
esac

