#!/bin/sh

eth0=e1000
ath0=ath_pci
additional="ath_rate_onoe ath_hal wlan"

ath_present=`/sbin/lsmod | grep -c "$ath0"`
eth_present=`/sbin/lsmod | grep -c "$eth0"`

#
# Run 'apt-get update' if connected with this IP via eth0
#
updateip=132.187.40.129

setup_ath() {
	logger -s -t ath0 "Trying to activate ath0..."
	if [ $eth_present != 0 ]; then
		/sbin/ifdown eth0
		/sbin/rmmod $eth0
	fi
	if [ $ath_present == 0 ]; then
		/sbin/modprobe $ath0
	fi

	# configure ath0 as up to be able to use iwscan
	/sbin/ifconfig ath0 up

	# Sleep a second for things to settle...
	sleep 3

	# Check for available access Point
	ap=`/sbin/iwlist ath0 scan`

	nores=`echo $ap | grep -c "No scan results"`
	unavail=`echo $ap | grep -c "unavailable"` 
	logger -s -t ath0 "$ap"

	if [ $unavail != 0 ] || [ $nores != 0 ]; then
		ap_nonavail=1;
	fi
	
	ap_nonavail=`/sbin/iwlist ath0 scan | grep -c "No scan results"`

	if [ $ap_nonavail != 1 ]; then
		/sbin/ifup ath0 up
		cp /etc/network/ddclient.ath0 /etc/ddclient.conf
		/usr/sbin/ddclient &
	else
		logger -s -t ath0  "No Access Point available/ready. Disabeling WLAN, removing modules"
		/sbin/ifdown ath0
		/sbin/rmmod $ath0
		/sbin/rmmod $additional
	fi
	ath_present=`/sbin/lsmod | grep -c "$ath0"`
	if [ $ath_present -gt 0 ]; then
		logger -s  -t ath0 "Driver loaded, WLAN activated and configured."
	fi
}

setup_eth() {
	logger -s -t eth0 "Trying to activate eth0..."
   	if [ $ath_present != 0 ]; then
		/sbin/ifdown ath0
		/sbin/rmmod $ath0
		/sbin/rmmod $additional
	fi
	if [ $eth_present == 0 ]; then
		/sbin/modprobe $eth0
	fi

	# Sleep a second for things to settle...
	sleep 3

	# check for a link
	link=`/sbin/mii-tool eth0 | grep -c "no link"`
	if [ $link == 0 ]; then
		/sbin/ifup eth0
		cp /etc/network/ddclient.eth0 /etc/ddclient.conf
		/usr/sbin/ddclient &
	else
		logger -s -t eth0 "No link or cable, disabeling LAN, removing modules"
		/sbin/rmmod $eth0
	fi
	#
	# Check if updates should be processed
	#
	updatepackages=`/sbin/ifconfig | grep eth0 -A 2 | grep $updateip -c`
	if [ $updatepackages == 1 ]; then
		logger -s -t LAN "Searching for system upgrades..."
		# /usr/bin/apt-get update > /dev/null 2> /dev/null 
		# /usr/bin/apt-get update > /dev/null 2> /dev/null
		# /usr/bin/apt-get -s upgrade | mail arwagner -s "[Debian] Available Upgrades"
	fi
	eth_present=`/sbin/lsmod | grep -c "$eth0"`
	if [ $eth_present -gt 0 ]; then
		logger -s -t eth0 "eth0 loaded, LAN activated and configured."
	fi
}

setup_off() {
	if [ $ath_present != 0 ]; then
		/sbin/ifdown ath0
		/sbin/rmmod $ath0
		/sbin/rmmod $additional
		logger -s -t LAN "ath0 shut down, modules removed."
	fi
	if [ $eth_present != 0 ]; then
		/sbin/ifdown eth0
		/sbin/rmmod e1000
		logger -s -t LAN "eth0 shut down, modules removed."
	fi
}

case $1 in
   #
   # Configure ath0 only mode
   #
   --ath0)
   	setup_ath
	;;
   #
   # Configure eth0 only mode
   #
   --eth0)
   	setup_eth
	;;
   #
   # Configure no networking
   #
   --off)
   	setup_off
	;;
   #
   # Configure eth0 or ath0, whatever is possible
   #
   *)
   	logger -s -t LAN "Searching for LAN connections, autoconfiguring..."
   	setup_eth
	if [ $eth_present == 0 ]; then
		setup_ath
	fi
   	;;
esac
   	
