#!/bin/sh

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

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

services="/etc/init.d/cupsys
/etc/init.d/ssh
/etc/init.d/privoxy
/usr/sbin/ddclient
/etc/init.d/fail2ban"

#---- normally, nfs is not required ----
# /etc/init.d/portmap
# /etc/init.d/nfs-common
# /etc/init.d/nfs-kernel-server
#---- normally, nfs is not required ----

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

stopNetworkServices() {
	for srv in $services
	do
		$srv stop
	done
}

startNetworkServices() {
	for srv in $services
	do
		$srv start
	done
}

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 2>&1`

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

	logger -s -t ath0 "$ap"

	ap_noavail=($unavail + $nores)

	if [ "$ap_nonavail" == 0 ]; then
	    /sbin/ifup ath0 up
	    if [ -e /etc/init.d/fail2ban ]; then
	            /etc/init.d/fail2ban start
	    fi
	    cp /etc/network/ddclient.ath0 /etc/ddclient.conf
	    # /usr/sbin/ddclient &
	    startNetworkServices
	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
		if [ -e /etc/init.d/fail2ban ]; then
		        /etc/init.d/fail2ban start
		fi
		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."
		startNetworkServices
	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
}

stopNetworkServices

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
   	
