#!/bin/bash
# acpi_hotkeys_ASUS_M6842NW
# Manfred Tremmel, based on the script of Stefan Seyfried
#

echo "my script $*"| \
    logger

# first get helper functions (e.g. DEBUG, load_scheme, ...)
. "/usr/lib/powersave/scripts/helper_functions"

PATH=/bin:/usr/bin # be paranoid, we're running as root.
export PATH
ret=0
MYNAME=${0##*/} # basename $0
if [ $# -ne 3 ] ; then # something wicked happened
  DEBUG "something wicked happened. number of arguments: $#, arguments: '$*'" ERROR
  exit 1
fi

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

TYPE=$1
set $3 # powersaved gives us "other '<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

  echo "VAL: '$VAL'"| \
    logger -t $MYNAME

if [ "$EVENT" != "hotkey" ]; then
  echo "non-hotkey-event: $TYPE $EVENT $ACPI $WHAT $SERIAL" | logger -t $MYNAME
  exit 0
fi
if [ $VAL -gt 16 -a $VAL -lt 48 ]; then # brightness up/down
  exit 0
elif [ $VAL -eq 51 -o $VAL -eq 52 ]; then # LCD on/off
  exit 0
elif [ $VAL -ge 97 -a $VAL -le 99 ]; then # internal/external/both
  exit 0
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/dvdrecorder
    : # 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 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
  if [ "`ps -A | grep -c kwifimanager`" = "0" ] ; then
    run_on_xserver "/opt/kde3/bin/kwifimanager" &
  else
    killall kwifimanager
  fi
  : # start kwifimanager
elif [ $VAL -eq 92 ]; then # Power4 Gear +
  run_on_xserver "/opt/kde3/bin/ksysguard" &
  : # start ksysguard
else
  DEBUG "undefined hotkey: $VAL $TYPE $EVENT $ACPI $WHAT $SERIAL" DIAG
  ret=1
fi

exit $ret
