#!/bin/bash
# acpi_hotkeys_Samsung_P30
# Manfred Timm, based on the script of Stefan Seyfried
# Provides basic functions; simple done; could be make better
# Backlit (Fn-F5) and Brightness (Fn-up/down) runs from Bios
# if [ $VAL -eq 79 ]; then # Fn-F2 --> for your own use (shows battery on Win)
#
# The scripts for mute/volume are taken from this website 
# http://www.hentges.net/misc/howtos/files/p30-acpi-events.tar.gz
#
# Although this is for a SAMSUNG machine, you need to load
# the asus_acpi module!

PATH=/bin:/usr/bin # be paranoid, we're running as root.
# first get helper functions (e.g. DEBUG, load_scheme, ...), extend $PATH,
# set helper variables. We get $SCRIPT_RETURN, EXIT and EV_ID from here.
. ${0%/*}/helper_functions # `dirname $0`/helper_functions
# get_x_user comes from here...
. $PUB_SCRIPT_DIR/x_helper_functions

# previously, we checked for $# <= 3. This is not very future-proof ;-)
if [ -z "$EV_ID" ]; then
    logger -t $MYNAME 'Sorry, not enough arguments: $4 is empty.'
    $SCRIPT_RETURN $EV_ID 1 "$MYNAME finished unsuccessful."
    EXIT 1
fi

run_on_xserver() {
  get_x_user
  su $X_USER -c "DISPLAY=$DISP $1"
}

TYPE=$1
set $3 # powersaved gives us "other 'schemename' '<content of /proc/acpi/event>'" so we must split it.
EVENT=$1   # "hotkey"
ACPI=$2    # "HOTK"
WHAT=$3    # "00000052"
SERIAL=$4  # "0000001c"

# it is easier to deal with numerical values (for me :-)
declare -i VAL
VAL=0x$WHAT # hex -> decimal
ret=0

if [ "$EVENT" != "hotkey" ]; then
  logger -t $MYNAME "non-hotkey-event: $TYPE $EVENT $ACPI $WHAT $SERIAL"
  # do not exit here, we need to execute SCRIPT_RETURN_HELPER
elif [ $VAL -ge 97 -a $VAL -le 99 ]; then # Fn-F4 internal/external/both
  : # do nothing
elif [ $VAL -eq 50 ]; then # Fn-F6 -> mute
    /etc/acpi/mute.sh &             # needs aumix 
  : # set loudnes to 0
elif [ $VAL -eq 49 ]; then # Fn-left -> volume down
    /etc/acpi/vol_down.sh &            # needs aumix, does "pcm"
  : # reduce loudnes
elif [ $VAL -eq 48 ]; then # Fn-right -> volume up
    /etc/acpi/vol_up.sh &            # needs aumix, does "pcm"
  : # increase loudnes
elif [ $VAL -eq 94 ]; then # Wlan Button on --> start centrino
    /etc/init.d/network stop eth0 &	# use ifplugd for eth0
    /sbin/ifup eth1 &
elif [ $VAL -eq 95 ]; then # Wlan Button off --> stop centrino
    /sbin/ifdown eth1 &
    /etc/init.d/network start eth0 &	# use ifplugd for eth0
elif [ $VAL -eq 80 ]; then # Start Mailer/Kmail
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/kmail" &
  : # do something
elif [ $VAL -eq 81 ]; then # Start Browser/Konqueror
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/kfmclient openProfile webbrowsing" &
  : # do something
elif [ $VAL -eq 82 ]; then # Start Calculator/Kcalc
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/kcalc" &
  : # do something 
else
  DEBUG "undefined hotkey: $VAL $TYPE $EVENT $ACPI $WHAT $SERIAL" DIAG
  ret=1
fi
#
# see example_event_script for an explanation.
$SCRIPT_RETURN $EV_ID 0 "$MYNAME finished"
#
EXIT $ret
