#Commands read and executed by frisbee components only.
#Localization - argolance - January 2013

#############################################

function max_priority {
	#set -x
	MAX_PRIORITY=0
	wpa_cli -i $INTERFACE list_networks |head | while read LINE ; do
		[[ $LINE =~ ^network* ]] && continue
                ID=$(echo "$LINE" |cut -f1)
		PRIORITY=`wpa_cli -i $INTERFACE get_network $ID priority`
		if [[ "$PRIORITY" > "$MAX_PRIORITY" ]] ; then
			MAX_PRIORITY="$PRIORITY"
			echo $MAX_PRIORITY
		fi
	done |tail -n 1
}

export -f max_priority

#############################################

function check_profile_exists {
    #set -x
	SSID=$1
	ID=-1
	wpa_cli -i $INTERFACE list_networks | while read LINE ; do
        	PSSID=$(echo "$LINE" |cut -f2)
                if [[ "$PSSID" == "$SSID" ]] ; then
                        ID=$(echo "$LINE" |cut -f1)
			echo $ID
			break
		fi
        done 
}

export -f check_profile_exists

#############################################

function add_profile {
	#set -x

	BSSID="$1"
	MAXP=`max_priority`
	[ -z "$MAXP" ] && MAXP=0
	((MAXP++))
	PRIORITY=$MAXP

	. gettext.sh
	if [[ $BSSID == '' ]] ; then
		Xdialog --title "Frisbee"  --msgbox "$(gettext 'Please select a network')" 0 0 
		return
	fi
	
	SSID=`wpa_cli scan_results -i $INTERFACE | grep "$BSSID" | cut -f5`
	FLAGS=`wpa_cli scan_results -i $INTERFACE | grep "$BSSID" | cut -f4`
	[ $SSID = "<hidden>" ] && SSID=''

	ID=`check_profile_exists $SSID`
	

	if [[ ! -z $ID ]] ; then
		RESULT=`wpa_cli -i $INTERFACE set_network $ID priority $PRIORITY`
		if [[ "$RESULT" != "OK" ]] ; then
				PARAMETER="priority"
				Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
		fi

		RESULT=`wpa_cli -i $INTERFACE save_config`
		if [[ "$RESULT" != "OK" ]] ; then
				Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to save configuration')" 0 0
		fi
		reset_wpa&
		reset_dhcp&
		return
	fi

	wpa_cli -i $INTERFACE disconnect
	
	echo "$FLAGS" | grep WPA
	if [[ $? == "0" ]] ; then
		ENC=WPA
		echo "$FLAGS" | grep TKIP
		if [[ $? == "0" ]] ; then
			ENCTYPE=TKIP
		else
			ENCTYPE=CCMP
		fi
		echo "$FLAGS" | grep WPA-
		if [[ $? == "0" ]] ; then
			PROTO=WPA
		else
			PROTO=RSN
		fi
	else
		echo $FLAGS | grep WEP
		if [  $? == "0" ] ; then
			ENC=WEP
		else
			ENC=NONE
		fi
	fi
	
	ID=`wpa_cli -i $INTERFACE add_network`
	if [ -z $ID ] | [[ "$ID" == "FAIL" ]] ; then
		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to add network')" 0 0
		return
	fi
	
	if [ -z "$SSID" ] ; then
		Xdialog  --title "Frisbee" --inputbox "$(gettext 'Enter the SSID')" 0 0 > /tmp/.frisbee/entry 2>&1
		if [[ $? == "1" ]] ; then
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
		SSID=`cat /tmp/.frisbee/entry`
		rm  /tmp/.frisbee/entry
		if [ -z "$SSID" ] ; then
			Xdialog  --title "Frisbee" --msgbox "$(gettext 'Invalid SSID')" 0 0
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
		SCANSSID=1
	fi
	
	
	RESULT=`wpa_cli -i $INTERFACE set_network $ID ssid \""${SSID}"\"`
	if [[ "$RESULT" != "OK" ]] ; then
		PARAMETER="ssid"
		Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
		wpa_cli -i $INTERFACE remove_network $ID 
		return
	fi
	
	if [[ $SCANSSID == 1 ]] ; then
		RESULT=`wpa_cli -i $INTERFACE set_network $ID scan_ssid 1`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="scan_ssid"
			Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
	fi
	
	RESULT=`wpa_cli -i $INTERFACE enable_network $ID`
	if [[ "$RESULT" != "OK" ]] ; then
		Xdialog  --title "Frisbee" --msgbox "$(gettext 'Failed to enable network')" 0 0
		wpa_cli -i $INTERFACE remove_network $ID 
		return
	fi
	
	if [[ "$ENC" == "WPA" ]] ; then
		MGMT=WPA-PSK
	else
		MGMT=NONE
	fi
	
	echo $FLAGS | grep IBSS
	if [[ $? == 0 ]] ; then
		RESULT=`wpa_cli -i $INTERFACE set_network $ID mode 1`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="mode"
			Xdialog  --title "Frisbee" --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
		RESULT=`wpa_cli -i $INTERFACE ap_scan 2`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="ap_scan"
			Xdialog  --title "Frisbee" --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
	fi
	
	RESULT=`wpa_cli -i $INTERFACE set_network $ID key_mgmt $MGMT`
	if [[ "$RESULT" != "OK" ]] ; then
		PARAMETER="key_mgmt"
		Xdialog  --title "Frisbee" --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
		wpa_cli -i $INTERFACE remove_network $ID 
		return
	fi
	
	if [[ "$ENC" == "WPA" ||  "$ENC" == "WEP" ]] ; then
		if [[ "$ENC" == "WEP" ]] ; then
		 MSG="$(gettext 'Please enter the ASCII or HEX key with no colons or dashes.\nThe HEX key works most reliably.')"
		else
		 MSG="$(gettext 'Please enter the Password.')"
		fi  
		Xdialog  --title "Frisbee" --password --inputbox "$MSG" 0 0 > /tmp/.frisbee/entry 2>&1
		if [[ $? == "1" ]] ; then
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
	        PASS=`cat /tmp/.frisbee/entry`
		rm  /tmp/.frisbee/entry
	 	if [ -z "$PASS" ] ; then
			Xdialog  --title "Frisbee" --msgbox "$(gettext 'Invalid Password')" 0 0	
			wpa_cli -i $INTERFACE remove_network $ID 
			wpa_cli -i $INTERFACE scan
			return
		fi
		if [[ "$ENC" == "WEP" ]] ; then
			echo "$PASS" |egrep '^([[:xdigit:]]){10}$|^([[:xdigit:]]){26}$|^([[:xdigit:]]){32}$'
			[[ $? == 0 ]] && HEX=1 
			if [[ $? == "0" ]] ; then
				RESULT=`wpa_cli -i $INTERFACE set_network $ID wep_key0 "$PASS"`
			    if [[ "$RESULT" != "OK" ]] ; then
	            		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Set HEX Password Failed')" 0 0
		            	wpa_cli -i $INTERFACE remove_network $ID
		            	wpa_cli -i $INTERFACE scan
	                	return
	        	 fi
			else
				echo "$PASS" |egrep '^.{5}$|^.{13}$|^.{16}$'
				if [[ "$?" == "0" ]] ; then
					RESULT=`wpa_cli -i $INTERFACE set_network $ID wep_key0 \""${PASS}"\"` 
					if [[ "$RESULT" != "OK" ]] ; then
						Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Set ASCII Password Failed')" 0 0
						wpa_cli -i $INTERFACE remove_network $ID
						wpa_cli -i $INTERFACE scan
						return
					fi
				else
					Xdialog  --title "Frisbee" --left --msgbox "$(gettext 'Invalid Password\n\nAscii keys must be 5, 13 or 16 characters.\nHex keys must be 10, 26, or 32 characters.')" 0 0
					wpa_cli -i $INTERFACE remove_network $ID 
					wpa_cli -i $INTERFACE scan
					return
				fi	
				
			fi
			Xdialog  --title "Frisbee" --left --no-tags  --radiolist "$(gettext 'Key Type?\n\nIf in doubt, try Open first')" 0 0 0  open open open  shared shared shared > /tmp/.frisbee/entry 2>&1
            AALG=`cat /tmp/.frisbee/entry`
            if [[ "$AALG" == "shared" ]] ; then
				RESULT=`wpa_cli -i $INTERFACE set_network $ID auth_alg SHARED`
				if [[ "$RESULT" != "OK" ]] ; then
			         PARAMETER="auth_alg"
			         Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
		            wpa_cli -i $INTERFACE remove_network $ID
					return
				fi
			fi

	
		else
			echo "$PASS" |egrep '^([[:xdigit:]]){64}$'
			[[ $? == 0 ]] && HEX=1 
			if [[ "$HEX" == "1" ]] ; then
				RESULT=`wpa_cli -i $INTERFACE set_network $ID psk $PASS`
			else
				RESULT=`wpa_cli -i $INTERFACE set_network $ID psk \""${PASS}"\"`
			fi
	        	if [[ "$RESULT" != "OK" ]] ; then
	                	Xdialog  --title "Frisbee" --msgbox "$(gettext 'Invalid Password')" 0 0
	                	wpa_cli -i $INTERFACE remove_network $ID
	                	return
	        	fi
			RESULT=`wpa_cli -i $INTERFACE set_network $ID pairwise $ENCTYPE`
	        	if [[ "$RESULT" != "OK" ]] ; then
	                	PARAMETER="pairwise"
	                	Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
	                	wpa_cli -i $INTERFACE remove_network $ID
	                	return
	        	fi
		fi
	fi
	
	
	if [[ $PROTO != '' ]] ; then
		RESULT=`wpa_cli -i $INTERFACE set_network $ID proto $PROTO`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="proto"
			Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			wpa_cli -i $INTERFACE remove_network $ID 
			return
		fi
	fi
	
	RESULT=`wpa_cli -i $INTERFACE set_network $ID priority $PRIORITY`
	if [[ "$RESULT" != "OK" ]] ; then
		PARAMETER="priority"
		Xdialog  --title "Frisbee"  --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
	fi

	RESULT=`wpa_cli -i $INTERFACE enable_network $ID`
	if [[ "$RESULT" != "OK" ]] ; then
		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to enable network')" 0 0
		wpa_cli -i $INTERFACE remove_network $ID 
	fi
	
	#wpa_cli -i $INTERFACE select_network $ID 
	
	RESULT=`wpa_cli -i $INTERFACE save_config`
	if [[ "$RESULT" != "OK" ]] ; then
		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to save configuration')" 0 0
	fi
	
	reset_wpa&
	reset_dhcp&
	
	. gettext.sh
	gtkdialog-splash -timeout 15 -placement center -bg lightgreen -text "`eval_gettext \"Profile for \\\$SSID added.\"`
	$(gettext 'Network can take a minute or so to reconfigure itself.')
	$(gettext 'To configure a static IP address, press the Manage Saved Networks button.')" &

	
	if [[ $ENC == "WPA" ]] ; then
		connection_check&
	fi
}
export -f   add_profile

#############################################

function set_ap_scan {
	. gettext.sh
	grep -q '^wireless_autostart=1' /etc/frisbee/frisbee.conf \
	 && WPACFGFILE="/etc/frisbee/wpa_supplicant.conf" \
	 || WPACFGFILE="/tmp/wpa_supplicant.conf"
	VAL=`grep ap_scan $WPACFGFILE`
	[ ! -z $VAL ] && VAL="`eval_gettext \"Currently \\\${VAL}.\"`" 
	
	Xdialog --no-tags --title "Frisbee - $(gettext 'AP Scan Mode')" --left --radiolist "$(gettext 'Set the AP Scan Mode (ap_scan WPA parameter).\n\nIt should be set to 1 unless you have an adhoc network, or\nare using ndiswrapper, or some other 3rd party driver.\n\nIt may also be necessary to set it to 2 to connect to a\nhidden SSID with some hardware.') \n  $VAL" 0 0 0 1 1 1 2 2 2 > /tmp/.frisbee/entry 2>&1
    APS=`cat /tmp/.frisbee/entry`
    [ -z $APS ] && return
    if [[ "$APS" == "1" ]] ; then
		RESULT=`wpa_cli -i $INTERFACE ap_scan 1`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="ap_scan"
			Xdialog  --title "Frisbee" --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			return
		fi
	else
		RESULT=`wpa_cli -i $INTERFACE ap_scan 2`
		if [[ "$RESULT" != "OK" ]] ; then
			PARAMETER="ap_scan"
			Xdialog  --title "Frisbee" --msgbox "`eval_gettext \"Failed to set network configuration parameter: \\\$PARAMETER\"`" 0 0
			return
		fi
	fi
	
	RESULT=`wpa_cli -i $INTERFACE save_config`
	if [[ "$RESULT" != "OK" ]] ; then
		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to save configuration')" 0 0
	fi
	
	reset_wpa&
	
}
export -f set_ap_scan

#############################################

function set_drop_timeout {
	#set -x

	WTIMEOUT=$(grep -o ^[0-9]* /etc/dhcpcd_dropwait_secs 2>/dev/null)
	[ "$WTIMEOUT" ] || WTIMEOUT=0
	Xdialog --title "Frisbee - $(gettext 'Drop Timeout')" --ok-label "$(gettext 'Set')" --left  --inputbox  "$(gettext 'If the connection drops, how many seconds should we wait\nbefore deconfiguring the wireless interface?\n\nIncrease this time for unstable connections.\nDecrease it if you have trouble roaming between networks.\n\nNote that network startup times may increase due to this delay.\nStart with 15 seconds and then adjust to the shortest time\nnecessary to maintain connections.')" 0 0 $WTIMEOUT > /tmp/entry 2>&1
	NEWTIMEOUT=`cat /tmp/entry`
	rm /tmp/entry
	MAXTIMEOUT=120
	if [[ "$NEWTIMEOUT" -gt 0 && "$NEWTIMEOUT" -lt "$MAXTIMEOUT" ]] ; then
		echo -n $NEWTIMEOUT > /etc/dhcpcd_dropwait_secs
		reset_dhcp
	elif [[ "$NEWTIMEOUT" -gt "$MAXTIMEOUT" ]] ; then
		echo -n $MAXTIMEOUT > /etc/dhcpcd_dropwait_secs
	elif [[ "$NEWTIMEOUT" -eq 0 ]] ; then
		rm -f /etc/dhcpcd_dropwait_secs
	fi
}

export -f set_drop_timeout 

#############################################
		
function wpaconf_edit {
	grep -q '^wireless_autostart=1' /etc/frisbee/frisbee.conf \
	 && WPACFGFILE="/etc/frisbee/wpa_supplicant.conf" \
	 || WPACFGFILE="/tmp/wpa_supplicant.conf"
	defaulttexteditor $WPACFGFILE
	RESULT=`wpa_cli -i $INTERFACE reconfigure`
	if [[ "$RESULT" != "OK" ]] ; then
		Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to save configuration')" 0 0
	fi
	
	reset_wpa&
}

export -f wpaconf_edit

#############################################

function dhcpconf_edit {
	defaulttexteditor /etc/frisbee/dhcpcd.conf
	reset_dhcp&
}

export -f dhcpconf_edit

function connection_check {
	sleep 60	
	STATE=`wpa_cli -i $INTERFACE status 2>/dev/null|grep wpa_state|cut -f2 -d "="`
	if [[ "$STATE" != "COMPLETED" ]] ; then
		Xdialog  --title "Frisbee - $(gettext 'Connect')" --left --msgbox "$(gettext 'Wpa_supplicant failed to connect.\n\nVerify that the password you entered is correct\nand that your network card supports wpa_supplicant.')" 0 0
	fi
}
export -f   connection_check 

#############################################

function current_ip {
	IFSTATUS="$(wpa_cli -i $INTERFACE status 2>/dev/null)"
	IP="$(echo "$IFSTATUS" | grep ^ip_address | cut -f 2 -d =)"
	if [ "$(echo "$IFSTATUS" | grep ^wpa_state | cut -f 2 -d =)" = "COMPLETED" ] \
	 && [ "$IP" ]; then
		. gettext.sh
		echo "`eval_gettext \"IP Address: \\\$IP\"`"
	fi
}
export -f   current_ip

#############################################

function current_status {
	IFSTATUS="$(wpa_cli -i $INTERFACE status 2>/dev/null)"
	SSID="$(echo "$IFSTATUS" | grep ^ssid | cut -f 2 -d =)"
	if [ "$(echo "$IFSTATUS" | grep ^wpa_state | cut -f 2 -d =)" != "COMPLETED" ] \
	 || [ -z "$(echo "$IFSTATUS" | grep ^ip_address | cut -f 2 -d =)" ] \
	 || [ -z "$SSID" ]; then
		echo "$(gettext 'Not Connected')"
	else	
		. gettext.sh
		echo "`eval_gettext \"Connected to \\\$SSID\"`"
	fi
}
export -f   current_status

#############################################

function if_connected {
	#set -x
	STATE="$(wpa_cli -i $INTERFACE status 2>/dev/null | grep wpa_state | cut -f 2 -d =)"
	[ -z "$STATE" -o "$STATE" = "DISCONNECTED" ] \
		&& return 1
	return 0
}
export -f   if_connected

#############################################

function delete_profile {
	#set -x
	ID="$1"
	
	if [[ -z $ID ]] ; then
	        Xdialog  --title "Frisbee" --msgbox "$(gettext 'Please select a network profile')" 0 0
	        return
	fi
	
	wpa_cli -i $INTERFACE  list_networks | grep "^$ID	"|grep CURRENT
	[[ $? == 0 ]] && CURRENT=yes
	
	RESULT=`wpa_cli -i $INTERFACE remove_network $ID`
	if [[ "$RESULT" != "OK" ]] ; then
	        Xdialog  --title "Frisbee" --msgbox "$(gettext 'Failed to delete network profile')" 0 0
	        return
	fi
	
	RESULT=`wpa_cli -i $INTERFACE save_config`
	if [[ "$RESULT" != "OK" ]] ; then
	        Xdialog  --title "Frisbee" --msgbox "$(gettext 'Failed to save configuration')" 0 0
	        return
	fi
	
	 if [[ ! -z $CURRENT ]] ; then
		disconnect
		connect
		wpa_cli -i $INTERFACE scan >/dev/null
		sleep 2
		reset_dhcp&  
	fi

	Xdialog  --title "Frisbee" --msgbox "$(gettext 'Profile deleted')" 0 0

}
export -f  delete_profile

#############################################

function get_auth {
	ID="$1"
	
	if [ -z $ID ]  ; then
	        return
	fi
	
	
	AUTH=`wpa_cli -i $INTERFACE get_network $ID key_mgmt`
	if [[ "$AUTH" != "FAIL" ]] ; then
	        echo $AUTH
	        return
	fi
}
export -f   get_auth

#############################################

function get_bssid {
	ID="$1"
	
	if [ -z $ID ] ; then 
	        return
	fi
	
	
	bssid=`wpa_cli -i $INTERFACE get_network $ID bssid`
	if [[ $bssid == "FAIL" ]] ; then
	        return
	fi
	echo $bssid |tr -d \"
}
export -f   get_bssid

#############################################

function get_dhcp_auto {
	IF=$1
	[ -z "$IF" ] || \
	cat /etc/frisbee/dhcpcd.conf | grep denyinterfaces | grep -q $IF
	if [[ $? == 0 ]] ; then
	        echo false
	else
	        cat /etc/frisbee/dhcpcd.conf | grep -q "^#$IF static\$"
	        if [[ $? == 0 ]] ; then
	                echo false
	        else
	                echo true
	        fi
	fi 
}
export -f   get_dhcp_auto

#############################################

function get_dhcp_auto_wireless {
	SSID=$1
	cat /etc/frisbee/dhcpcd.conf | grep -q "^#$SSID static\$"
	if [[ $? == 0 ]] ; then
	    echo false
	else
		echo true
	fi
}
export -f   get_dhcp_auto_wireless

#############################################

function get_dhcp_ignored {
	INTERFACE="$1"
	[ -z "$INTERFACE" ] || \
	cat /etc/frisbee/dhcpcd.conf | grep denyinterfaces | grep -q $INTERFACE
	if [[ $? == 0 ]] ; then
	        echo true
	else
	        echo false
	fi 
}
export -f  get_dhcp_ignored 

#############################################

function get_dns1 {
	IF=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$IF static\$" -A4|tail -n 1|cut -d= -f2 |cut -d" " -f1
}
export -f   get_dns1

#############################################

function get_dns1_wireless {
	SSID=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$" -A5|tail -n 1|cut -d= -f2 |cut -d" " -f1
}
export -f   get_dns1_wireless

#############################################

function get_dns2 {
	IF=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$IF static\$" -A4|tail -n 1|cut -d= -f2 |cut -d" " -f2 
}
export -f   get_dns2

#############################################

function get_dns2_wireless {
	SSID=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$" -A5|tail -n 1|cut -d= -f2 |cut -d" " -f2 
}
export -f   get_dns2_wireless

#############################################

function get_enabled {
	ID="$1"
	
	
	if [ -z $ID ] ; then
	        return
	fi
	
	disabled=`wpa_cli -i $INTERFACE get_network $ID disabled`
	if [[ "$disabled" == "0" ]] ; then
	        echo "true"
	else
	        echo "false"
	fi
}

export -f  get_enabled

#############################################

function get_enc {
	ID=`echo $1`
	if [ -z $ID ]  ; then
	        return
	fi
	
	AUTH=`wpa_cli -i $INTERFACE get_network $ID key_mgmt`
	if [[ "$AUTH" == "NONE" ]] ; then
	        WEP=`wpa_cli -i $INTERFACE get_network $ID wep_key0`
	        if [[ "$WEP" != "FAIL" ]] ; then
	                echo "WEP" 
	                return
	        else
	                echo "OPEN"
	                return
	        fi
	fi
	ENC=`wpa_cli -i $INTERFACE get_network $ID pairwise`
	if [[ "$ENC" != "FAIL" ]] ; then
	        echo $ENC
	        return
	fi
}
export -f   get_enc

#############################################

function get_gateway {
	IF=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$IF static\$" -A3|tail -n 1|cut -d= -f2
}
export -f   get_gateway

#############################################

function get_gateway_wireless {
	SSID=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$" -A4|tail -n 1|cut -d= -f2
}
export -f   get_gateway_wireless

#############################################

function get_interfaces {
	iwconfig 2>&1 |grep ^[a-z]|while read if line ; do
	        if [[ $line == "no wireless extensions." ]] ; then
	                line=wired
	        else
	                line=wireless
	        fi
	        if [[ $if == lo ]] ; then
	                line=loopback
	        elif [[ $if == ppp* ]] ; then
	                line=ppp
	        fi
	        cat /etc/frisbee/dhcpcd.conf | grep denyinterfaces | grep -q $if
	        if [[ $? == 0 ]] ; then
	                state=ignored
	        else
	                cat /etc/frisbee/dhcpcd.conf | grep "^#$if static\$"  | grep -q $if
	                if [[ $? == 0 ]] ; then
	                        state=static
	                else
	                        state=auto
	                fi
	                
	        fi
	        echo "$if|$line|$state"
	done    
	echo
	echo
	echo
	echo
}
export -f   get_interfaces 

#############################################
	
function get_key {
	ID="$1"
	if [ -z $ID ]  ; then
	        return
	fi
	
	wep_key=`wpa_cli -i $INTERFACE get_network $ID wep_key0`
	if [[ $wep_key != "FAIL" ]] ; then
	        echo "[Key Configured]"
	fi
	psk=`wpa_cli -i $INTERFACE get_network $ID psk`
	if [[ $psk != "FAIL" ]] ; then
	        echo "[Key Configured]"
	fi
}
export -f   get_key 

#############################################

function get_log {
	sync
	cat /tmp/wpa_supplicant.log 2>/dev/null |tail -n 500 > /tmp/.frisbee/wpa_supplicant_log_tail
}
export -f   get_log 

#############################################

function get_mask {
	IF=$1
	  CIDR=`cat /etc/frisbee/dhcpcd.conf | grep "^#$IF static\$" -A2|tail -n 1 |cut -d= -f2|cut -d\/ -f2`
	 [ ! $CIDR ] && return
	  full_octets=$((CIDR/8))
	  partial_octet=$((CIDR%8))
	
	  for ((i=0;i<4;i+=1)); do
	    if [ $i -lt $full_octets ]; then
	      mask="${mask}255"
	    elif [ $i -eq $full_octets ]; then
	      mask="${mask}$((256 - 2**(8-partial_octet)))"
	    else
	      mask="${mask}0"
	    fi  
	    test $i -lt 3 && mask="${mask}."
	  done
	
	  echo $mask
}
export -f   get_mask

#############################################

function get_mask_wireless {
	SSID=$1
	  CIDR=`cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$" -A3|tail -n 1 |cut -d= -f2|cut -d\/ -f2`
	 [ ! $CIDR ] && return
	  full_octets=$((CIDR/8))
	  partial_octet=$((CIDR%8))
	
	  for ((i=0;i<4;i+=1)); do
	    if [ $i -lt $full_octets ]; then
	      mask="${mask}255"
	    elif [ $i -eq $full_octets ]; then
	      mask="${mask}$((256 - 2**(8-partial_octet)))"
	    else
	      mask="${mask}0"
	    fi  
	    test $i -lt 3 && mask="${mask}."
	  done
	
	  echo $mask
}
export -f   get_mask_wireless

#############################################

function get_priority { 
	ID="$1"
	if [ -z $ID ] ; then
	        return
	fi
	
	pri=`wpa_cli -i $INTERFACE get_network $ID priority|tr -d \"`
	if [[ $pri != "FAIL" ]] ; then
	        echo $pri
	fi 

}
export -f   get_priority

#############################################

function get_proto {
	ID="$1"
	
	if [ -z $ID ]  ; then
	        return
	fi
	
	AUTH=`wpa_cli -i $INTERFACE get_network $ID proto`
	if [[ "$AUTH" == "FAIL" ]] ; then
	        return
	fi
	
	if [[ $AUTH == "RSN" ]] ; then
	        echo "WPA2"
	        return
	fi
	
	if [[ $AUTH == "WPA" ]] ; then
	        echo "WPA"
	        return
	fi
	
	echo "NONE"
}
export -f   get_proto

#############################################

function get_auth_alg {
	ID="$1"
	
	if [ -z $ID ]  ; then
	        return
	fi
	
	AUTH=`wpa_cli -i $INTERFACE get_network $ID auth_alg`
	if [[ "$AUTH" == "FAIL" ]] ; then
	        return
	fi
	
	[ -z $AUTH ] && AUTH=NONE
	
	echo "$AUTH" 
	
	return
}
export -f   get_auth_alg

#############################################

function get_scan_results {
	if_connected || return
	wpa_cli -i $INTERFACE scan >/dev/null
	wpa_cli scan_results -i $INTERFACE |grep ^..:..: |sed 's/	/|/g'
	
	for i in 1 2 3 4 5 6 7 8 9 10 11 ; do 
	echo
	done
}
export -f   get_scan_results

#############################################

function get_ssid {
	ID=`cat /tmp/.frisbee/id 2>/dev/null`
	if [ -z $ID ] ; then
	        return
	fi
	
	ssid=`wpa_cli -i $INTERFACE get_network $ID ssid | tr -d \"`
	if [[ $ssid != "FAIL" ]] ; then
	        echo $ssid
	fi 
}
export -f   get_ssid 

#############################################

function get_ssids {
	wpa_cli -i $INTERFACE list_networks |egrep  ^[0123456789] | sed 's/	/|/g'| while read line ; do
	echo $line
	done 
	echo
	echo
	echo
	echo
}
export -f   get_ssids 

#############################################

function get_static_ip {
	IF=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$IF static\$" -A2|tail -n 1|cut -d= -f2|cut -d\/ -f1
}
export -f   get_static_ip

#############################################

function get_static_ip_wireless {
	SSID=$1
	cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$" -A3|tail -n 1|cut -d= -f2|cut -d\/ -f1
}
export -f   get_static_ip_wireless

#############################################

function get_status {
	echo "Wpa_supplicant Status:" > /tmp/.frisbee/status 
	echo "-------------------------------------------------" >> /tmp/.frisbee/status 
	wpa_cli -i $INTERFACE status >> /tmp/.frisbee/status 2>/dev/null
	[ $? -ne 0 ] && echo "Not Active" >> /tmp/.frisbee/status
	echo "-------------------------------------------------" >> /tmp/.frisbee/status 
	echo >> /tmp/.frisbee/status 
	iwconfig $INTERFACE >> /tmp/.frisbee/status 2>&1
	ifconfig $INTERFACE >> /tmp/.frisbee/status 2>&1
}
export -f   get_status 

#############################################

function start_dhcp {
	setsid dhcpcd -b -d $(dhcpcd_dropwait_option) -f /etc/frisbee/dhcpcd.conf
	sleep 2
}
export -f start_dhcp

#############################################

function reset_dhcp {
	dhcpcd -k
	killall -9 dhcpcd
	killall wpa_cli 2>/dev/null
	start_dhcp
}
export -f reset_dhcp

#############################################

function start_wpa {
	ifconfig $INTERFACE up
	if grep -q '^wireless_autostart=1' /etc/frisbee/frisbee.conf ; then
		WPACFGFILE="/etc/frisbee/wpa_supplicant.conf"
		rm -f /tmp/wpa_supplicant.conf
	else
		WPACFGFILE="/tmp/wpa_supplicant.conf"
		[ -f $WPACFGFILE ] \
		 || sed -e '/^network=/,/}/d' /etc/frisbee/wpa_supplicant.conf > $WPACFGFILE
	fi
	if grep -q '^wpa_log_mode=1' /etc/frisbee/frisbee.conf ; then
		setsid wpa_supplicant -B -d -Dwext -i$INTERFACE -c$WPACFGFILE > /tmp/wpa_supplicant.log
	else
		setsid wpa_supplicant -B -Dwext -i$INTERFACE -c$WPACFGFILE > /tmp/wpa_supplicant.log
	fi
	wpa_cli -i $INTERFACE scan >/dev/null

}
export -f start_wpa

#############################################

function reset_wpa {
	pidof wpa_supplicant
	if [[ $? = 0 ]] ; then
		wpa_cli terminate
		sleep 2 
		wpa_cli terminate 2>/dev/null
	fi
	killall -9 wpa_supplicant 2>/dev/null
	rm -rf /var/run/wpa_supplicant/*
	rm -f /tmp/wpa_supplicant.log
	start_wpa
	connect
}
export -f reset_wpa 

#############################################

function roam {
		wpa_cli -i $INTERFACE list_networks | while read LINE ; do
	                ID=$(echo "$LINE" |cut -f1)
	                wpa_cli -i $INTERFACE enable_network $ID
	done
	wpa_cli -i $INTERFACE save_config
	sleep 1
	
	reset_wpa
}
export -f roam

#############################################

function cleanfile {
	ECHO=1
	while read LINE ; do 
	        [[ $LINE == "#$1" ]] && ECHO=0
	        [[ $LINE == "#$1 static" ]] && ECHO=0
	        [[ $ECHO == 2 ]] && ECHO=1
	        [[ $LINE == "#end $1" ]] && ECHO=2
	        [[ $ECHO == 1 ]] && echo $LINE
	done
}
export -f   cleanfile 

#############################################

function update_dhcp {
	IF=`echo $1|cut -f1 -d\|`
	IGNORE=`echo $1|cut -f2 -d\|`
	AUTO=`echo $1|cut -f3 -d\|`
	IP=`echo $1|cut -f4 -d\|`
	SUBNET=`echo $1|cut -f5 -d\|`
	GATEWAY=`echo $1|cut -f6 -d\|`
	DNS1=`echo $1|cut -f7 -d\|`
	DNS2=`echo $1|cut -f8 -d\|`
	
	if [ ! $IF ]  ;then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Please select an interface')" 0 0 ; return 
	fi
	
	IGNOREORIG=`cat /etc/frisbee/dhcpcd.conf |grep denyinterfaces`
	IGNORELIST=`echo $IGNOREORIG |cut -f2 -d" "|tr , " "`
	
	for i in $IGNORELIST ; do
	        if [[ $IF == $i ]] ; then
	                IGNORED=1
	        fi
	done
	
	if [[ $IGNORE == true ]] ; then
	        if [[ $IGNORED == 1 ]] ; then
	                return
	        else
	                IGNORELIST="$IGNORELIST $IF"
	        fi
	fi
	
	if [[ $AUTO == true && $IGNORED == 1 ]] ; then
	        
	        IGNORELIST=`for i in $IGNORELIST ; do
	                if [[ $IF != $i ]] ; then
	                        echo -n "$i "
	                fi
	        done
	        echo`
	fi
	
	IGNORELIST=`echo "$IGNORELIST"|tr " " ,|sed 's/,$//'`
	IGNORELIST=`echo "denyinterfaces $IGNORELIST"`
	
	if [[ "$IGNORELIST" != "$IGNOREORIG" ]] ; then
	        cat /etc/frisbee/dhcpcd.conf |sed "s/^denyinterfaces.*$/$IGNORELIST/" > /tmp/dhcpcd.conf 
	        mv /tmp/dhcpcd.conf /etc/frisbee
	        reset_dhcp&
	fi
	
	if [[ $AUTO == true || $IGNORE == true ]] ; then
	        cat /etc/frisbee/dhcpcd.conf | grep "interface $IF"
	        if [[ $? == 0 ]] ; then
	                cat /etc/frisbee/dhcpcd.conf |cleanfile $IF >  /tmp/dhcpcd.conf 
	                mv /tmp/dhcpcd.conf /etc/frisbee
	        fi
	        return
	fi
	
	
	IP=`echo $IP|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $IP ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid IP address')" 0 0 ; return 
	fi
	GATEWAY=`echo $GATEWAY|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $GATEWAY ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid gateway')" 0 0  ; return 
	fi
	SUBNET=`echo $SUBNET|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $SUBNET ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid subnet mask')" 0 0  ; return 
	fi
	DNS1=`echo $DNS1|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $DNS1 ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid IP Address for DNS Server 1')" 0 0  ; return 
	fi
	DNS2=`echo $DNS2|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	SUBNET=`echo $SUBNET|tr "." " "`
	CIDR=0
	for dec in $SUBNET ; do
	        case $dec in
	            255) let CIDR+=8;;
	            254) let CIDR+=7;;
	            252) let CIDR+=6;;
	            248) let CIDR+=5;;
	            240) let CIDR+=4;;
	            224) let CIDR+=3;;
	            192) let CIDR+=2;;
	            128) let CIDR+=1;;
	            0);;
	            *) Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid subnet mask')" 0 0  ; return ;;
	        esac
	done
	
	
	cat /etc/frisbee/dhcpcd.conf |cleanfile $IF > /tmp/dhcpcd.conf 
	echo "#$IF static" >> /tmp/dhcpcd.conf
	echo "interface $IF " >> /tmp/dhcpcd.conf
	echo "static ip_address=${IP}/${CIDR} " >> /tmp/dhcpcd.conf
	echo "static routers=$GATEWAY " >> /tmp/dhcpcd.conf
	echo "static domain_name_servers=$DNS1 $DNS2" >> /tmp/dhcpcd.conf
	echo "#end $IF" >> /tmp/dhcpcd.conf
	mv /tmp/dhcpcd.conf /etc/frisbee
	reset_dhcp&
}
export -f   update_dhcp

#############################################

function update_dhcp_wireless {
	#set -x
	SSID=`echo $1|cut -f1 -d\|`
	AUTO=`echo $1|cut -f2 -d\|`
	IP=`echo $1|cut -f3 -d\|`
	SUBNET=`echo $1|cut -f4 -d\|`
	GATEWAY=`echo $1|cut -f5 -d\|`
	DNS1=`echo $1|cut -f6 -d\|`
	DNS2=`echo $1|cut -f7 -d\|`

	if [[ -z $SSID ]] ; then
	        return
	fi
	
	if [[ $AUTO == true ]] ; then
	     cat /etc/frisbee/dhcpcd.conf | grep "^#$SSID static\$"
	     if [[ $? == 0 ]] ; then
	           cat /etc/frisbee/dhcpcd.conf |cleanfile $SSID >  /tmp/dhcpcd.conf 
	           mv /tmp/dhcpcd.conf /etc/frisbee
	    fi
	    return
	fi
	
	IP=`echo $IP|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $IP ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid IP address')" 0 0 ; return 
	fi
	GATEWAY=`echo $GATEWAY|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $GATEWAY ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid gateway')" 0 0  ; return 
	fi
	SUBNET=`echo $SUBNET|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $SUBNET ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid subnet mask')" 0 0  ; return 
	fi
	DNS1=`echo $DNS1|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	if [ ! $DNS1 ] ; then
	         Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid IP address for DNS Server 1')" 0 0  ; return 
	fi
	DNS2=`echo $DNS2|awk --posix -F"." '{ if ($0~/^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1<=255 && $2<=255 && $3<=255 && $4<=255) print $0}'`
	SUBNET=`echo $SUBNET|tr "." " "`
	CIDR=0
	for dec in $SUBNET ; do
	        case $dec in
	            255) let CIDR+=8;;
	            254) let CIDR+=7;;
	            252) let CIDR+=6;;
	            248) let CIDR+=5;;
	            240) let CIDR+=4;;
	            224) let CIDR+=3;;
	            192) let CIDR+=2;;
	            128) let CIDR+=1;;
	            0);;
	            *) Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid subnet mask')" 0 0  ; return ;;
	        esac
	done
	
	
	cat /etc/frisbee/dhcpcd.conf |cleanfile $SSID > /tmp/dhcpcd.conf 
	echo "#$SSID static" >> /tmp/dhcpcd.conf
	echo "interface $INTERFACE" >> /tmp/dhcpcd.conf
	echo "ssid $SSID" >> /tmp/dhcpcd.conf
	echo "static ip_address=${IP}/${CIDR} " >> /tmp/dhcpcd.conf
	echo "static routers=$GATEWAY " >> /tmp/dhcpcd.conf
	echo "static domain_name_servers=$DNS1 $DNS2" >> /tmp/dhcpcd.conf
	echo "#end $SSID" >> /tmp/dhcpcd.conf
	mv /tmp/dhcpcd.conf /etc/frisbee
	reset_dhcp&
}
export -f   update_dhcp_wireless

#############################################

function update_profile {
	ID=`echo $1|cut -f1 -d\|`
	SSID=`echo $1|cut -f2 -d\|`
	BSSID=`echo $1|cut -f3 -d\|`
	ENCKEY=`echo $1|cut -f4 -d\|`
	PRIORITY=`echo $1|cut -f5 -d\|`
	ENABLED=`echo $1|cut -f6 -d\|`
	AUTH=`echo $1|cut -f7 -d\|`
	ENC=`echo $1|cut -f8 -d\|`
	echo "$ID $SSID $BSSID $ENCKEY $PRIORITY $ENABLED"
	
	wpa_cli  list_networks | grep "^$ID    "|grep CURRENT
	[[ $? == 0 ]] && CURRENT=yes
	
	
	if [[ -z $ID ]] ; then
	        Xdialog  --title "Frisbee" --msgbox "$(gettext 'Please select a profile to update')" 0 0
	        return
	fi
	        
	
	RESULT=`wpa_cli -i $INTERFACE set_network $ID priority $PRIORITY`
	if [[ "$RESULT" != "OK" ]] ; then
	        Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid priority')" 0 0
	        wpa_cli -i $INTERFACE reconfigure
	        return
	fi
	
	if [[ "$ENABLED" == "true" ]] ; then
	        RESULT=`wpa_cli -i $INTERFACE enable_network $ID`
	        if [[ "$RESULT" != "OK" ]] ; then
	                Xdialog  --title "Frisbee" --msgbox "$(gettext 'Failed to enable network')" 0 0
	                wpa_cli -i $INTERFACE reconfigure
	                return
	        fi
	else
	        RESULT=`wpa_cli -i $INTERFACE disable_network $ID`
	        if [[ "$RESULT" != "OK" ]] ; then
	                Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to disable network')" 0 0
	                wpa_cli -i $INTERFACE reconfigure
	                return
	        fi
	fi
	
	if [[ $BSSID != '' ]] ; then
	        RESULT=`wpa_cli -i $INTERFACE set_network $ID bssid $BSSID`
	        if [[ "$RESULT" != "OK" ]] ; then
	                Xdialog  --title "Frisbee" --msgbox "$(gettext 'Invalid BSSID')" 0 0
	                wpa_cli -i $INTERFACE reconfigure
	                return
	        fi
	fi
	
	if [[ ("$AUTH" == "WPA-PSK" ||  "$ENC" == "WEP") && "$ENCKEY" != "[Key Configured]"  ]] ; then
	        if [ -z $ENCKEY ] ; then
	                Xdialog  --title "Frisbee" --msgbox "$(gettext 'Invalid Password')" 0 0   
	                wpa_cli -i $INTERFACE reconfigure
	                return
	        fi
	        if [[ "$ENC" == "WEP" ]] ; then
	                echo "$ENCKEY" |egrep '^([[:xdigit:]]){10}$|^([[:xdigit:]]){26}$|^([[:xdigit:]]){32}$'
	                [[ $? == 0 ]] && HEX=1 
	                if [[ "$HEX" == "1" ]] ; then
	                        RESULT=`wpa_cli -i $INTERFACE set_network $ID wep_key0 $ENCKEY`
	                else
	                        RESULT=`wpa_cli -i $INTERFACE set_network $ID wep_key0 \""${ENCKEY}"\"`
	                fi
	                if [[ "$RESULT" != "OK" ]] ; then
	                        Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid Password')" 0 0
	                        wpa_cli -i $INTERFACE reconfigure
	                        return
	                fi
	
	        else
	                echo "$ENCKEY" |egrep '^([[:xdigit:]]){64}$'
	                [[ $? == 0 ]] && HEX=1
	                if [[ "$HEX" == "1" ]] ; then
	                        RESULT=`wpa_cli -i $INTERFACE set_network $ID psk $ENCKEY`
	                else
	                        RESULT=`wpa_cli -i $INTERFACE set_network $ID psk \""${ENCKEY}"\"`
	                fi
	                if [[ "$RESULT" != "OK" ]] ; then
	                        Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Invalid Password')" 0 0
	                        wpa_cli -i $INTERFACE reconfigure
	                        return
	                fi
	        fi
	fi
	
	
	
	RESULT=`wpa_cli -i $INTERFACE save_config`
	if [[ "$RESULT" != "OK" ]] ; then
	        Xdialog  --title "Frisbee"  --msgbox "$(gettext 'Failed to save configuration')" 0 0
	        wpa_cli -i $INTERFACE reconfigure
	        return
	fi
	#[[ $CURRENT == "yes" ]] && reset_wpa
	reset_wpa
	reset_dhcp
	
	Xdialog  --title "Frisbee" --msgbox "$(gettext 'Profile updated')" 0 0
}
export -f   update_profile

#############################################

function connect {
	#set -x
	prof=`wpa_cli -i $INTERFACE list_networks|tail -n 1|cut -d " " -f1 `
	if [[ $prof == "network" ]] ; then
	        return 1
	fi
	wpa_cli -i $INTERFACE status|grep -q COMPLETED 
	if [[ $? == 0 ]] ; then
	        #dhcpcd -n 
	        return 0
	fi
	        ifconfig $INTERFACE up
	        pidof wpa_supplicant
	        if [[ $? != 0 ]] ; then
	                start_wpa
	        fi
	        pidof dhcpcd
	        if [[ $? != 0 ]] ; then
	                start_dhcp
	        fi
	        wpa_cli -i $INTERFACE reconnect 
	        #dhcpcd -n 
	
	sleep 2
	return 0
}
export -f connect

#############################################


function disconnect {
	[ "$INTERFACE" = "none" ] && return
	wpa_cli -i $INTERFACE disconnect 2>/dev/null
	sleep 1 
	wpa_cli -i $INTERFACE disconnect 2>/dev/null
	killall wait_disconnect 
	dhcpcd -k $INTERFACE 

}
export -f disconnect

#############################################

function wait_disconnect {
	for i in 1 2 3 ; do
	        sleep 10
	        wpa_cli -i $INTERFACE status | grep COMPLETED
	        if [[ $? == 0 ]] ; then
				return
	        fi
	done

dhcpcd -k $INTERFACE 
}
export -f wait_disconnect

#############################################


function change_if {
	IFACES=''
	[[ `cat /etc/frisbee/interfaces` == "" ]] && return
	for i in `cat /etc/frisbee/interfaces` ; do IFACES="$IFACES $i $i $i "; done
	Xdialog  --title "Frisbee" --no-tags --radiolist "$(gettext 'Please select an interface')"  0 0 0 $IFACES > /etc/frisbee/userif 2>&1
	IF=`cat /etc/frisbee/userif`
	[ ! -z $IF ] && echo "$IF" > /etc/frisbee/interface
	#reset_wpa
}
export -f change_if

#############################################
