#!/bin/sh
#
# Start the network....
#

# functions

pppoe() {
	echo "pppoe dialing up....." 
	killall -9 udhcpc 
	pppoe-setup < /mnt/wbcfg/cur/pppoe_setup.conf
	route del default
	pppoe-start	
}

pppow() {
	echo "ppp over wifi ...." 
	echo "start wifi connetion first"
	killall -9 udhcpc 
	echo "dialing up over wifi"
	pppoe-setup < /mnt/wbcfg/cur/pppoe_setup.conf
	route del default
	pppoe-start	
}

lan() {
	echo "Staring network using LAN configure"
	
	killall -9 udhcpc 
	lan_use_dhcp=`sed -n 's/^lan_Use_Dhcp=//p' /mnt/wbcfg/cur/hd35.conf`
	if [ "$lan_use_dhcp" == "NO" ]; then
		lan_mac=`sed -n 's/^lan_Mac=//p' /mnt/wbcfg/cur/hd35.conf`
		lan_ip=`sed -n 's/^lan_Ip=//p' /mnt/wbcfg/cur/hd35.conf`
		lan_netmask=`sed -n 's/^lan_Netmask=//p' /mnt/wbcfg/cur/hd35.conf`
		lan_default_gateway=`sed -n 's/^lan_Default_Gateway=//p' /mnt/wbcfg/cur/hd35.conf`
		lan_dns1=`sed -n 's/^lan_Dns1=//p' /mnt/wbcfg/cur/hd35.conf`
		lan_dns2=`sed -n 's/^lan_Dns2=//p' /mnt/wbcfg/cur/hd35.conf`
		ifconfig eth0 $lan_ip netmask $lan_netmask
		route del default
		route add default gw $lan_default_gateway
		echo $lan_dns1
		echo $lan_dns2
		echo "nameserver $lan_dns1" > /etc/resolv.conf
		echo "nameserver $lan_dns2" >> /etc/resolv.conf
	else
		/sbin/udhcpc -i eth0 -n
		lan_use_fixed_dns=`sed -n 's/^lan_Use_Fixed_Dns=//p' /mnt/wbcfg/cur/hd35.conf`
		if [ "$lan_use_fixed_dns" == "YES" ]; then
			lan_dns1=`sed -n 's/^lan_Dns1=//p' /mnt/wbcfg/cur/hd35.conf`
			lan_dns2=`sed -n 's/^lan_Dns2=//p' /mnt/wbcfg/cur/hd35.conf`
			echo "nameserver $lan_dns1" > /etc/resolv.conf
			echo "nameserver $lan_dns2" >> /etc/resolv.conf
		fi
	fi
}

wifi() {
	echo "Staring network using wifi configure"

	killall -9 udhcpc 

	passwordMode=`sed -n 's/^passwordMode=//p' /mnt/wbcfg/cur/hd35.conf`
	if [ "$passwordMode" == "ASCII" ]; then
		password=`sed -n 's/^AsciiPassword=//p' /mnt/wbcfg/cur/hd35.conf`
	elif [ "$passwordMode" == "HEX" ]; then
		password=`sed -n 's/^HexPassword=//p' /mnt/wbcfg/cur/hd35.conf`
	fi
	ssid=`sed -n 's/^ssid=//p' /mnt/wbcfg/cur/hd35.conf`
	securityMode=`sed -n 's/^securityMode=//p' /mnt/wbcfg/cur/hd35.conf`
	#authenticationType=`sed -n 's/^authenticationType=//p' /mnt/wbcfg/cur/hd35.conf`
#	password=`sed -n 's/^password=//p' /mnt/wbcfg/cur/hd35.conf`
	channel=`sed -n 's/^channel=//p' /mnt/wbcfg/cur/hd35.conf`
	wifi_ip=`sed -n 's/^WiFi_Ip=//p' /mnt/wbcfg/cur/hd35.conf`
	wifi_netmask=`sed -n 's/^WiFi_Netmask=//p' /mnt/wbcfg/cur/hd35.conf`
	wifi_default_gateway=`sed -n 's/^WiFi_Default_Gateway=//p' /mnt/wbcfg/cur/hd35.conf`
	wifi_use_dhcp=`sed -n 's/^WiFi_Use_Dhcp=//p' /mnt/wbcfg/cur/hd35.conf`
	wifi_use_fixed_dns=`sed -n 's/^WiFi_Use_Fixed_Dns=//p' /mnt/wbcfg/cur/hd35.conf`
	
	# fix wifi bug
	#ap_address=`iwlist ra0 scanning essid $ssid |grep Address:|awk '{print $5}'`
	export LD_LIBRARY_PATH=/mnt/wbbrowser/lib:$LD_LIBRARY_PATH
	/mnt/GetWiFiAPMacAddress
	ap_address=`sed -n 's/^ap_MacAddress=//p' /mnt/wbcfg/cur/hd35.conf`
	echo $ap_address
	iwconfig ra0 ap $ap_address	
	# end fix wifi bug

	if [ "$wifi_use_dhcp" == "YES" ]; then
		if [ "$securityMode" == "WEP-OPEN" ]; then
			#if [ "$authenticationType" == "open" ]; then
				iwconfig ra0 key open
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				if [ "$passwordMode" == "ASCII" ]; then
					iwconfig ra0 enc s:$password
				elif [ "$passwordMode" == "HEX" ]; then
					iwconfig ra0 enc $password
				fi
				udhcpc -i ra0 -n
			#elif [ "$authenticationType" == "shared" ]; then
		elif [ "$securityMode" == "WEP-SHARED" ]; then
				iwconfig ra0 key restricted
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				if [ "$passwordMode" == "ASCII" ]; then
					iwconfig ra0 enc s:$password
				elif [ "$passwordMode" == "HEX" ]; then
					iwconfig ra0 enc $password
				fi
				udhcpc -i ra0 -n
			#fi
		elif [ "$securityMode" == "WPA-PSK-TKIP" ]; then
				iwpriv ra0 set AuthMode=WPAPSK
        			iwpriv ra0 set EncrypType=TKIP
        			iwpriv ra0 set SSID="$ssid"
        			iwpriv ra0 set WPAPSK="$password"
        			iwpriv ra0 set SSID="$ssid"
				udhcpc -i ra0 -n
				#udhcpc -i ra0 
		elif [ "$securityMode" == "WPA-PSK-AES" ]; then
				iwpriv ra0 set AuthMode=WPAPSK
        			iwpriv ra0 set EncrypType=AES
        			iwpriv ra0 set SSID="$ssid"
        			iwpriv ra0 set WPAPSK="$password"
        			iwpriv ra0 set SSID="$ssid"
				udhcpc -i ra0 -n
				#udhcpc -i ra0 
		elif [ "$securityMode" == "WPA2-PSK" ]; then
				echo "WPA2-PSK securityMode now not implemented yet!"
		elif [ "$securityMode" == "Disabled" ]; then
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				udhcpc -i ra0 -n
		fi
	 if [ "$wifi_use_fixed_dns" == "YES" ]; then
			wifi_dns1=`sed -n 's/^WiFi_Dns1=//p' /mnt/wbcfg/cur/hd35.conf`
			wifi_dns2=`sed -n 's/^WiFi_Dns2=//p' /mnt/wbcfg/cur/hd35.conf`
			echo "nameserver $wifi_dns1" > /etc/resolv.conf
			echo "nameserver $wifi_dns2" >> /etc/resolv.conf
	  fi
  elif [ "$wifi_use_dhcp" == "NO" ]; then
		if [ "$securityMode" == "WEP-OPEN" ]; then
#			if [ "$authenticationType" == "open" ]; then
				iwconfig ra0 key open
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				if [ "$passwordMode" == "ASCII" ]; then
					iwconfig ra0 enc s:$password
				elif [ "$passwordMode" == "HEX" ]; then
					iwconfig ra0 enc $password
				fi
				ifconfig ra0 inet $wifi_ip netmask $wifi_netmask up
				route del default
				route add default gw $wifi_default_gateway
#			elif [ "$authenticationType" == "shared" ]; then
		elif [ "$securityMode" == "WEP-SHARED" ]; then
				iwconfig ra0 key restricted
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				if [ "$passwordMode" == "ASCII" ]; then
					iwconfig ra0 enc s:$password
				elif [ "$passwordMode" == "HEX" ]; then
					iwconfig ra0 enc $password
				fi
				ifconfig ra0 inet $wifi_ip netmask $wifi_netmask up
				route del default
				route add default gw $wifi_default_gateway
#			fi
		elif [ "$securityMode" == "WPA-PSK-TKIP" ]; then
				iwpriv ra0 set AuthMode=WPAPSK
        			iwpriv ra0 set EncrypType=TKIP
        			iwpriv ra0 set SSID="$ssid"
        			iwpriv ra0 set WPAPSK="$password"
        			iwpriv ra0 set SSID="$ssid"
				# check below line command
				iwconfig ra0 channel $channel
				ifconfig ra0 inet $wifi_ip netmask $wifi_netmask up
				route del default
				route add default gw $wifi_default_gateway
		elif [ "$securityMode" == "WPA-PSK-AES" ]; then
				iwpriv ra0 set AuthMode=WPAPSK
        			iwpriv ra0 set EncrypType=AES
        			iwpriv ra0 set SSID="$ssid"
        			iwpriv ra0 set WPAPSK="$password"
        			iwpriv ra0 set SSID="$ssid"
				# check below line command
				iwconfig ra0 channel $channel
				ifconfig ra0 inet $wifi_ip netmask $wifi_netmask up
				route del default
				route add default gw $wifi_default_gateway
		elif [ "$securityMode" == "WPA2-PSK" ]; then
				echo "WPA2-PSK securityMode now not implemented yet!"
		elif [ "$securityMode" == "Disabled" ]; then
				iwconfig ra0 essid "$ssid"
				iwconfig ra0 channel $channel
				ifconfig ra0 inet $wifi_ip netmask $wifi_netmask up
				route del default
				route add default gw $wifi_default_gateway
		fi
		wifi_dns1=`sed -n 's/^WiFi_Dns1=//p' /mnt/wbcfg/cur/hd35.conf`
		wifi_dns2=`sed -n 's/^WiFi_Dns2=//p' /mnt/wbcfg/cur/hd35.conf`
		echo "nameserver $wifi_dns1" > /etc/resolv.conf
		echo "nameserver $wifi_dns2" >> /etc/resolv.conf
	fi
}

# functions

# fix wifi bug
# fix wget in busybox
#echo "kill usbdevpoll first"
#killall -9 usbdevpoll
#echo $?
#echo " >>replace busybox <<"
#cp -rf /mnt/busybox /bin/
#echo "kill usbdevpoll first"
#echo "run usbdevpoll again"
#/usr/bin/usbdevpoll &
echo "remove /usr/bin/wget"
rm -rf /usr/bin/wget
echo "relink wget to new busybox in mnt directory"
ln -s /mnt/busybox /usr/bin/wget

cp -rf /mnt/iwlist /sbin/iwlist

connectionType=`sed -n 's/^connection_Type=//p' /mnt/wbcfg/cur/hd35.conf`
echo $connectionType

if [ "$connectionType" == "PPPoE" ]; then
	route add -net 224.0.0.0 netmask 240.0.0.0 dev ppp0
	pppoe
elif [ "$connectionType" == "LAN" ]; then
	lan
	route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0		
elif [ "$connectionType" == "WiFi" ]; then
	wifi
	route add -net 224.0.0.0 netmask 240.0.0.0 dev ra0		
elif [ "$connectionType" == "PPPoW" ]; then
	pppow
fi
