#!/bin/bash
#################################
# SETTINGS

# modules to unload
MODULES="uhci_hcd ehci_hcd"
# for x osdcat...
export DISPLAY=:0.0
export XAUTHORITY=`ls /etc/X11/wdm/authdir/authdir/authfiles/*`
# crappy code, to find out who is the current X-fg user
export XUSER=`pstree -Au | egrep 'wdm.+\(.+\)' | sed -e 's/.*(//; s/).*//'`

#################################
# LOGGING FACILITY (OSD / SYSLOG)
log() {
	osd $*
	logger -t x40 $*
}
osd() {
	if [ -x /usr/bin/osd_cat ]
	then
		echo "$*" | osd_cat --font -*-utopia-*-r-*-*-*-180-*-*-*-*-*-* --colour=Steelblue --shadow 2 --pos bottom --align center --offset 25 --delay 2 &
	fi
}

#################################
# FUNCTIONS THAT STACK FUNCTIONS
event_sleep() {
	commit_sleep
}
event_suspend() {
	log Suspend initiated...
	unload_modules
	screen_saver
	commit_suspend
	reload_modules
	restart_hotplug
}
event_hibernate() {
	log Hibernation initiated...
	stop_net
	unload_modules
	commit_hibernate
	reload_modules
	restart_hotplug
	start_net
}

event_lid_open() {
	switch_vt
}
event_lid_close() {
	screen_saver
}
event_bay_eject() {
	#  shutdown procedure for bay devices
		# i got no such hardware, so i cant figure it out ;)
	ibm_bay_eject
}
event_bay_insert() {
	# initialisation stuff for bay hardware 
		# i got no such hardware, so i cant figure it out ;)
	true
}
event_dock_init() {
	ibm_dock_init
	# initialisation stuff here
		# i got no such hardware, so i cant figure it out ;)
}
event_dock_eject() {
	# shutdown procedure for bay
		# i got no such hardware, so i cant figure it out ;)
	# finally call acpi eject
	ibm_dock_eject
}
event_power() {
	commit_shutdown
}
#################################
# FUNCTIONS THAT DO THE WORK
commit_shutdown() {
	log Initiating shutdown...
	echo 0 blink > /proc/acpi/ibm/led
	#init 0
}
start_net() {
	log Network on...
	/etc/init.d/net.eth0 start
	#/etc/init.d/net.ath0 start
	sleep 2
}
stop_net() {
	log Network off...
	/etc/init.d/net.eth0 stop
	#/etc/init.d/net.ath0 stop
	sleep 2
}

# this function toggles the thinklight
ibm_thinklight() {
	local C
	C=`grep -c on /proc/acpi/ibm/light`
	if [ "$C" = 1 ] ; then
	  echo on > /proc/acpi/ibm/light	
	else
	  echo off > /proc/acpi/ibm/light
	fi
}

# this function toggles bluetooth
ibm_bluetooth() {
	local C
	C=`grep -c enable /proc/acpi/ibm/bluetooth`
	if [ "$C" = 1 ] ; then
	  log Bluetooth enabled...
	  echo enable > /proc/acpi/ibm/bluetooth
	  sleep 2
	  /etc/init.d/bluetooth start
	else
	  log Bluetooth disabled...
	  /etc/init.d/bluetooth stop
	  echo disable > /proc/acpi/ibm/bluetooth
	fi
}

# this functions loads/unloads the flight level
ibm_wlan() {
	local C
	C=`lsmod | egrep -c "^ath_pci"`
	if [ "$C" = 1 ] ; then
	  log WLAN disabled...
	  #/etc/init.d/net.ath0 stop
	  modprobe -r ath_pci
	else
	  log WLAN enabled...
	  modprobe ath_pci
	 # /etc/init.d/net.ath0 start
	fi
}
ibm_bay_eject() {
	echo eject > /proc/acpi/ibm/bay
}
ibm_dock_init() {
	echo dock > /proc/acpi/ibm/dock
}
ibm_dock_eject() {
	echo undock > /proc/acpi/ibm/dock
}
unload_modules() {
	local M
	for M in $MODULES ;
	do
		modprobe -r $M 2>&1 >/dev/null ;
	done
}
commit_hibernate() {
	sync
	/sbin/hwclock --systohc --localtime
	echo disk > /sys/power/state
	/sbin/hwclock --hctosys --localtime
}
commit_suspend() {
	sync
	/sbin/hwclock --systohc --localtime
	echo mem > /sys/power/state
	/sbin/hwclock --hctosys --localtime
}
commit_sleep() {
	sync
	echo standby > /sys/power/state
}
reload_modules() {
	local M
	for M in $MODULES ;
	do
		modprobe $M 2>&1 >/dev/null ;
	done
}
restart_hotplug() {
	/etc/init.d/hotplug restart
}
screen_saver() {
	su - ${XUSER} -c "xlock -display ${DISPLAY} -mode blank &"
}
switch_vt() {
	local FG
	FG=`fgconsole`
	chvt 1
	chvt $FG
}

#######################
# EVENT ROUTING

# explode $1
#set $1

EVENT_TYPE=$1   # "ibm/hotkey"
EVENT_SUBTYPE=$2    # "HKEY"
EVENT_ID=$3  # "00000080"
EVENT_NR=$4    # "0000xxxx"

logger -t x40 $EVENT_TYPE $EVENT_SUBTYPE $EVENT_ID $EVENT_NR

if [ "$EVENT_TYPE" = "ibm/hotkey" ] ; then
        if [ "$EVENT_SUBTYPE" = HKEY ] ; then
	  case "$EVENT_NR" in
	 	00001001)			# FN-F1
		    ;;
                00001002)			# FN-F2
                    ;;
		00001003)			# FN-F3
	 	    event_sleep
		    ;;
	 	00001004)			# FN-F4
	 	    event_suspend
	 	    ;;
	 	00001005)			# FN-F5 (BT is Hardwired on R51!)
	 	    ibm_bluetooth
	 	    ;;
	        00001006)			# FN-F6
                    ibm_wlan
		    ;;
	 	00001007)			# FN-F7
	 	    ;;
	 	00001008)			# FN-F8
	 	    ibm_wlan
	 	    ;;
	 	00001009)			# FN-F9
		    osd `acpi -V` `cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | awk '{printf("@ %u Mhz\n",$1/1000);}'`
	 	    ;;
                0000100a)			# FN-F10
                    ;;
                0000100b9)			# FN-F11
                    ;;
	 	0000100c)			# FN-F12
	 	    event_hibernate
	 	    ;;
                0000100e)			# FN-INS
                    ;;
                0000100f)			# FN-DEL
                    ;;
		00005001)			# LID CLOSE
		    event_lid_close
		    ;;
		00005002)			# LID OPEN
		    event_lid_open
		    ;;
	 	*)
	 	    log ibm unassigned hotkey: $EVENT_NR
	 	    ;;
    	  esac  
	fi
fi
if [ "$EVENT_TYPE" = "ibm/dock" ] ; then
	if [ "$EVENT_SUBTYPE" = MSTR ] ; then
	  case "$EVENT_NR" in
		00000001)
		     # eject request
		     ibm_dock_eject
		     ;;
		00000002)
		     # got undocked
		     true
		     ;;
		00000003)
		     # got docked
		     ibm_dock_init
		     ;;
		*)
		     true
		     ;;
	  esac
        fi
fi
if [ "$EVENT_TYPE" = "ibm/bay" ] ; then
	if [ "$EVENT_SUBTYPE" = BAY ] ; then
	  case "$EVENT_ID" in
	  	00000003)
      		    # eject request
    		    ibm_bay_eject
		    ;;
		00000001)		    
		    # eject lever inserted
		    ibm_bay_insert
		    ;;
		*)
		    true
		    ;;
	  esac
	fi
fi
if [ "$EVENT_TYPE" = "button/power" ] ; then
        event_power
fi

