#!/bin/bash
# acpi_hotkeys_ASUS_M6842NW
# Manfred Tremmel, based on the script of Stefan Seyfried
# patched for SUSE 9.3 by Stefan Seyfried :-)
#

PATH=/bin:/usr/bin # be paranoid, we're running as root.
# first get helper functions (e.g. DEBUG, load_scheme, ...), extend $PATH,
# set variables etc. We get $SCRIPT_RETURN 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 -gt 16 -a $VAL -lt 48 ]; then # brightness up/down
  : # do nothing
elif [ $VAL -eq 51 -o $VAL -eq 52 ]; then # LCD on/off
  : # do nothing
elif [ $VAL -ge 97 -a $VAL -le 99 ]; then # internal/external/both
  : # do nothing
elif [ $VAL -eq 50 ]; then # Fn-F10 -> mute
  if [ "`run_on_xserver '/opt/kde3/bin/dcop kmix | grep -c -m 1 kmix'`" = "1" ] ; then
    if [ "`run_on_xserver '/opt/kde3/bin/dcop kmix Mixer0 mute 0'`" = "false" ] ; then
      run_on_xserver "/opt/kde3/bin/dcop kmix Mixer0 setMute 0 true" &
    else
      run_on_xserver "/opt/kde3/bin/dcop kmix Mixer0 setMute 0 false" &
    fi
  else
    mute &             # from the aumix package
  fi
  : # set loudnes to 0
elif [ $VAL -eq 49 ]; then # Fn-F11 -> volume down
  if [ "`run_on_xserver '/opt/kde3/bin/dcop kmix | grep -c -m 1 kmix'`" = "1" ] ; then
    run_on_xserver "/opt/kde3/bin/dcop kmix Mixer0 decreaseVolume 0" &
  else
    aumix -w -3 &            # needs aumix, does "pcm"
  fi
  : # reduce loudnes
elif [ $VAL -eq 48 ]; then # Fn-F12 -> volume up
  if [ "`run_on_xserver '/opt/kde3/bin/dcop kmix | grep -c -m 1 kmix'`" = "1" ] ; then
    run_on_xserver "/opt/kde3/bin/dcop kmix Mixer0 increaseVolume 0" &
  else
    aumix -w +3 &            # needs aumix, does "pcm"
  fi
  : # increase loudnes
elif [ $VAL -eq 64 ]; then # <<
  run_on_xserver "/opt/kde3/bin/dcop kaffeine KaffeineIface previous" &
  : # fast backward
elif [ $VAL -eq 65 ]; then # >>
  run_on_xserver "/opt/kde3/bin/dcop kaffeine KaffeineIface next" &
  : # fast forward
elif [ $VAL -eq 67 ]; then # stop
  if [ "`run_on_xserver '/opt/kde3/bin/dcop kaffeine KaffeineIface isPlaying'`" = "true" ] ; then
    run_on_xserver "/opt/kde3/bin/dcop kaffeine KaffeineIface stop" &
    : # kaffeine is running, press stop
  else
    eject /dev/hdc
    : # kaffeine is not running, eject CD/DVD
  fi
  : # stop
elif [ $VAL -eq 69 ]; then # play/pause
  if [ "`run_on_xserver '/opt/kde3/bin/dcop kaffeine KaffeineIface isPlaying'`" = "true" ] ; then
    run_on_xserver "/opt/kde3/bin/dcop kaffeine KaffeineIface pause" &
    : # play button in kaffeine
  else
    media=`cd-info --no-tracks --no-cddb --no-device-info --dvd --no-header --cdrom-device=/dev/hdc 2>/dev/null | grep "Disc mode is listed as:" | awk -F ": " '{print $2}'`
    case "$media" in
      CD-DA)
        run_on_xserver "/opt/kde3/bin/kaffeine AudioCD >/dev/null 2>&1"
      ;;
      CD?DATA*)
        run_on_xserver "/opt/kde3/bin/kaffeine VCD >/dev/null 2>&1"
      ;;
      *DVD*)
        run_on_xserver "/opt/kde3/bin/kaffeine DVD >/dev/null 2>&1"
      ;;
      *)
        run_on_xserver "/opt/kde3/bin/kaffeine >/dev/null 2>&1"
      ;;
    esac
  fi
  : # do something
elif [ $VAL -eq 80 ]; then # Start Mailer
  run_on_xserver "/opt/kde3/bin/kmail" &
  : # do something
elif [ $VAL -eq 81 ]; then # Start Browser
  run_on_xserver "/opt/kde3/bin/kfmclient openProfile webbrowsing" &
  : # do something
elif [ $VAL -eq 93 ]; then # Wireles LAN
  RF_KILL=`find /sys/bus/pci/drivers/ipw2200/0*/rf_kill`
  if [ "x${RF_KILL}" != "x" ] ; then # only when driver is available
    WLAN_STATE=`cat "$RF_KILL"`
    #DCOP_KINTERNET=`run_on_xserver '/opt/kde3/bin/dcop | grep kinternet'`
    if [ "${WLAN_STATE}" = "0" ] ; then # wlan is enabled
      # disable wlan
      echo 1 > "${RF_KILL}"
      # switch off wlan LED
      echo 0 > /proc/acpi/asus/wled
    else
      # enable wlan
      echo 0 > "${RF_KILL}"
      # turn on wlan LED
      echo 1 > /proc/acpi/asus/wled
    fi
  fi
  : # togle wlan state activated/deactivated
elif [ $VAL -eq 92 ]; then # Power4 Gear +
  POWERSAVESTATE=`/usr/bin/powersave -x | grep active | awk -F " " '{print $1}'`
  case "$POWERSAVESTATE" in
    Performance)
      NEWPOWERSAVESTATE="Acoustic"
      ;;
    Acoustic)
      NEWPOWERSAVESTATE="Presentation"
      ;;
    Presentation)
      NEWPOWERSAVESTATE="Powersave"
      ;;
    *)
      NEWPOWERSAVESTATE="Performance"
      ;;
  esac
  /usr/bin/powersave -e ${NEWPOWERSAVESTATE}
  run_on_xserver "/opt/kde3/bin/kdialog --passivepopup \"New Powersave state ${NEWPOWERSAVESTATE}\" 2" &
  : # switching powersave states
elif [ $VAL -eq 106 ]; then # touchpad on/off
  if test -x /usr/X11R6/bin/synclient ; then
    SYNCLIENT=/usr/X11R6/bin/synclient
  else
    SYNCLIENT=/usr/bin/synclient
  fi
  PadOn=`${SYNCLIENT} -l | grep -c "TouchpadOff.*0"`
  if [ "${PadOn}" = "1" ] ; then
    ${SYNCLIENT} TouchpadOff=1
  else
    ${SYNCLIENT} TouchpadOff=0
  fi
  : # turn touchpad on or off
else
  DEBUG "undefined hotkey: keycode '$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
