#! /bin/sh

set -e

PROC=/proc/acpi/ibm

test -e "$PROC/driver" || exit 0



get_field() {
	test -e "$PROC/$1" || exit 1

	ret=$(grep ^$2: "$PROC/$1" | tr -s '\t' '\t' | cut -f 2)
}


get_toggle() {
	test -e "$PROC/$1" || exit 1

	get_field $1 status
	case $ret in
	  enabled)
		ret=disable
		;;
	  disabled)
		ret=enable
		;;
	esac
}



toggle() {
	test -e "$PROC/$1" || exit 1

	get_toggle $1
	echo "$ret" >"$PROC/$1"
}



fn_f1() {
	true
}

fn_f2() {
	true
}

fn_f3() {
	/usr/local/sbin/ibm-screen-backlight.sh
}

fn_f4() {
	true
}

fn_f5() {
#	toggle bluetooth
	true
}

fn_f6() {
	true
}

fn_f7() {
#	get_field video status
	true
}

fn_f8() {
	true
}

fn_f9() {
	true
}

fn_f10() {
	true
}

fn_f11() {
	true
}

fn_f12() {
	true
}



echo "$(date): $*" >>/var/log/acpi.log



case $4 in
  00000001|00001001)
	fn_f1
	;;

  00000002|00001002)
	fn_f2
	;;

  00000003|00001003)
	# turn of screen
	fn_f3
	;;

  00000004|00001004)
	# sleep
	fn_f4
	;;

  00000005|00001005)
	# bt
	fn_f5
	;;

  00000006|00001006)
	fn_f6
	;;

  00000007|00001007)
	# crt/lcd
	fn_f7
	;;

  00000008|00001008)
	fn_f8
	;;

  00000009|00001009)
	fn_f9
	;;

  0000000a|0000100a)
	fn_f10
	;;

  0000000b|0000100b)
	fn_f11
	;;

  0000000c|0000100c)
	# hibernate
	fn_f12
	;;

  00005001)
	# lid close
	/usr/local/sbin/ibm-screen-backlight.sh 0
	/usr/local/sbin/ibm-x-lock.sh 0
	;;

  00005002)
	# lid open
	/usr/local/sbin/ibm-screen-backlight.sh 1
	/usr/local/sbin/ibm-x-lock.sh 1
	;;

  *)
	echo "Huh?"
	exit 1
	;;
esac
