#!/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 POWERSAVE_ACPI_MODULES
# as well as POWERSAVE_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.
export PATH
MYNAME=${0##*/} # basename $0


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
  exit 0
fi

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

  logger "$MYNAME WLAN-card configured as $INTERFACE is going down."
  /sbin/ifdown $INTERFACE
  sleep 1
  /sbin/modprobe -r ipw2200 ieee80211 ieee80211_crypt ieee80211_crypt_wep
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
  sleep 2
  /sbin/modprobe ieee80211 ieee80211_crypt ieee80211_crypt_wep
  sleep 1
  /sbin/ifup $INTERFACE
fi

exit 0