#! /bin/sh

PWR=`cat /proc/acpi/asus/wlan`
SERVICE=""

unload_modules() {
	echo "`date` Unloading all wlan modules" >> /var/log/wlan.log
	# remove wlan modules
	rmmod wlan_scan_sta
	rmmod wlan_tkip 2>/dev/null
	rmmod wlan_wep 2>/dev/null
	rmmod wlan_ccmp 2>/dev/null
	rmmod wlan_acl 2>/dev/null
	rmmod ath_pci
	rmmod ath5k
	if [ "`uname -r`" != "2.6.27.10-m" ] ; then
		sleep 1
		rmmod ath_rate_atheros
		rmmod ath_hal
		rmmod wlan
		rmmod ath_dfs
	fi
	echo "`date` Wlan modules unloaded" >> /var/log/wlan.log
}


case $1 in
	poweron)
		echo "`date` Wlan poweron event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "0" ]; then
			echo "`date` Powering on wlan" >> /var/log/wlan.log
			echo 1 > /proc/acpi/asus/wlan
			sleep 1
			echo "`date` Kernel version: `uname -r`"
			if [ "`uname -r`" == "2.6.27.10-m" ] ; then
				echo "`date` Loading ath5k module" >> /var/log/wlan.log
				modprobe ath5k
				sleep 1
				echo "`date` Renaming wlan0 to ath0" >> /var/log/wlan.log
				ip link set wlan0 name ath0
			else
				if [ -z "`grep ^ath_pci /proc/modules`" ]; then
					modprobe ath_pci
					sleep 1
				fi
			fi
			/sbin/ifconfig ath0 up
		else
			if [ "`uname -r`" == "2.6.27.10-m" ] ; then
				echo "`date` (for sure) Renaming wlan0 to ath0" >> /var/log/wlan.log
				ip link set wlan0 name ath0
				sleep 1
				/sbin/ifconfig ath0 up
			fi
		fi
		;;

	restore)
		echo "`date` Wlan restore event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "1" ]; then
			if [ -f /tmp/wlan_suspend.dat ]; then
				SERVICE=`cat /tmp/wlan_suspend.dat`
				rm -f /tmp/wlan_suspend.dat
			fi
			if [ -n "$SERVICE" ] ; then
				#restore wlan connection only if present!
				WLAN_ESSID=`/opt/xandros/bin/xandrosncs-servicedb -i $SERVICE -g WIRELESS_ESSID | cut -f 2 -d '='`
				if [ -n "$WLAN_ESSID" ]; then
					/sbin/ifconfig ath0 down
					WLAN_CHANNEL=`/opt/xandros/bin/xandrosncs-servicedb -i $SERVICE -g WIRELESS_CHANNEL | cut -f 2 -d '='`
					if [ -n "$WLAN_CHANNEL" ]; then
						iwconfig ath0 channel $WLAN_CHANNEL
						DELAY=15
					else
						iwconfig ath0 channel 0
						DELAY=30
					fi
					iwconfig ath0 essid "$WLAN_ESSID"
					/sbin/ifconfig ath0 up

					while [ $DELAY -gt 0 ]; do
						sleep 1
						WLAN_FOUND=`iwlist ath0 scan 2>/dev/null | grep ESSID | cut -d '"' -f 2`
						ESSID_FOUND=`echo $WLAN_FOUND | grep "\b${WLAN_ESSID}\b"`
						echo "$DELAY. Looking for: $WLAN_ESSID, found $WLAN_FOUND. match $ESSID_FOUND.">>/tmp/wlan_wakeup.log
						if [ -n "$ESSID_FOUND" ]; then
							break;
						fi
						DELAY=$((DELAY - 1))
					done

					if [ -n "$ESSID_FOUND" ]; then
						touch /tmp/xandrosncs_no_status_dialog
						/opt/xandros/bin/xandrosncs-control -i $SERVICE start
						rm /tmp/xandrosncs_no_status_dialog
					fi
				fi
			fi
		fi
		;;

	poweroff)
		echo "`date` Wlan poweroff event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "1" ]; then
			for i in `/opt/xandros/bin/xandrosncs-servicedb --list | grep "[[:space:]]wlan[[:space:]]" | cut -f 1 -d ' '` ; do
				WLAN_STATUS=`/opt/xandros/bin/xandrosncs-control -i $i status | cut -f 2 -d ' '`
				if [ "up" = $WLAN_STATUS ]; then
					SERVICE=$i
					echo $SERVICE > /tmp/wlan_suspend.dat
					break;
				fi
			done

			if [ -n "$SERVICE" ] ; then 
				touch /tmp/xandrosncs_no_status_dialog
				/opt/xandros/bin/xandrosncs-control -i $SERVICE stop
				rm -f /etc/resolvconf/run/interface/ath0.$SERVICE
				rm /tmp/xandrosncs_no_status_dialog
			fi

			# remove wlan interface
			ifconfig ath0 down
			sleep 1
			wlanconfig ath0 destroy

			unload_modules

			echo 0 > /proc/acpi/asus/wlan
		fi
		;;

	suspend)
		echo "`date` Wlan suspend event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "1" ]; then
			echo 0 > /proc/acpi/asus/wlan
		fi
		;;

	cleanup)
		echo "`date` Wlan cleanup event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "0" ]; then
			for i in `/opt/xandros/bin/xandrosncs-servicedb --list | grep "[[:space:]]wlan[[:space:]]" | cut -f 1 -d ' '` ; do
				WLAN_STATUS=`/opt/xandros/bin/xandrosncs-control -i $i status | cut -f 2 -d ' '`
				if [ "up" = $WLAN_STATUS ]; then
					SERVICE=$i
					echo $SERVICE > /tmp/wlan_suspend.dat
					break;
				fi
			done
			if [ -n "$SERVICE" ] ; then 
				touch /tmp/xandrosncs_no_status_dialog
				/opt/xandros/bin/xandrosncs-control -i $SERVICE stop
				rm -f /etc/resolvconf/run/interface/ath0.$SERVICE
				rm /tmp/xandrosncs_no_status_dialog
			fi

			# remove wlan interface
			ifconfig ath0 down
			sleep 1
			wlanconfig ath0 destroy
			unload_modules
		fi
		;;

	status)
		echo "`date` Wlan status event (PWR: $PWR)" >> /var/log/wlan.log
		if [ "$PWR" = "1" ]; then
			echo "ON"
		else
			echo "OFF"
		fi
		;;

esac
