#!/bin/sh

. /mnt/mmcblk0p1/userconfig.txt

hostname "$HOSTNAME"

wlan()
{
	if [ "$WLAN" = "true"  ]
	then
		if [ "$WLANSSID" = "" ]
		then
			echo "Network:     There is no wireless network configured, exiting!"
		else
			echo "Network:     No ethernet connected, configuring WiFi for configured network "\"$WLANSSID"\"..."
			ifconfig wlan0 up
			iwlist wlan0 scan > /var/wirelesscan
			if ! grep -q "$WLANSSID" /var/wirelesscan
			then
				echo "Network:     Configured network not available, exiting!"
				exit
			fi
			if cat /var/wirelesscan | sed -n "/\"$WLANSSID\"/,/Quality/p" | grep -q "Encryption key:on"
			then
				echo "Network:     The Network \""$WLANSSID"\"is encrypted, let's figure how..."
				if cat /var/wirelesscan | sed -n "/\"$WLANSSID\"/,/Quality/p" | grep -i -q "WPA"
				then
					echo "             It uses WPA encryption, configuring it for that..."
					connect_wpa
				else echo "             It uses WEP encryption,configuring it for that..."
					connect_wep
				fi
			else
				echo "             Oh my, it uses no encryption, bad idea, configuring it for that..."
				connect_open
			fi
		fi
	fi
}

connect_open()
{
	echo "             Setting up connection to unencrypted netword \""$WLANSSID"\" now."
	ifconfig wlan0 up
	iwconfig wlan0 mode managed
	iwconfig wlan0 essid "$WLANSSID"
	udhcpc -b -i wlan0 -x hostname  $HOSTNAME
}

connect_wep()
{
	echo "             Setting up connection to WEP encrypted network \""$WLANSSID"\" now.."
	ifconfig wlan0 up
	iwconfig wlan0 essid "$WLANSSID"
	## Works with "shared key in hex!"
	iwconfig wlan0 key "$WLANPWD"
	iwconfig wlan0 mode managed
	udhcpc -b -i wlan0 -x hostname  $HOSTNAME
}

connect_wpa()
{
	echo "             Setting up connection to WPA encrypted network \""$WLANSSID"\" now."
	wpa_passphrase "$WLANSSID" "$WLANPWD" > /var/wpa_supplicant.conf
	wpa_supplicant -B -Dwext -iwlan0 -c /var/wpa_supplicant.conf 
	udhcpc -b -i wlan0 -x hostname: "$HOSTNAME" > /dev/null &> /dev/null
}

eth_dhcp()
{
	echo "             using dhcp as requested per userconfig-file..."
	udhcpc -b -i eth0 -x hostname:"$HOSTNAME" > /dev/null &> /dev/null
	if [ "$WLAN" = "true"  ]
	then
		ifconfig wlan0 up
	fi
}

eth_manual()
{
	sleep 0.5
	echo "             manually as requested per userconfig-file"
	ifconfig eth0 "$IPADDR" netmask "$NETMASK" up
	echo "nameserver "$DNSSERV"" > /etc/resolv.conf
	route add default gw "$GATEWAY"
	if [ "$WLAN" = "true" ]
	then
		ifconfig wlan0 up
	fi
}

ifconfig lo up
until $(ifconfig eth0 up &> /dev/null)
do
	sleep 0.3 # ; echo "waiting for eth0 to initialize..."
done

if [ "$DHCP" = "ON" ]
then echo "Network:     DHCP is ON"
	sleep 2
	#until ( ifconfig eth0 up ) ; do sleep 0.3 ; done
	if  (dmesg | grep -q WLAN)
    then
    	echo "Network     :Wireless network interface found and initialised, may be used as an option."
		WLAN="true"
	else
       	echo "Network:     No wireless network interface found, wireless options not available."
		WLAN="false"
	fi

	if [ "$(cat /sys/class/net/eth0/carrier)" = "1" ]
	then
		echo "Network:     Ethernet connected, configuring wired interface (eth0)"
		if [ "$DHCP" = "ON" ]
		then
			eth_dhcp
		fi
	else
		wlan
	fi
else
	eth_manual
fi

echo "Network:     Our hostname  : $(hostname)"
echo "             Our IP-address: $(ifconfig | grep inet | grep -v 127 | awk -F: '{ print $2 }' | awk '{ print $1 }')"
