function LedOn()
{
        echo 75 > /sys/class/gpio/export
        echo out > /sys/class/gpio/gpio75/direction
        echo 1 > /sys/class/gpio/gpio75/value
        echo 75 > /sys/class/gpio/unexport
}

function LedOff()
{
        echo 75 > /sys/class/gpio/export
        echo out > /sys/class/gpio/gpio75/direction
        echo 0 > /sys/class/gpio/gpio75/value
        echo 75 > /sys/class/gpio/unexport
}


#check for network connection
LedOff

#get the wlan0 status from ifconfig
ADDR=$(ifconfig | grep -A1 wlan0)
echo "ADDR=$ADDR"

COUNT=10
#evaluate the status of wlan0 to see if it is up
while [[ $ADDR != *"inet addr"* ]] && [[ $COUNT > 0 ]]
do
	echo "Attempting to connect to WiFi $COUNT more times"
	LedOff

	# shut down and bring up wlan0
	ifdown wlan0
	ifup wlan0

	LedOn
	sleep 1
	LedOff
	
	#reacquire the wlan0 status from ifconfig
	ADDR=$(ifconfig | grep -A1 wlan0)

	COUNT=$(($COUNT - 1))
done
#quits when wirless status is up or 10 tries

if [[ $COUNT > 0 ]]; then
	#turn on LED if WiFi is up
	LedOn
else
    echo "Could not connect to wireless network"
fi
