#!/bin/sh

# Normal Gigabit LAN
eth0=tg3
lanif=lan0

# Wireless, called ath0 as this script is pored from madwifi
ath0=ipw2200
drvopts=""
# as only one interface is up at a time it is always eth0...
wlanif=ath0
additional="ieee80211 ieee80211_crypt"

# ieee 1394 AKA FireWire(tm)
eth2=eth1394

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.164

setup_ath() {
	logger -s -t ath0 "Trying to activate ath0..."

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

	nores=`echo $ap | grep -c "No scan results"`
	unavail=`echo $ap | grep -c "unavailable"` 
	logger -s -t $wlanif "$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 $wlanif up
#		cp /etc/network/ddclient.ath0 /etc/ddclient.conf
#		/usr/sbin/ddclient &
	else
		logger -s -t $wlanif "No Access Point available/ready. Disabeling WLAN, removing modules"
		/sbin/ifdown $wlanif
		/sbin/rmmod $ath0
		/sbin/rmmod $additional
	fi
	ath_present=`/sbin/lsmod | grep -c "$ath0"`
	if [ $ath_present -gt 0 ]; then
		logger -s  -t $wlanif "Driver loaded, WLAN activated and configured."
	fi
}

setup_eth() {
	logger -s -t $lanif "Trying to activate $lanif..."

	# check for a link
	link=`/sbin/mii-tool $lanif | grep -c "link ok"`
	logger -s -t $lanif "link ok reports: $link"
	if [ $link == 0 ]; then
		logger -s -t $lanif "No link or cable, disabeling LAN, removing modules"
		/sbin/rmmod $eth0
	else
		/sbin/ifup $lanif
# 		cp /etc/network/ddclient.eth0 /etc/ddclient.conf
# 		/usr/sbin/ddclient &
	fi

	eth_present=`/sbin/lsmod | grep -c "$eth0"`
	if [ $eth_present -gt 0 ]; then
		logger -s -t $lanif " loaded, LAN activated and configured."
		/sbin/ifdown $wlanif
		/sbin/rmmod $ath0
		/sbin/rmmod $additional
		logger -s -t LAN "$wlanif shut down, modules removed."
	fi
}

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

logger -s -t LAN "LAN Configuration"
setup_off
logger -s -t LAN "setup_off, all modules unloaded"
/sbin/modprobe $eth0
logger -s -s LAN "moprobed $eth0"
/sbin/modprobe $ath0
logger -s -s LAN "moprobed $ath0"
sleep 3

/sbin/ifrename -p -d
logger -s -s LAN "Interfaces renamed"
/sbin/ifconfig $lanif up
/sbin/ifconfig $wlanif up
# Sleep a second for things to settle...
sleep 3

logger -s -t LAN "Starting actual configuration"

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
   	
