#!/bin/sh
. /etc/config/wifiMgrcfg
n="0"

NetStatus()
{
	if [ "$n" = "0" ]; then
		sleep $initialSleep #Sleep adjusts for network not up on boot
		net=$(ping $PingLocation -c5 |  grep 'received' | awk -F', ' '{ print $2}' | cut -c1)
			if [ "$net" -gt "0" ]; then
				logger WifiMgr: Initial network check OK!
				logger WifiMgr: It appears I have nothing to do at the moment. I will stay vigilent!
				NetStatus
			else
				logger WifiMgr: Got $net out of 5 ping responses.
				logger WifiMgr: Network check failed. Starting network change...
				NetChange

			fi
	fi
	logger WifiMgr: Waiting $pingSleep seconds then checking the network again...
	sleep $pingSleep
	logger WifiMgr: Attempting to ping $PingLocation now...
	net=$(ping $PingLocation -c5 |  grep 'received' | awk -F', ' '{ print $2}' | cut -c1)
	if [ "$net" -gt "0" ]; then
		logger WifiMgr: Got $net out of 5 ping responses.
		logger WifiMgr: Network appears OK.               
		NetStatus
	else
		logger WifiMgr: Got $net out of 5 ping responses.
		logger WifiMgr: Network check failed. Starting network change...
		NetChange
	fi
}



NetChange()
{
	scanres=
	ifconfig wlan0 down
	iw phy phy0 interface add scan0 type station
	ifconfig scan0 up
	logger WifiMgr: Performing network scan for available APs...
		while [ "$scanres" = "" ]; do
			#Sometimes it shows nothing, so better to ensure we did a correct scan
			scanres=$(iw scan0 scan|grep SSID)
		done

	iw dev scan0 del
	ifconfig wlan0 up
	killall -HUP hostapd
	
	if [ "$1" ]; then
		ssid=net"$1"_ssid
		eval ssid=\$$ssid
		echo Trying to connect to network "$1"":    $ssid"
		n=$(expr "$1" - "1")
	fi
	
	if [ "$n" -ge "$apNumber" ]; then
		n="1"
	else
		n=$(expr "$n" + "1")
	fi
	
	if [ "$n" = "99" ]; then
		#Crazy high wireless count, breaking loop!
		break
	fi

		ssid=net"$n"_ssid
		encrypt=net"$n"_encrypt
		key=net"$n"_key
		bssid=net"$n"_bssid
		network=net"$n"_network
		device=net"$n"_device
		mode=net"$n"_mode
		channel=net"$n"_channel

		eval ssid=\$$ssid
		eval encrypt=\$$encrypt
		eval key=\$$key
		eval bssid=\$$bssid
		eval network=\$$network
		eval device=\$$device
		eval mode=\$$mode
		eval channel=\$$channel
                
		if [ "$ssid" = "" ]; then
			#ssid empty assuming there is no AP's available or it's the end of the wlist file
			uci delete wireless.@wifi-iface[0]
			uci commit wireless
#			sleep 5
			logger WifiMgr: No usable APs found or end of AP list, disabling the WWAN and restarting network.
#			wifi down
#			sleep 5
#			wifi up
#			sleep 5
			/etc/init.d/network restart
			sleep 5
			NetChange
		fi

#		Section ommitted for security
#		
#		echo SSID: $ssid
#		echo Password: $key
#		echo Encryption: $encrypt
#		echo BSSID: $bssid
#		echo Network Interface: $network
#		echo Physical Device: $device
#		echo Connection Mode: $mode                
#

		active=$(echo $scanres | grep " $ssid ">&1 )
		
		if [ "$active" ]; then
			
			logger WifiMgr: $ssid network found. Applying settings...
			ifaceResult=$(uci show wireless | grep iface)
				
			if [ "$ifaceResult" = "" ]; then
				echo Did not find wifi-iface entry in wireless config file... Adding to wireless config file.
				uci add wireless wifi-iface
				uci commit wireless
			fi
					
			uci set wireless.@wifi-device[0].disabled="1"
			uci commit wireless
#			wifi down
#			sleep 5
			uci delete wireless.@wifi-device[0].channel
			uci set wireless.@wifi-device[0].channel="$channel"
			uci delete wireless.@wifi-iface[0].ssid
			uci set wireless.@wifi-iface[0].ssid="$ssid"
			uci delete wireless.@wifi-iface[0].encryption
			uci set wireless.@wifi-iface[0].encryption="$encrypt"
			uci delete wireless.@wifi-iface[0].key
			uci set wireless.@wifi-iface[0].key="$key"
			uci delete wireless.@wifi-iface[0].bssid
			uci set wireless.@wifi-iface[0].bssid="$bssid"
			uci delete wireless.@wifi-iface[0].network
			uci set wireless.@wifi-iface[0].network="$network"
			uci delete wireless.@wifi-iface[0].device
			uci set wireless.@wifi-iface[0].device="$device"
			uci delete wireless.@wifi-iface[0].mode
			uci set wireless.@wifi-iface[0].mode="$mode"
			uci set wireless.@wifi-device[0].disabled="0"
			uci set wireless.@wifi-iface[0].disabled="0"
			uci commit wireless
			logger WifiMgr: Attempting connection to $ssid now...
#			sleep 5
#			wifi up
#			sleep 5
       		/etc/init.d/network restart
       		sleep 5
			NetStatus
		else
			NetChange
		fi
}

if [ "$1" = "" ]; then
	echo "No arguments supplied"
	exit 0
elif [ "$1" = "--force" ]; then
	NetChange $2
elif [ "$1" = "--daemon" ]; then	
	NetStatus
else
	echo "Wrong arguments"
	exit 0
fi
exit 0