#!/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 POWERSAVE_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

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
  /sbin/modprobe -r ipw2200 ieee80211_crypt_wep ieee80211 ieee80211_crypt
fi

if [ $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 ieee80211_crypt_wep ieee80211 ieee80211_crypt
  sleep 1
  /sbin/ifup $INTERFACE
fi

# see example_event_script for an explanation.
$SCRIPT_RETURN $EV_ID 0 "$MYNAME finished"
#
EXIT 0
