#!/bin/bash
#Frisbee Copyright Jemimah Ruhala 2012 jemimah@gmx.com
#Localization - argolance - January 2013

export TEXTDOMAIN=frisbee
export OUTPUT_CHARSET=UTF-8
. gettext.sh

. /usr/local/frisbee/func

[ -f /tmp/.frisbee_newly_installed ] \
 && rm -f /tmp/.frisbee_newly_installed \
 && sed -i -e '/^frisbee_mode=/ s/=.*/=0/' /etc/frisbee/frisbee.conf #reset for mode_enable

if grep -q '^frisbee_mode=0' /etc/frisbee/frisbee.conf;then
	gtkdialog-splash -placement center -bg orange -text "$(gettext 'Checking network devices')" &
	SPLASHPID=$!
	frisbee_mode_enable
	setsid /etc/init.d/frisbee start
	kill $SPLASHPID 2>/dev/null
fi

rm -f /tmp/.frisbee/* 2>/dev/null

if [[ `cat /etc/frisbee/interfaces |wc -l` > 1 ]] ; then
	MULTIFS=sensitive=\"true\"
else
	MULTIFS=sensitive=\"false\"
fi

###Get the wireless device, i.e. ath0, wlan0, ra0, etc...
WIRELESSEN=sensitive=\"true\"
export INTERFACE=`cat /etc/frisbee/interface 2>/dev/null`
if [[ -z $INTERFACE ]] ; then
	INTERFACE=none
	WIRELESSEN=sensitive=\"false\"
fi

#pre-set buttons for wifi option...
if grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf;then
	ENABLEWIRELESS=sensitive=\"true\"
else
	ENABLEWIRELESS=sensitive=\"false\"
fi

grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf \
 && if_connected \
 && wpa_cli -i $INTERFACE scan >/dev/null &
get_log

function profile_editor {
	export TEXTDOMAIN=frisbee
	export OUTPUT_CHARSET=UTF-8
	. gettext.sh

	export PROFEDIT="
	<window title=\"Frisbee - $(gettext 'Manage Saved Network Profiles')\"icon-name=\"network-idle\" window-position=\"1\">
	<vbox>
	    <frame $(gettext 'Select a Profile to Edit or Delete')>
	     <table>
              <width>600</width>
              <height>100</height>
	      <label>$(gettext 'ID   | SSID                               |BSSID                 |Flags')</label>
	      <variable>ID</variable>
	      <input>get_ssids</input>
	      <action>echo \$ID > /tmp/.frisbee/id</action>
	      <action>clear:ENCKEY</action>
	      <action>refresh:ENCKEY</action>
	      <action>refresh:SSID</action>
	      <action>clear:PRIORITY</action>
	      <action>refresh:PRIORITY</action>
	      <action>clear:BSSID</action>
	      <action>refresh:BSSID</action>
	      <action>refresh:AUTH</action>
	      <action>refresh:AUTHALG</action>
	      <action>refresh:PROTO</action>
	      <action>refresh:ENC</action>
	      <action>refresh:ENABLED</action>
	      <action>refresh:AUTODHCP</action>
	     </table>
	     <hbox>
	      <vbox>
	       <hbox>
	         <text tooltip-text=\"$(gettext 'To change the SSID, you must delete the profile and add it again')\">
	         <variable>SSID</variable>
	         <input>echo -n SSID:\ ; get_ssid</input> 
	        </text>
	      <text tooltip-text=\"$(gettext 'If you need to change this, delete the profile and add the network again; encryption settings are automatically detected')\">
	       <variable>AUTH</variable>
	       <input>echo -n Auth-type:\ ;get_auth \"\$ID\"</input> 
	      </text>
	      <text  tooltip-text=\"$(gettext 'If you need to change this, delete the profile and add the network again; encryption settings are automatically detected')\">
	       <variable>PROTO</variable>
	       <input>echo -n Proto:\ ; get_proto \"\$ID\"</input>
	      </text>
	      <text  tooltip-text=\"$(gettext 'If you need to change this, delete the profile and add the network again; encryption settings are automatically detected')\">
	       <variable>AUTHALG</variable>
	       <input>echo -n Auth-Alg:\ ; get_auth_alg \"\$ID\"</input>
	      </text>
	     <text  tooltip-text=\"$(gettext 'If you need to change this, delete the profile and add the network again; encryption settings are automatically detected')\">
	       <variable>ENC</variable>
	       <input>echo -n Encryption:\ ; get_enc \"\$ID\"</input>
	      </text>
	       </hbox>
	     <hbox>
	     <checkbox tooltip-text=\"$(gettext 'Wpa_supplicant will not try to connect to a network that is disabled')\">
	      <label>\"$(gettext 'Network Enabled')\"</label>
	      <variable>ENABLED</variable>
	      <input>get_enabled \"\$ID\"</input>
	     </checkbox>
	      <hbox>
	       <text>
	        <label>\"$(gettext 'Priority')\"</label>
	       </text>
	       <entry tooltip-text=\"$(gettext 'Wpa_supplicant will prefer to connect to the network with the largest number in the Priority field')\">
	        <variable>PRIORITY</variable>
	        <input>get_priority \"\$ID\"</input> 
	       </entry>
	       </hbox>
	       </hbox>
	       <hbox>
	        <text>
	         <label>\"$(gettext 'Key or Password')\"</label>
	        </text>
	        <entry tooltip-text=\"$(gettext 'Enter your password or HEX key here; for best results with WEP, use the HEX key')\">
	         <visible>password</visible>
	         <variable>ENCKEY</variable>
	         <input>echo</input> 
	         <input>get_key \"\$ID\"</input> 
	        </entry>
	       </hbox>
	     </vbox>
	   </hbox>
	  </frame>
	  <frame $(gettext 'DHCP Settings')>
	   <hbox>
     <vbox>
        <checkbox tooltip-text=\"$(gettext 'Request IP address and routing information from a DHCP server')\">
        <label>$(gettext 'Auto DHCP')</label>
        <variable>AUTODHCP</variable>
        <input>get_dhcp_auto_wireless \"\$(get_ssid)\"</input>
        <action>if true clear:IP</action>
        <action>if true disable:IP</action>
        <action>if true disable:IPLAB</action>
        <action>if true clear:SUBNET</action>
        <action>if true disable:SUBNET</action>
        <action>if true disable:SUBNETLAB</action>
        <action>if true clear:GATEWAY</action>
        <action>if true disable:GATEWAY</action>
        <action>if true disable:GATEWAYLAB</action>
        <action>if true clear:DNS1</action>
        <action>if true disable:DNS1</action>
        <action>if true disable:DNS1LAB</action>
        <action>if true clear:DNS2</action>
        <action>if true disable:DNS2</action>
        <action>if true disable:DNS2LAB</action>
        <action>if false refresh:IP</action>
        <action>if false enable:IP</action>
        <action>if false enable:IPLAB</action>
        <action>if false refresh:SUBNET</action>
        <action>if false enable:SUBNET</action>
        <action>if false enable:SUBNETLAB</action>
        <action>if false refresh:GATEWAY</action>
        <action>if false enable:GATEWAY</action>
        <action>if false enable:GATEWAYLAB</action>
        <action>if false refresh:DNS1</action>
        <action>if false enable:DNS1</action>
        <action>if false enable:DNS1LAB</action>
        <action>if false refresh:DNS2</action>
        <action>if false enable:DNS2</action>
        <action>if false enable:DNS2LAB</action>
       </checkbox>
      <hbox>
       <text>
       <visible>disabled</visible>
       <variable>IPLAB</variable><label>$(gettext 'Static IP Address')</label></text>
       <entry tooltip-text=\"$(gettext 'Enter static IP address')\">
        <visible>disabled</visible>
        <variable>IP</variable>
        <input>get_static_ip_wireless \"\$(get_ssid)\"</input>
       </entry>
      </hbox>
      <hbox>
       <text>
        <visible>disabled</visible>
        <variable>SUBNETLAB</variable>
        <label>\"$(gettext 'Subnet Mask')\"</label>
       </text>
       <entry tooltip-text=\"$(gettext 'Enter subnet mask')\">
        <visible>disabled</visible>
        <variable>SUBNET</variable>
        <input>get_mask_wireless \"\$(get_ssid)\"</input>
       </entry>
      </hbox>
      </vbox>
      <vbox>
       <hbox>
       <text>
        <visible>disabled</visible>
        <variable>GATEWAYLAB</variable><label>$(gettext 'Gateway')</label></text>
        <entry tooltip-text=\"$(gettext 'Enter gateway IP address')\">
         <visible>disabled</visible>
         <variable>GATEWAY</variable> 
         <input>get_gateway_wireless \"\$(get_ssid)\"</input>
        </entry>
       </hbox>
      <hbox>
       <text>
        <visible>disabled</visible>
        <variable>DNS1LAB</variable><label>$(gettext 'DNS Server 1')</label>
       </text>
       <entry tooltip-text=\"$(gettext 'Enter the IP address for the DNS server')\">
        <visible>disabled</visible>
        <variable>DNS1</variable>
        <input>get_dns1_wireless \"\$(get_ssid)\"</input>
       </entry>
      </hbox>
      <hbox>
         <text>
          <visible>disabled</visible>
          <variable>DNS2LAB</variable><label>$(gettext 'DNS Server 2')</label>
         </text>
	 <entry tooltip-text=\"$(gettext 'Enter IP address for backup DNS server; this field is optional')\">
          <visible>disabled</visible>
          <variable>DNS2</variable>
          <input>get_dns2_wireless \"\$(get_ssid)\"</input>
         </entry>
        </hbox>
       </vbox>
      </hbox>
      </frame>
	  <hbox space-expand=\"true\">
	   <button tooltip-text=\"$(gettext 'Select a profile, make changes, then click this button to save the changes')\">
	    <input file stock=\"gtk-save\"></input>
	    <label>$(gettext 'Save Profile')</label>
	    <action>update_dhcp_wireless \"\$(get_ssid)|\$AUTODHCP|\$IP|\$SUBNET|\$GATEWAY|\$DNS1|\$DNS2\"</action>
	    <action>update_profile \"\$ID|\$SSID|\$BSSID|\$ENCKEY|\$PRIORITY|\$ENABLED|\$AUTH|\$ENC\"</action>
	    <action>clear:ID</action>
	    <action>refresh:ID</action>
	    <action>refresh:ENCKEY</action>
	    <action>refresh:SSID</action>
	    <action>refresh:PRIORITY</action>
	    <action>refresh:BSSID</action>
	    <action>refresh:AUTH</action>
	    <action>refresh:PROTO</action>
	    <action>refresh:AUTHALG</action>
	    <action>refresh:ENC</action>
	    <action>refresh:ENABLED</action>
	   </button>
	   <button tooltip-text=\"$(gettext 'Delete the selected profile')\" >
	    <input file stock=\"gtk-delete\"></input>
	    <label>$(gettext 'Delete Profile')</label>
	    <action>delete_profile \"\$ID\"</action>
	    <action>clear:ID</action>
	    <action>refresh:ID</action>
	    <action>refresh:ENCKEY</action>
	    <action>refresh:SSID</action>
	    <action>refresh:PRIORITY</action>
	    <action>refresh:BSSID</action>
	    <action>refresh:AUTH</action>
	    <action>refresh:PROTO</action>
	    <action>refresh:AUTHALG</action>
	    <action>refresh:ENC</action>
	    <action>refresh:ENABLED</action>
	   </button>
	  </hbox>
	 </vbox>
	 </window>"
	
		 
   rm -f /tmp/.frisbee/id
   gtkdialog --program=PROFEDIT --center
}
export -f profile_editor

function diag {
		export TEXTDOMAIN=frisbee
		export OUTPUT_CHARSET=UTF-8
		. gettext.sh
		get_status
		export DIAGNOSTICS="
		<window title=\"Frisbee - $(gettext 'Wireless Diagnostic Tools')\"icon-name=\"network-idle\" window-position=\"1\">
		   <vbox> 

	    <frame `eval_gettext \"Status of \\\$INTERFACE\"`>
	     <edit editable=\"false\">
	      <variable>STATUS</variable>
	      <input file>/tmp/.frisbee/status</input>
	      <height>200</height>
	      <width>600</width>
	     </edit>
		 <hbox space-expand=\"true\">
		  <checkbox tooltip-text=\"$(gettext 'Log wpa_supplicant activities; restart networks to activate - creates large /tmp/wpa_supplicant.log file')\">
		   <variable>ENDEBUG</variable>
		   <input>grep -q '^wpa_log_mode=1' /etc/frisbee/frisbee.conf && echo true || echo false</input>
		   <label>$(gettext 'Enable Debug Logging')</label>
		   <action>if true grep -q '^wpa_log_mode=1' /etc/frisbee/frisbee.conf || sed -i -e '/^wpa_log_mode=/ s/=.*/=1/' /etc/frisbee/frisbee.conf</action>
		   <action>if false grep -q '^wpa_log_mode=0' /etc/frisbee/frisbee.conf || sed -i -e '/^wpa_log_mode=/ s/=.*/=0/' /etc/frisbee/frisbee.conf</action>
		  </checkbox>
          <button>
           <label>$(gettext 'Refresh')</label>
            <input file stock=\"gtk-refresh\"></input>
            <action>get_status</action>
             <action>refresh:STATUS</action>    
          </button>
		 </hbox>
	    </frame>
	    <hbox> 
	    <frame $(gettext 'WPA_Supplicant Tools')>
	    <vbox> 
 	   <hbox space-expand=\"true\">
	      <button tooltip-text=\"$(gettext 'View the wpa_supplicant log file')\">
	     <input file stock=\"gtk-find\"></input>
	     <label>$(gettext 'View Log')</label>
	     <action>Xdialog --title \"Frisbee - $(gettext 'Wpa_Supplicant Log')\" --no-cancel --ok-label \"Close\"  --textbox /tmp/wpa_supplicant.log 30 100& </action>
	    </button>
 	   </hbox>
 	   <hbox space-expand=\"true\">
	    <button tooltip-text=\"$(gettext 'Edit the wpa_supplicant configuration file')\"> 
	     <input file stock=\"gtk-edit\"></input>
	     <label>$(gettext 'Edit Configuration')</label>
	     <action>wpaconf_edit</action>
	    </button>
 	   </hbox>
 	   <hbox space-expand=\"true\">
	     <button tooltip-text=\"$(gettext 'Control Wi-Fi Protected Access by command line interface commands to wpa_supplicant')\">
	     <input file stock=\"gtk-execute\"></input>
	     <label>$(gettext 'Control WPA Functions')</label>
	     <action>rxvt -e wpa_cli -i $INTERFACE&</action>
	    </button>
 	   </hbox>
	    </vbox> 
	    </frame>
	    <frame $(gettext 'Other Actions')>
	    <vbox> 
 	   <hbox space-expand=\"true\">
	    <button tooltip-text=\"$(gettext 'Set time to wait after connection loss until closing its interface')\">
	     <input file stock=\"gtk-preferences\"></input>
	     <label>$(gettext 'Set Drop Timeout')</label>
	     <action>set_drop_timeout</action>
	    </button>
 	   </hbox>
 	   <hbox space-expand=\"true\">
	    <button tooltip-text=\"$(gettext 'Set ap_scan parameter')\">
	     <input file stock=\"gtk-preferences\"></input>
	     <label>$(gettext 'Set AP scan mode')</label>
	     <action>set_ap_scan</action>
	    </button>
 	   </hbox>
 	   <hbox space-expand=\"true\">
	     <button tooltip-text=\"$(gettext 'Generate diagnostic data')\">
	     <input file stock=\"gtk-execute\"></input>
	     <label>$(gettext 'Generate Diagnostic Data')</label>
	     <action>pdiag --wpa&</action>
	    </button>
 	   </hbox>
	    </vbox> 
	   </frame>
	   </hbox>
	    
	  </vbox>
	  </window>
	"
	gtkdialog --program=DIAGNOSTICS --center
}
export -f diag

export MAIN_DIALOG="
<window title=\"Frisbee - $(gettext 'Configure Networks')\"icon-name=\"network-idle\" window-position=\"1\">
 <vbox>
  <notebook labels=\"$(gettext 'Wireless Networks|Network Interfaces')\">
   <vbox $WIRELESSEN>
     <hbox>
      <hbox space-expand=\"true\">
      <checkbox tooltip-text=\"$(gettext 'Enable wireless networking')\">
       <variable>ENWIRE</variable>
       <input>grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf && echo true || echo false</input>
       <label>$(gettext 'Enable Wireless')</label>
       <action>if true grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf || sed -i -e '/^wireless_enabled=/ s/=.*/=1/' /etc/frisbee/frisbee.conf</action>
       <action>if false grep -q '^wireless_enabled=0' /etc/frisbee/frisbee.conf || sed -i -e '/^wireless_enabled=/ s/=.*/=0/' /etc/frisbee/frisbee.conf</action>
       <action>if true if_connected || ( connect && wpa_cli -i \$INTERFACE scan )</action>
       <action>if false if_connected && disconnect</action>
       <action>if true enable:TABLE</action>
       <action>if false disable:TABLE</action>
       <action>if true enable:CONNECTBUTTON</action>
       <action>if false disable:CONNECTBUTTON</action>
       <action>if true enable:REFRESHBUTTON</action>
       <action>if false disable:REFRESHBUTTON</action>
       <action>if true enable:CURSTAT</action>
       <action>if false disable:CURSTAT</action>
       <action>if true enable:CURIP</action>
       <action>if false disable:CURIP</action>
       <action>refresh:CURSTAT</action>
       <action>refresh:CURIP</action>
      </checkbox>
      <checkbox tooltip-text=\"$(gettext 'Connect last-used, available wireless network automatically when Puppy starts; must be checked to retain any saved network profiles (because the configuration file is only temporary, for current session)')\">
       <variable>ENAUTO</variable>
       <input>grep -q '^wireless_autostart=1' /etc/frisbee/frisbee.conf && echo true || echo false</input>
       <label>$(gettext 'Connect WiFi Automatically')</label>
       <action>if true sed -i -e '/^wireless_autostart=[^1]/ s/=.*/=1/' /etc/frisbee/frisbee.conf</action>
       <action>if false sed -i -e '/^wireless_autostart=[^0]/ s/=.*/=0/' /etc/frisbee/frisbee.conf</action>
       <action>reset_wpa&</action>
       <action>reset_dhcp&</action>
      </checkbox>
      </hbox>
      <button $MULTIFS tooltip-text=\"$(gettext 'Select an interface, if you have multiple wireless cards')\"> <input file stock=\"gtk-preferences\"></input>
       <label>$(gettext 'Change Interface')</label>
       <action>change_if</action>
       <action type=\"exit\">RESTART</action>
      </button>
      <button tooltip-text=\"$(gettext 'Diagnose wireless problems')\"> <input file stock=\"gtk-execute\"></input>
       <label>$(gettext 'Diagnostics')</label>
       <action>get_status</action>
       <action>diag&</action>
      </button>
     </hbox>
    <frame `eval_gettext \"Scan results for \\\$INTERFACE\"`>
     <table $ENABLEWIRELESS>
      <width>700</width>
      <height>180</height>
      <variable>TABLE</variable>
      <label>$(gettext 'BSSID                  |Freq  |Strength|Encryption              |SSID')</label>
      <input>get_scan_results</input>	
     </table>
      <hbox space-expand=\"true\">
     <text $ENABLEWIRELESS tooltip-text=\"$(gettext 'Connection status')\">
      <variable>CURSTAT</variable>
      <input>current_status</input>
     </text>
     <text $ENABLEWIRELESS tooltip-text=\"$(gettext 'Connection status')\">
      <variable>CURIP</variable>
      <input>current_ip</input>
     </text>
       <button $ENABLEWIRELESS tooltip-text=\"$(gettext 'Connect to selected network')\">
       <input file stock=\"gtk-connect\"></input>
       <label>$(gettext 'Connect')</label>
       <variable>CONNECTBUTTON</variable>
       <action>add_profile \"\$TABLE\"</action>
       <action>clear:ID</action>
       <action>refresh:ID</action>
       <action>refresh:CURSTAT</action>
       <action>refresh:CURIP</action>
      </button>
      <button $ENABLEWIRELESS>
       <label>$(gettext 'Refresh')</label>
        <input file stock=\"gtk-refresh\"></input>
       <variable>REFRESHBUTTON</variable>
        <action>clear:TABLE</action>
        <action>refresh:TABLE</action>
        <action>refresh:CURSTAT</action>
        <action>refresh:CURIP</action>
      </button>
     </hbox>
    </frame>
     <hbox space-expand=\"true\">
      <button tooltip-text=\"$(gettext 'Edit or delete saved network profiles')\"> <input file stock=\"gtk-preferences\"></input>
       <label>$(gettext 'Manage Saved Networks')</label>
       <action>profile_editor&</action>
     </button>
     <button tooltip-text=\"$(gettext 'Restart the wpa_supplicant daemon')\">
     <input file stock=\"gtk-redo\"></input> 
     <label>$(gettext 'Restart Networks')</label>
     <action>gtkdialog-splash -placement center -timeout 3 -bg orange -text  \"$(gettext 'Resetting network processes')\"&</action>
     <action>grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf && reset_wpa&</action>
     <action>reset_dhcp&</action>
    </button>
     </hbox>
    <hbox><timer interval=\"15\" visible=\"false\">
        <action>clear:TABLE</action>
        <action>refresh:TABLE</action>
        <action>refresh:CURSTAT</action>
        <action>refresh:CURIP</action>
    </timer></hbox>
   </vbox>
   
 <vbox>
   <frame>
    <table>
     <label>$(gettext 'Interface   | Type                                        |Action')</label>
     <variable>IF</variable>
     <input>get_interfaces</input>
     <action>refresh:IGNORE</action>
     <action>refresh:AUTODHCP</action>
    </table>
    <hbox>
     <vbox>
      <hbox>
       <checkbox tooltip-text=\"$(gettext 'Tell dhcpcd to ignore this interface')\">
        <label>$(gettext 'Ignore')</label>
        <variable>IGNORE</variable>
        <input>get_dhcp_ignored \"\$IF\"</input>
        <action>if true clear:AUTODHCP</action>
        <action>if true disable:AUTODHCP</action>
        <action>if true clear:IP</action>
        <action>if true disable:IPLAB</action>
        <action>if true clear:SUBNET</action>
        <action>if true disable:SUBNETLAB</action>
        <action>if true clear:GATEWAY</action>
        <action>if true disable:GATEWAYLAB</action>
        <action>if true clear:DNS1</action>
        <action>if true disable:DNS1LAB</action>
         <action>if true clear:DNS2</action>
        <action>if true disable:DNS2LAB</action>
        <action>if false refresh:IP</action>
        <action>if false enable:IP</action>
        <action>if false enable:IPLAB</action>
        <action>if false refresh:SUBNET</action>
        <action>if false enable:SUBNET</action>
        <action>if false enable:SUBNETLAB</action>
        <action>if false refresh:GATEWAY</action>
        <action>if false enable:GATEWAY</action>
        <action>if false enable:GATEWAYLAB</action>
        <action>if false refresh:DNS1</action>
        <action>if false enable:DNS1</action>
         <action>if false enable:DNS1LAB</action>
        <action>if false refresh:DNS2</action>
        <action>if false enable:DNS2</action>
        <action>if false enable:DNS2LAB</action>
        <action>if false enable:AUTODHCP</action>
       </checkbox>
       </hbox>
      <hbox>
       <text>
       <visible>disabled</visible>
       <variable>IPLAB</variable><label>$(gettext 'Static IP Address')</label></text>
       <entry tooltip-text=\"$(gettext 'Enter static IP address')\">
        <visible>disabled</visible>
        <variable>IP</variable>
        <input>get_static_ip \"\$IF\"</input>
       </entry>
      </hbox>
      <hbox>
       <text>
        <visible>disabled</visible>
        <variable>SUBNETLAB</variable>
        <label>$(gettext 'Subnet Mask')</label>
       </text>
       <entry tooltip-text=\"$(gettext 'Enter subnet mask')\">
        <visible>disabled</visible>
        <variable>SUBNET</variable>
        <input>get_mask \"\$IF\"</input>
       </entry>
      </hbox>
      <hbox>
       <text>
        <visible>disabled</visible>
        <variable>GATEWAYLAB</variable><label>$(gettext 'Gateway')</label></text>
        <entry tooltip-text=\"$(gettext 'Enter gateway IP address')\">
         <visible>disabled</visible>
         <variable>GATEWAY</variable> 
         <input>get_gateway \"\$IF\"</input>
        </entry>
       </hbox>
      </vbox>
      <vbox>
             <checkbox tooltip-text=\"$(gettext 'Request IP address and routing information from a DHCP server')\">
        <label>$(gettext 'Auto DHCP')</label>
        <variable>AUTODHCP</variable>
        <input>get_dhcp_auto \"\$IF\"</input>
        <action>if true clear:IP</action>
        <action>if true disable:IP</action>
        <action>if true disable:IPLAB</action>
        <action>if true clear:SUBNET</action>
        <action>if true disable:SUBNET</action>
        <action>if true disable:SUBNETLAB</action>
        <action>if true clear:GATEWAY</action>
        <action>if true disable:GATEWAY</action>
        <action>if true disable:GATEWAYLAB</action>
        <action>if true clear:DNS1</action>
        <action>if true disable:DNS1</action>
        <action>if true disable:DNS1LAB</action>
        <action>if true clear:DNS2</action>
        <action>if true disable:DNS2</action>
        <action>if true disable:DNS2LAB</action>
        <action>if true clear:IGNORE</action>
        <action>if false refresh:IP</action>
        <action>if false enable:IP</action>
        <action>if false enable:IPLAB</action>
        <action>if false refresh:SUBNET</action>
        <action>if false enable:SUBNET</action>
        <action>if false enable:SUBNETLAB</action>
        <action>if false refresh:GATEWAY</action>
        <action>if false enable:GATEWAY</action>
        <action>if false enable:GATEWAYLAB</action>
        <action>if false refresh:DNS1</action>
        <action>if false enable:DNS1</action>
        <action>if false enable:DNS1LAB</action>
        <action>if false refresh:DNS2</action>
        <action>if false enable:DNS2</action>
        <action>if false enable:DNS2LAB</action>
       </checkbox>
      <hbox>
       <text>
        <visible>disabled</visible>
        <variable>DNS1LAB</variable><label>$(gettext 'DNS Server 1')</label>
       </text>
       <entry tooltip-text=\"$(gettext 'Enter the IP address for the DNS server')\">
        <visible>disabled</visible>
        <variable>DNS1</variable>
        <input>get_dns1 \"\$IF\"</input>
       </entry>
      </hbox>
      <hbox>
         <text>
          <visible>disabled</visible>
          <variable>DNS2LAB</variable><label>$(gettext 'DNS Server 2')</label>
         </text>
	 <entry tooltip-text=\"$(gettext 'Enter IP address for backup DNS server; this field is optional')\">
          <visible>disabled</visible>
          <variable>DNS2</variable>
          <input>get_dns2 \"\$IF\"</input>
         </entry>
        </hbox>
      <hbox space-expand=\"true\">
      <button tooltip-text=\"$(gettext 'Save interface settings; dhcpcd will restart')\">
      <input file stock=\"gtk-save\"></input>
      <label>$(gettext 'Save')</label>
      <action>update_dhcp \"\$IF|\$IGNORE|\$AUTODHCP|\$IP|\$SUBNET|\$GATEWAY|\$DNS1|\$DNS2\"</action>
      <action>clear:IF</action>
      <action>refresh:IF</action>
     </button>
     </hbox>
       </vbox>
      </hbox>
    </frame>
    <hbox space-expand=\"true\">
     <button tooltip-text=\"$(gettext 'View network information')\"> <input file stock=\"gtk-execute\"></input>
       <label>$(gettext 'Network Status')</label>
       <action>ipinfo&</action>
     </button>
    <button tooltip-text=\"$(gettext 'View the dhcpcd log file')\">
     <input file stock=\"gtk-find\"></input>
     <label>$(gettext 'View DHCP Log')</label>
     <action> grep dhcpcd /var/log/messages > /tmp/dhcpcd.log</action>
     <action>Xdialog --title \"Frisbee - $(gettext 'DHCP Log')\" --no-cancel --ok-label \"Close\"  --textbox /tmp/dhcpcd.log 30 100& </action>
    </button>
    </hbox>
    <hbox space-expand=\"true\">
    <button tooltip-text=\"$(gettext 'Edit the dhcpcd configuration file')\"> 
	     <input file stock=\"gtk-edit\"></input>
	     <label>$(gettext 'Edit dhcpcd.conf')</label>
	     <action>dhcpconf_edit</action>
	 </button>
    <button tooltip-text=\"$(gettext 'Restart the dhcpcd daemon')\">
     <input file stock=\"gtk-redo\"></input> 
     <label>$(gettext 'Restart DHCP')</label>
     <action>gtkdialog-splash -placement center -timeout 3 -bg orange -text  \"$(gettext 'Resetting DHCP processes')\"&</action>
     <action>reset_dhcp&</action>
    </button>
    </hbox>
   </vbox>
  </notebook>
 <hbox>
  <hbox space-expand=\"true\">
   <checkbox tooltip-text=\"$(gettext 'Show notices of changes to network interface states')\">
    <variable>ENNOTIFY</variable>
    <input>grep -q '^announce_state_changes=1' /etc/frisbee/frisbee.conf && echo true || echo false</input>
    <label>$(gettext 'Announce State Changes')</label>
    <action>if true grep -q '^announce_state_changes=1' /etc/frisbee/frisbee.conf || sed -i -e '/^announce_state_changes=/ s/=.*/=1/' /etc/frisbee/frisbee.conf</action>
    <action>if true [ -f /etc/dhcpcd_state_notify ] || touch /etc/dhcpcd_state_notify</action>
    <action>if false grep -q '^announce_state_changes=0' /etc/frisbee/frisbee.conf || sed -i -e '/^announce_state_changes=/ s/=.*/=0/' /etc/frisbee/frisbee.conf</action>
    <action>if false [ -f /etc/dhcpcd_state_notify ] && rm -f /etc/dhcpcd_state_notify</action>
   </checkbox>
  </hbox>
  <button tooltip-text=\"$(gettext 'Exit')\">
  <input file stock=\"gtk-quit\"></input>
  <label>$(gettext 'Exit')</label>
  <action type=\"exit\">Exit-NOW</action>
 </button>
 </hbox>
 </vbox>
<action signal=\"show\">grep -q '^wireless_enabled=1' /etc/frisbee/frisbee.conf && gtkdialog-splash -placement center -bg orange -timeout 10 -text \"$(gettext 'Scanning for available networks. Please wait awhile for them to display.')\" &</action>
</window>
"

EXIT=`gtkdialog --program=MAIN_DIALOG --center | grep ^EXIT | cut -f 2 -d = | grep -v 'Exit on' | tail -n 1 | tr -d \"`
rm -f /tmp/.frisbee/*

if [[ "$EXIT" == "RESTART" ]] ; then 
	/etc/init.d/frisbee stop
	/etc/init.d/frisbee start
	exec frisbee
fi
