#!/bin/bash 

cfile=/etc/wpa_supplicant/wpa_supplicant-wlan0.conf


zenity --width=400 --height=400 --info --title "Scanning Networks" --text "Please note that this utility does not set up wireless networking, it just allows you to change networks.\n\nTo switch from wired to wireless networking please contact Groovix technical support.\n\n\nPlease Wait..."&
zpid=$!

#sudo ip link set wlan0 down
#sleep 3

ip link set wlan0 up
sleep 5

iwlist wlan0 scanning  > /tmp/wpa_supplicant_network_chooser.scan
kill $zpid

ssid_columns=$(cat /tmp/wpa_supplicant_network_chooser.scan | awk -F: 'BEGIN{ printf "zenity --width=400 --height=400  --list --print-column=ALL --text \"Available Networks\" --list --column ESSID --column Security --column Signal "; }/Quality/{ split($0,x,"="); Quality = int(x[2]*100/70+.5); }/Encryption/{ Encryption = $2; }/ESSID/{ ESSID = $2; printf "%s \"%s\" \"%s%%\" \\\n", ESSID, Encryption, Quality}' |sh); 


ssid=$(echo $ssid_columns | cut -f 1 -d\|)
security=$(echo $ssid_columns | cut -f 2 -d\|)


echo ssid_columns is \"$ssid_columns\"
echo ssid is \"$ssid\"
echo security is \"$security\"

if [[ -z "$ssid" ]] ; then
	echo "exiting"
	exit 10
else 
	echo "using ssid $ssid"
fi


#echo 1
#    sudo ip link set wlan0 down 
#echo 2
#    sudo iwconfig wlan0 essid $ssid 
echo 3
	if [[ "$security" = "off" ]] ; then
		echo "NO SECURITY"
		echo > $cfile
		echo "network={" >> $cfile
		echo -n "  ssid=\"" >> $cfile
		echo -n $ssid   >> $cfile
		echo "\"" >> $cfile
		echo    "  key_mgmt=NONE" >> $cfile
		echo "}" >> $cfile

	else
			wpa_passphrase $ssid > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf $(zenity --width=400 --height=400  --password --title "Network Passphrase")
	fi
echo 3a
    
zenity --width=400 --height=400  --info --title "Setting Up Network" --text "Please Wait..."&
zpid=$!
echo 4
#    sudo ip link set wlan0 up 
#    sleep 5
echo 5
#    sudo service wpa_supplicant restart
echo 6
    # it is taking at least 15 seconds before network is really up !!!!!!!!!!!!!!
    #todo : put in while look to check if it is ready yet, give up after 60 seconds
    #sleep 20

# this seems to work better than a restart
#killall wpa_supplicant
#wpa_supplicant -Dwext -iwlan0 -c$cfile &
#sleep 5

#this seems to work by itself
systemctl restart wpa_supplicant@wlan0 


sleep 20

    echo gateway is $(ip r | grep default | cut -d ' ' -f 3)
    echo ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3)
    success=$(ping -q -w 1 -c 1 $(ip r | grep default | cut -d ' ' -f 3) > /dev/null && echo ok || echo error)
    if [[ $success = "error" ]] ; then
        # some gateways might not accept pings, try util.groovix.net too, even though this depends on full internet and firewall allowing external pings
        echo      ping -q -w 1 -c 1 69.164.198.101
    	success=$(ping -q -w 1 -c 1 69.164.198.101 > /dev/null && echo ok || echo error)
    fi

kill $zpid

    if [[ $success = "error" ]] ; then
        echo $success
        zenity --width=400 --height=400  --error --title "Error" --text "Unable to Connect"
    else
        zenity --width=400 --height=400  --info --title "Success" --text "Connected to $ssid"
    fi

