#!/bin/bash
# acpi_samsung_p35_hotkeys
# Sven Burmeister, based on the scripts by Manfred Tremmel and Stefan Seyfried.
#
# To use this script on SuSE, place it in /usr/lib/powersave/scripts, include
# the asus_acpi module in ACPI_MODULES
# as well as ACPI_MODULES_NOT_TO_UNLOAD, and place the filename of this script
# in EVENT_OTHER
# (use Yast > System > /etc/sysconfig Editor's search functinality).
#
# Make sure the script is executable.
#
# At the moment only the WLAN-key is handled.
#
# set -vx

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 from here.
. ${0%/*}/helper_functions # `dirname $0`/helper_functions
# get_x_user comes from here...
. $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"

if [ "$EVENT" != "hotkey" ]; then
  echo "non-hotkey-event: $TYPE $EVENT $ACPI $WHAT $SERIAL" | logger -t $MYNAME
  $SCRIPT_RETURN $EV_ID 0 "$MYNAME finished"
  EXIT 0
fi

if [ $WHAT == "0000005f" ]; then #bring WLAN down
  INTERFACE=""
  INTERFACEID=`/bin/ls /etc/sysconfig/network/ifcfg-wlan*`
  INTERFACEID=${INTERFACEID#*id}
  INTERFACE=`/sbin/getcfg-interface id$INTERFACEID`

  logger "$MYNAME WLAN-card configured as $INTERFACE is going down."
  /sbin/ifdown $INTERFACE
  sleep 1
  #turn radio off 0 = on, 1 = off, 2 = wlan off but radio on?
  #RF_KILL=`/usr/bin/find /sys/bus/pci/drivers/ipw2200/0*/rf_kill`
  #if ["x${RF_KILL}" != "x" ] ; then #driver is available
  #  echo 1 > "${RF_KILL}" #disable wlan
  #fi
  /sbin/modprobe -r ipw2200

elif [ $WHAT == "0000005e" ]; then #bring WLAN up
  INTERFACE=""
  TEMPO=""

  IFACES="eth0 eth1" #Depending on which module is loaded first on boot, WLAN is either eth0 or eth1

  for i in $IFACES; do
    TEMPO=`/usr/sbin/iwconfig $i 2>&1 | grep "such device"` #if wlan-interface is down, iwconfig will return: No such device.

    if [ "$TEMPO" != "" ]; then
      INTERFACE="$i"
    fi
  done

  logger "$MYNAME WLAN-card configured as $INTERFACE is coming up."
  /sbin/modprobe ipw2200
  sleep 1

  #turn radio on
  RF_KILL=`/usr/bin/find /sys/bus/pci/drivers/ipw2200/0*/rf_kill`
  if ["x${RF_KILL}" != "x" ] ; then #driver is available
    echo 0 > "${RF_KILL}" #enable wlan
  fi

  /sbin/ifup $INTERFACE

elif [ $WHAT == "00000032" ]; then #mute
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/dcop kmix Mixer0 toggleMute 82801DB-ICH4" &
  : #do something

elif [ $WHAT == "00000030" ]; then #volume up
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/dcop kmix Mixer0 increaseVolume 82801DB-ICH4" &
  : #do something

elif [ $WHAT == "00000031" ]; then #volume down
  run_on_xserver "export DISPLAY=:0.0;/opt/kde3/bin/dcop kmix Mixer0 decreaseVolume 82801DB-ICH4" &
  : #do something

#elif [ $WHAT == "00000050" ]; then #mail
  #there is no dcop boolean yet to show if there is new mail, in any folder

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 0
