#!/bin/sh

. /usr/share/acpi-support/key-constants

wlan_control=/proc/acpi/asus/wlan
bt_control=/proc/acpi/asus/bt

WLANSTATE=$(cat $wlan_control)
BTSTATE=$(cat $bt_control)

# The sequence here *is* important.
unload_modules() {
   modprobe -r -q -w rt2860sta
}

# At least this one's straightforward.
load_modules() {
   modprobe rt2860sta
}

wifi_on() {
   # Force PCI Express Hotplug to reinit
   modprobe -r -q pciehp
   sleep 1
   # pciehp_force may be unnecessary; Xandros did it.
   modprobe pciehp pciehp_force=1 pciehp_debug=1
   sleep 1
   # Switch on the hardware
   echo 1 > $wlan_control
   sleep 1
   load_modules
   sleep 1
   /etc/init.d/networking restart
}

wifi_off() {
   ifconfig ra0 down
   sleep 1
   unload_modules
   echo 0 > $wlan_control
   modprobe -r -q pciehp
}


bt_on()
{
    echo 1 > $bt_control
    sleep 1
    modprobe bluetooth
    modprobe hci_usb
    /etc/init.d/bluetooth stop
}

bt_off()
{
    modprobe -r hci_usb
    modprobe -r bluetooth 
    sleep 1
    echo 0 > $bt_control
    /etc/init.d/bluetooth start
}


if [ -f $bt_control ]
then
  if [ $WLANSTATE -eq  1 -a $BTSTATE -eq 1 ]
  then
    wifi_off
    bt_off
  elif [ $WLANSTATE -eq  0 -a $BTSTATE -eq 0 ]
  then
    wifi_on
    bt_on
  elif [ $WLANSTATE -eq  1 -a $BTSTATE -eq 0 ]
  then
    wifi_off
    bt_off
  elif [ $WLANSTATE -eq  0 -a $BTSTATE -eq 1 ]
  then
    wifi_on
    bt_on
  fi
else
    case $WLANSTATE in
      1)   
            wifi_off
            bt_off
      ;;
      0)
            wifi_on
            bt_on
      ;;
    esac
fi 

