#! /bin/bash

#v0.8.1 patch from HairyWill
PWIRELESS_VERSION="0.8.3" #2008-05-12
#urban: use /etc/WAG/profile-conf as common database
#urban: external script for use with gtkdialog (saves you from exiting gtkdialog)
[ ! -e $HOME/.config/Pwireless/Pwireless-save.sh ] && echo "#!/bin/sh 

#args: \$1 <profile aka TITLE>; \$2 <new> | <save> | <delete>
. /usr/sbin/wag-profiles.sh 
refreshProfilesWindowInfo
#rxvt -e echo \"args: \$1 \$2\"
case \"\$2\" in
\"new\" )
	setupNewProfile			
	;; 
\"save\" )
	CURRENT_PROFILE=\"\$1\"
	loadProfileData \"\$1\"			
	;;
\"delete\" )
	CURRENT_PROFILE=\"\$1\"
	NEW_PROFILE_DATA=\"\"
	saveProfiles
	exit 0			
	;;
* )
	exit 1 # wrong usage			
	;;
esac

eval \`cat /root/.config/Pwireless/\$1\`
PROFILE_TITLE=\"\$1\"
PROFILE_ESSID=\"\$ESSID\"
PROFILE_MODE=\"managed\"
if [[ \$ENCRYPTION != \"\" && \$ENCRYPTION != \"off\" ]] ; then 
	PROFILE_ENCRYPTION=\"WEP\" # WEP<->WPA|WPA2
	PROFILE_KEY=\"\$ENCRYPTION\"
fi
if [[ \$IPCONFIG == \"\" ]] ; then PROFILE_NWID=\"auto\" ; else PROFILE_NWID=\"\$IPCONFIG\" ; fi #correct 'false' entry 
if [[ \$CHANNEL == \"\" ]] ; then PROFILE_CHANNEL=\"auto\" ; else PROFILE_CHANNEL=\"\$CHANNEL\" ; fi
if [[ \$APMAC == \"\" ]] ; then PROFILE_AP_MAC=\"auto\" ; else PROFILE_AP_MAC=\"\$APMAC\" ; fi
PROFILE_SECURE_O=\"true\" # true=open, false=restricted
assembleProfileData
saveProfiles

exit 0" > /$HOME/.config/Pwireless/Pwireless-save.sh
chmod 777 $HOME/.config/Pwireless/Pwireless-save.sh

rm -f /tmp/Pwireless-profile-connect

###Check for gtkdialog

GTKDIALOG=""

if [ "`which gtkdialog`" != "" ]; then
GTKDIALOG=gtkdialog
fi
if [ "`which gtkdialog3`" != "" ]; then
GTKDIALOG=gtkdialog3
fi
if [ "$GTKDIALOG" = "" ]; then
echo "you need to install gtkdialog"
fi

###Make sure the user is root

if [ "`which whoami`" != "" ]; then
	if [ "`whoami`" != root ]; then
		export MAIN_DIALOG='
		 <vbox>
		  <text wrap="true" width-chars="20">
			<label>Запуск этой программы возможен только пользователем root</label>
		  </text>
		  <hbox>
		   <button ok></button>
		  </hbox>
		 </vbox>
		'
		$GTKDIALOG --program=MAIN_DIALOG --center
		exit 0
	fi
fi

###Get the wireless device, i.e. ath0, wlan0, ra0, etc...
ifconfig `cat /proc/net/wireless | grep -Eo '.+:' | grep -Eo '[^: ][[:alnum:]]+'` up
iwconfig | grep ESSID: > /tmp/Pwireless-device #urban. wont work if IF is down or driver problems. 
DEVICE=`cat /tmp/Pwireless-device | cut -f 1 -d ' '`
ifconfig "$DEVICE" up
IP=`ifconfig $DEVICE | grep 'inet addr:' | sed 's/^[ ^t]*//' | sed 's/inet addr://g' | cut -f 1 -d ' '`
if [ "$IP" = "" ]; then
IP=127.0.0.1
fi
echo $IP > /tmp/Pwireless-IP

###Find a suitable dhcp client, i.e. dhclient, pump, or dhcpcd

DHCP="" #$DHCP is the client daemon.

if [ "`which dhclient`" != "" ]; then
DHCP=dhclient
fi
if [ "`which pump`" != "" ]; then
DHCP="pump -i"
fi
if [ "`which dhcpcd`" != "" ]; then
DHCP=dhcpcd
fi
if [ "$DHCP" = "" ]; then
	export MAIN_DIALOG='
	 <vbox>
	  <text wrap="true" width-chars="20">
		<label>Требуется установить dhclient, dhcpcd, или pump</label>
	  </text>
	  <hbox>
	   <button ok></button>
	  </hbox>
	 </vbox>
	'
	$GTKDIALOG --program=MAIN_DIALOG --center
	exit 0
fi

echo "$DHCP $DEVICE" > /tmp/Pwireless-dhcp #urban: add $DEVICE 

###create a temp file to either view all wifi or only open unencrypted wifi

if ! [ -f /tmp/Pwireless-open ]; then
echo false > /tmp/Pwireless-open
fi
rm -f /tmp/Pwireless-cells 2>/dev/null

###Scan for wifi & build the main gui

iwlist $DEVICE scanning > /tmp/Pwireless
NUMCELLS=`cat /tmp/Pwireless | grep "   Cell " | wc -l | sed 's/ //g'`
a=1
b=`expr "$NUMCELLS" + 1`

#urban: load saved profile titles for combobox. 

#if [ -d $HOME/.config/Pwireless ]; then 
#cd $HOME/.config/Pwireless
#if [ "`ls`" != "" ]; then 
#SSIDS="" #($SSID is a custom name here) 
#for ONESSID in `ls | sed 's/-GETIP//g' | sed 's/-IWCONFIG//g' | sort -u`
#do
# SSIDS="$SSIDS   <item>$ONESSID</item>"
#done
#fi
#fi
PROFILE_TITLES=`grep -E 'TITLE[0-9]+=' /etc/WAG/profile-conf | cut -d= -f2 | tr -d '"' | tr " " "_" `
SSIDS=""
for TITLE in $PROFILE_TITLES
	do SSIDS="${SSIDS}<item>$TITLE</item>" #SSIDS == PROFILE_LIST for combobox 
done

#urban: disabled. Progressbar shown instead. (see 499, was: <action type=\"launch\">DIALOG</action>)
export DIALOG='
  <vbox>
    <text>
      <label>Профиль сохранён.</label>
    </text>
    <button>
      <label>Выход</label>
      <action type="closewindow">DIALOG</action>
    </button>
  </vbox>
'

export MAIN_DIALOG="
<window title=\"Pwireless-$PWIRELESS_VERSION - Сканер беспроводных сетей\"icon-name=\"gtk-connect\">
 <vbox>
<notebook labels=\"Сети|Профили\">
<vbox>
"
if [ "`cat /tmp/Pwireless-open`" = false ]; then
MAIN_DIALOG="$MAIN_DIALOG
 <frame Результаты сканирования всех wifi сетей - `cat /tmp/Pwireless-device | cut -f 1 -d ' '` IP is `cat /tmp/Pwireless-IP`>"
else
MAIN_DIALOG="$MAIN_DIALOG
 <frame Результаты сканирования только открытых wifi сетей с публичными SSID - `cat /tmp/Pwireless-device | cut -f 1 -d ' '` IP is `cat /tmp/Pwireless-IP`>"
 fi
MAIN_DIALOG="$MAIN_DIALOG
  <table>
    <width>565</width><height>300</height>
    <variable>TABLE</variable>
    <label>Cell |ESSID                 |Режим       |Канал |Качество |Сигнал/Шум     |Шифров.| MAC адрес AP </label>"
    
###adding a zero before all single digit cells that are found by iwlist so grep can find the specified cells in the temp file

while [ "$a" != "$b" ]; do
if [ "`echo $a | wc -c | sed 's/ //g'`" = 2 ]; then
NUM=0"$a"
else
NUM=$a
fi

###grep the temp file for the ESSID

ESSID=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep ESSID: | head -n 1 | sed 's/^[ ^t]*//' | sed 's/ESSID://g'`

#v1.8.1 patch from HairyWill...
#ESSID2=`echo "$ESSID" | sed 's/\"//g'`
ESSID2=`echo "$ESSID" | sed 's/\"//g;s/</\\\</g;s/>/\\\>/g'` 

if [ "$ESSID2" = "" ]; then
ESSID2=HIDDEN-ESSID
fi
if [ "$ESSID2" = " " ]; then
ESSID2=HIDDEN-ESSID
fi

###grep the temp file for Mode, Channel, Quality, & Encryption type

if [ "`cat /tmp/Pwireless | grep Quality=`" != "" ]; then
QTEMP="Quality="
else
QTEMP="Quality:"
fi

MODE=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep Mode: | head -n 1 | sed 's/^[ ^t]*//' | sed 's/Mode://g' | sed -r "s/\<[a-z]+/\L&/g" | sed 's/master/managed/g'`
CHANNEL=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep Channel | head -n 1 | tail -c 4 | grep -o [0-9][0-9]*`
QUALITY=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep $QTEMP | head -n 1 | sed 's/^[ ^t]*//' | tr -s ' ' | cut -f 1 -d ' ' | sed "s/$QTEMP//g"`
ENCRYPTION=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep "Encryption key:" | head -n 1 | sed 's/Encryption key://g' | sed 's/^[ ^t]*//'`
SIGNAL=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep $QTEMP | head -n 1 | tr -s ' ' | cut -f 4 -d ' ' | sed 's/level//g' | sed 's/=//g' | sed 's/://g'`
NOISE1=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep $QTEMP | head -n 1 | tr -s ' ' | cut -f 7 -d ' ' | sed 's/level//g' | sed 's/=//g' | sed 's/://g'`
NOISE2=`cat /tmp/Pwireless | grep "   Cell $NUM" -A 9 | grep $QTEMP | head -n 1 | tr -s ' ' | cut -f 8 -d ' '`
if [ "$SIGNAL" = "" ]; then
SIGNAL=?
fi
if [ "$NOISE1" = "" ]; then
NOISE1=?
fi
if [ "$QUALITY" = "" ]; then
QUALITY=?
fi
NOISE="$NOISE1 $NOISE2"
APMAC=`cat /tmp/Pwireless | grep "   Cell $NUM" | sed 's/^[ ^t]*//' | tr -s ' ' | cut -f 5 -d ' '`

if [ "$CHANNEL" = "" ]; then
CHANNEL=auto
fi
if [ "`echo $APMAC | grep : | wc -c | sed 's/ //g'`" != 18 ]; then
APMAC=auto
fi


echo Cell="$NUM" >> /tmp/Pwireless-cells
echo ESSID="$ESSID" >> /tmp/Pwireless-cells
echo Channel="$CHANNEL" >> /tmp/Pwireless-cells
echo Encryption="$ENCRYPTION" >> /tmp/Pwireless-cells
echo SIGNAL="$SIGNAL" >> /tmp/Pwireless-cells
echo NOISE="$NOISE" >> /tmp/Pwireless-cells
echo APMAC="$APMAC" >> /tmp/Pwireless-cells
echo MODE="$MODE" >> /tmp/Pwireless-cells

###differentiate between WEP, WPA, & unencrypted wifi signals
if [ "$ENCRYPTION" = off ]; then
ENCRYPTION2=OPEN
fi
if [ "$ENCRYPTION" = on ]; then
if [ "`cat /tmp/Pwireless | grep "   Cell $NUM" -A 10 | grep 'IE: IEEE 802.11i/WPA'`" != "" ]; then
ENCRYPTION2=WPA
else
ENCRYPTION2=WEP
fi
fi

echo Encryption2="$ENCRYPTION2" >> /tmp/Pwireless-cells

###build gui with either All wifi, or just OPEN unencrypted wifi that broadcasts the SSID
if [ "`cat /tmp/Pwireless-open`" = true ]; then
	if [ "$ENCRYPTION" = off ]; then
		if [ "$ESSID2" != HIDDEN-ESSID ]; then
		MAIN_DIALOG="$MAIN_DIALOG
			<item>$NUM | $ESSID2 | $MODE | $CHANNEL | $QUALITY | $SIGNAL/$NOISE | $ENCRYPTION2 | $APMAC</item>"
		fi
	fi
else
MAIN_DIALOG="$MAIN_DIALOG
	<item>$NUM | $ESSID2 | $MODE | $CHANNEL | $QUALITY | $SIGNAL/$NOISE | $ENCRYPTION2 | $APMAC</item>"
fi
a=`expr "$a" + 1`
done
MAIN_DIALOG="$MAIN_DIALOG

    <action>echo $TABLE</action>
  </table>
  <hbox>
    <button>
       <input file stock=\"gtk-dialog-authentication\"></input>
      <label>Обновить все сети</label>
      <action type=\"exit\">VIEW-ALL</action>
    </button>
    <button>
       <input file stock=\"gtk-apply\"></input>
      <label>Обновить только открытые сети</label>
      <action type=\"exit\">VIEW-OPEN</action>
    </button>
    <button>
       <input file stock=\"gtk-connect\"></input>
      <label>Соединиться</label>
      <action type=\"exit\">Connect-NOW</action>
    </button>
		<button>
			<input file stock=\"gtk-add\"></input>
        <label>Добавить профиль</label>
        <action type=\"exit\">ADD-PROFILE</action>
      </button>
		<button>
			<input file stock=\"gtk-quit\"></input>
        <label>ВЫХОД</label>
        <action type=\"exit\">Exit-NOW</action>
      </button>
  </hbox>
  </frame>
 </vbox>
    <vbox>
	<hbox>
     <text>
      <label>Выберите профиль и нажмите кнопку OK или кнопку Удалить для удаления профиля</label>
    </text>
    <combobox>
     <variable>SSID</variable>
      $SSIDS
    </combobox>
    <button>
       <input file stock=\"gtk-apply\"></input>
      <label>OK</label>
      <action>echo \$SSID | tee /tmp/Pwireless-ssid</action>
      <action>refresh:CHANNEL</action>
      <action>refresh:ESSID</action>
      <action>refresh:ENCKEY</action>
      <action>refresh:APMAC2</action>
      <action>refresh:MODE2</action>
      <action>refresh:IWCONFIG</action>
      <action>refresh:GETIP</action>
      <action>refresh:IPCONFIG</action>
    </button>
    <button>
       <input file stock=\"gtk-delete\"></input>
      <label>Удалить</label>
      <action>rm $HOME/.config/Pwireless/\$SSID</action>
      <action>rm -f $HOME/.config/Pwireless/\$SSID-IWCONFIG</action>
      <action>rm -f $HOME/.config/Pwireless/\$SSID-GETIP</action>
      
      <action>exec $HOME/.config/Pwireless/Pwireless-save.sh \$SSID delete</action>
      
        <action type=\"exit\">REFRESH</action>
    </button>
    </hbox>
    <frame Нажмите 'ОБНОВИТЬ параметры' для того, чтобы применить сделанные вами изменения>
    <hbox>
    <text><label>Транслировать ESSID                               :</label></text>
	<entry>
	<variable>ESSID</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep ESSID | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
    <hbox>
    <text><label>Транслировать номер канала ESSID:</label></text>
	<entry>
	<variable>CHANNEL</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep CHANNEL | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
    <hbox>
    <text><label>Ключ шифрования (ASCII)                           :</label></text>
	<entry>
	<variable>ENCKEY</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep KEY | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
    <hbox>
    <text><label>MAC адрес точки доступа (AP)                                    :</label></text>
	<entry>
	<variable>APMAC2</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep AP_MAC | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
    <hbox>
    <text><label>Режим                                                      :</label></text>
	<entry>
	<variable>MODE2</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep MODE | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
  <hbox>
    <text><label>IP                                                           :</label></text>
	<entry>
	<variable>IPCONFIG</variable>
	<input>echo</input>
    <input>cat /etc/WAG/profile-conf | grep -A 11 \$SSID | grep NWID | cut -d= -f2 | tr -d '\"'</input>
  </entry>
  </hbox>
	<hbox>
  	<button>
	<input file stock=\"gtk-refresh\"></input>
        <label>ОБНОВИТЬ параметры</label>
	<action>rm -f $HOME/.config/Pwireless/\$SSID-IWCONFIG</action>
	<action>rm -f $HOME/.config/Pwireless/\$SSID-GETIP</action>
      <action>refresh:IWCONFIG</action>
      <action>refresh:GETIP</action>
      <action>echo \$IWCONFIG | tee $HOME/.config/Pwireless/\$SSID-IWCONFIG</action>
      <action>echo \$GETIP | tee $HOME/.config/Pwireless/\$SSID-GETIP</action>
      <action>echo ESSID=\$ESSID | tee $HOME/.config/Pwireless/\$SSID</action>   
	<action>echo CHANNEL=\$CHANNEL | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo ENCRYPTION=\$ENCKEY | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo APMAC=\$APMAC2 | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo MODE=\$MODE2 | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo IPCONFIG=\$IPCONFIG | tee -a $HOME/.config/Pwireless/\$SSID</action>
      </button>
      </hbox>
      </frame>
	<frame При необходимости отредактируйте следующие строки и нажмите 'Соединиться'
	или 'Сохранить профиль' для сохраниня ваших изменений.>
    <hbox>
	<entry>
	<variable>IWCONFIG</variable>
		
	<input>if [ -f $HOME/.config/Pwireless/\$SSID ]; then echo iwconfig `cat /tmp/Pwireless-device | cut -f 1 -d ' '` mode \$MODE2 essid \$ESSID key \$ENCKEY ap \$APMAC2 channel \$CHANNEL ; fi</input>
    <input>if [[ -z \$ENCKEY ]] ; then echo iwconfig `cat /tmp/Pwireless-device | cut -f 1 -d ' '` mode \$MODE2 essid \$ESSID key off ap \$APMAC2 channel \$CHANNEL ; else echo iwconfig `cat /tmp/Pwireless-device | cut -f 1 -d ' '` mode \$MODE2 essid \$ESSID key \$ENCKEY ap \$APMAC2 channel \$CHANNEL ; fi</input>
	<input>if [ -f $HOME/.config/Pwireless/\$SSID-IWCONFIG ]; then cat $HOME/.config/Pwireless/\$SSID-IWCONFIG; fi</input>
  
  </entry>
  </hbox>
    <hbox>
	<entry>
	<variable>GETIP</variable>
    <input>if [[ -z \$IPCONFIG || \$IPCONFIG == auto ]] ; then echo `cat /tmp/Pwireless-dhcp` ; else echo \"ifconfig ip \$IPCONFIG up\" ; fi </input>
	</entry>
  </hbox>
  </frame>
    <hbox>
    <button>
      <input file stock=\"gtk-save\"></input>
      <label>Сохранить профиль</label>
      <action>echo \$IWCONFIG | tee $HOME/.config/Pwireless/\$SSID-IWCONFIG</action>
      <action>echo \$GETIP | tee $HOME/.config/Pwireless/\$SSID-GETIP</action>
      
    <action>echo ESSID=\$ESSID | tee $HOME/.config/Pwireless/\$SSID</action>   
	<action>echo CHANNEL=\$CHANNEL | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo ENCRYPTION=\$ENCKEY | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo APMAC=\$APMAC2 | tee -a $HOME/.config/Pwireless/\$SSID</action> 
	<action>echo MODE=\$MODE2 | tee -a $HOME/.config/Pwireless/\$SSID</action>
	<action>echo IPCONFIG=\$IPCONFIG | tee -a $HOME/.config/Pwireless/\$SSID</action>
	
	<action>exec $HOME/.config/Pwireless/Pwireless-save.sh \$SSID save</action>
	 
    </button>
    <button>
       <input file stock=\"gtk-connect\"></input>
      <label>Соединиться</label>
      <action>echo \$IWCONFIG | tee $HOME/.config/Pwireless/\$SSID-IWCONFIG</action>
      <action>echo \$GETIP | tee $HOME/.config/Pwireless/\$SSID-GETIP</action>
      <action>echo \$SSID | tee /tmp/Pwireless-profile-connect</action>
      <action type=\"exit\">Connect-NOW</action>
    </button>
	<button>
	<input file stock=\"gtk-quit\"></input>
        <label>ВЫХОД</label>
        <action type=\"exit\">Exit-NOW</action>
      </button>
      </hbox>
    </vbox>
 </notebook>
 </vbox>
 </window>
"

RESULTS=`$GTKDIALOG --program=MAIN_DIALOG --center`

###view OPEN wifi

if [ "`echo "$RESULTS" | grep VIEW-OPEN`" != "" ]; then
echo true > /tmp/Pwireless-open
Pwireless &
exit 0
fi

###view ALL wifi

if [ "`echo "$RESULTS" | grep VIEW-ALL`" != "" ]; then
echo false > /tmp/Pwireless-open
Pwireless &
exit 0
fi

###exit the program if the Quit button is pushed

if [ "`echo "$RESULTS" | grep Exit-NOW`" != "" ]; then
exit 0
fi

###exit the program if the X button is pushed

if [ "`echo "$RESULTS" | grep abort`" != "" ]; then
exit 0
fi

#if none of the above it must be the 'connect' buttons  or the 'add new profile' button...
###variables for connecting or adding a profile

CELL=`echo "$RESULTS" | grep "TABLE=\"" | sed 's/TABLE=\"//g' | sed 's/ //g' | sed 's/\"//g'`
ESSID=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 1 | grep ESSID= | sed 's/ESSID=//g' | sed 's/\"//g'`
CHANNEL=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 2 | grep Channel= | sed 's/Channel=//g'`
ENCRYPTION=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 3 | grep Encryption= | sed 's/Encryption=//g'`
ENCRYPTION2=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 4 | grep Encryption2= | sed 's/Encryption2=//g'`
APMAC=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 6 | grep APMAC= | sed 's/APMAC=//g'`
MODE=`cat /tmp/Pwireless-cells | grep Cell=$CELL -A 7 | grep MODE= | sed 's/MODE=//g'`

###add a profile
if [ "`echo "$RESULTS" | grep ADD-PROFILE`" != "" ]; then
	mkdir -p $HOME/.config/Pwireless
	NEWESSID=`echo "$ESSID" | sed 's/ /_/g'`		
	export MAIN_DIALOG="						

	 <vbox>
	 <frame Введите имя создаваемого профиля>
	  <entry>
		<input>echo $NEWESSID</input>
		<variable>ENTRY</variable>
	  </entry>
	  	  <entry>
			<default>off</default> 
			<visible>disabled</visible>
			<variable>KKEY</variable>
	  </entry>
	  <hbox>
	       <button>
       <input file stock=\"gtk-dialog-authentication\"></input>
      <label>Есть ключ</label>
      <action>enable:KKEY</action>
      <action>clear:KKEY</action>
    </button>
	   <button ok>
	   <action>echo NEWESSID=\"\$ENTRY\"</action>
	   <action>EXIT:ok</action>
	   </button>
	  </hbox>
	  </frame>
	 </vbox>
	"
	eval `$GTKDIALOG --program=MAIN_DIALOG --center`
	echo "--------> $EXIT $NEWESSID $KKEY" #urban: debug.
	#NEWESSID=`echo "$RESULTS5" | grep ENTRY= | cut -f 2 -d '=' | sed 's/\"//g' | sed 's/ /_/g'`
	echo ESSID=\""$ESSID"\" > $HOME/.config/Pwireless/$NEWESSID 
	echo CHANNEL=$CHANNEL >> $HOME/.config/Pwireless/$NEWESSID
	echo "$ENCRYPTION"
	if [ "$ENCRYPTION" = off ]; then #in file on disk $ENCRYPTION == thekey. 
		ENCRYPTION=off
	else
		echo "you entered $KKEY"
		ENCRYPTION=$KKEY
	fi
	echo APMAC=$APMAC >> $HOME/.config/Pwireless/$NEWESSID
	echo MODE=$MODE >> $HOME/.config/Pwireless/$NEWESSID
	echo ENCRYPTION=$ENCRYPTION >> $HOME/.config/Pwireless/$NEWESSID
	echo IPCONFIG=auto >> $HOME/.config/Pwireless/$NEWESSID
	$HOME/.config/Pwireless/Pwireless-save.sh $NEWESSID new #& #urban: use profile-conf finally
	echo x
	Pwireless &
	exit 0
fi

###connect to the selected wifi network
if [ "`echo "$RESULTS" | grep Connect-NOW`" != "" ]; then #corresponding 'fi' is in line 765.
	PROFILE=""
	if [ -f /tmp/Pwireless-profile-connect ]; then #this file used to check $SSID from combobox 
		PROFILE=`cat /tmp/Pwireless-profile-connect` 
		rm /tmp/Pwireless-profile-connect 
	fi

	###if WPA encryption is part of the selected wifi network you will get a popup saying WPA is not yet supported
	if [ "$PROFILE" = "" ]; then
		if [ "$ENCRYPTION2" = WPA ]; then
			export MAIN_DIALOG='
			 <vbox>
			  <text wrap="true" width-chars="20">
				<label>Эта программа пока не поддерживает шифрование WPA</label>
			  </text>
			  <hbox>
			   <button ok></button>
			  </hbox>
			 </vbox>
			'
			$GTKDIALOG --program=MAIN_DIALOG --center
			Pwireless &
			exit 0
		fi

		###if selected network has a non-broadcasted SSID you will need to enter it in this gui
		if [ "$ESSID" = "" ]; then
			export MAIN_DIALOG='
			 <vbox>
			  <entry>
				<default>Введите SSID ключ</default>
				<variable>ENTRY</variable>
			  </entry>
			  <hbox>
			   <button ok></button>
			  </hbox>
			 </vbox>
			'
			RESULTS4=`$GTKDIALOG --program=MAIN_DIALOG --center`
			ESSID=`echo $RESULTS4 | grep ENTRY= | sed 's/ENTRY=//g' | sed 's/\"//g' | sed 's/ EXIT=OK//g'`
		fi

		###if WEP encryption is part of the selected network you will need to enter the WEP Key into this gui
		if [ "$ENCRYPTION" = on ]; then 
			export MAIN_DIALOG='
			 <vbox>
			  <entry>
				<default>Введите WEP ключ</default>
				<variable>ENTRY</variable>
			  </entry>
			  <hbox>
			   <button ok></button>
			  </hbox>
			 </vbox>
			'
			RESULTS3=`$GTKDIALOG --program=MAIN_DIALOG --center`
			KEY=`echo $RESULTS3 | cut -f 1 -d ' ' | sed 's/ENTRY=//g' | sed 's/\"//g' | sed 's/://g'` #urban: kill ":" from hex keys
			echo "------------->$KEY"
		else 
			KEY=off
		fi

		###all information is available to finally use iwconfig to input the variables
		iwconfig $DEVICE mode $MODE
		iwconfig $DEVICE essid "$ESSID"
		iwconfig $DEVICE key $KEY
		iwconfig $DEVICE ap $APMAC  
		iwconfig $DEVICE channel $CHANNEL 
		#iwconfig $DEVICE nickname default
		#iwconfig $DEVICE bit auto 
		#iwconfig $DEVICE rate auto ###urban: dont overestimate compatibility of buggy NIC drivers with iwconfig...
		#iwconfig $DEVICE txpower auto ###urban: fix for ralink devices to not accept a key due to <set txpower not possible>. 
	else ##urban: case: connect button is pressed && profile exists
		#chmod a+x $HOME/.config/Pwireless/$PROFILE-IWCONFIG 
		#$HOME/.config/Pwireless/$PROFILE-IWCONFIG #If one of the commands is not supported by the driver we are out of the game. This is likely to happen all the time. eg. my orinoco card. 
		#useProfile #no. make good script instead and preserve the idea of user commandline..
		a=1; while [ $a -le 18 ] ; do # quick and dirty. attention if command is longer than 2 fields. eg. key open s:... or <value> empty
			let "a+=2"
			eval `cat $HOME/.config/Pwireless/$PROFILE-IWCONFIG | cut -f 1,2,$a,$(($a+1)) -d ' '` 
 			sleep 1 #sorry rt73 wants this.
 		done #note: the loop evaluates 18 fields, so additional commands will be executed when entered.
		DHCP=`cat $HOME/.config/Pwireless/$PROFILE-GETIP | cut -f 1 -d ' '`
		
	fi

	###this gui is a progress bar to let you know your dhcp client is trying to obtain an IP
	export MAIN_DIALOG="
	<vbox>
	  <frame Progress>
		<text>
		  <label>Запуск $DHCP для получения IP адреса.</label>
		</text>
		<progressbar>
		  <label>Запуск $DHCP</label>
		  <input>for i in \$(seq 0 10 100); do echo \$i; sleep 4; done</input>
		</progressbar>
	  </frame>
	 </vbox>
	"
	$GTKDIALOG --program=MAIN_DIALOG --center &

	X1PID=$!

	###killall pre-existing dhcp clients
	if [ "$DHCP" = "pump -i" ]; then
		killall pump
	fi
	if [ "$DHCP" = dhcpcd ]; then
		killall dhcpcd
		rm -f /etc/dhcpc/dhcpcd-*
	fi
	if [ "$DHCP" = dhclient ]; then
		killall dhclient
	fi

	###command to obtain an IP
	sleep 3
	if [ "$PROFILE" = "" ]; then
		$DHCP $DEVICE
	else
		chmod a+x $HOME/.config/Pwireless/$PROFILE-GETIP
		exec $HOME/.config/Pwireless/$PROFILE-GETIP
	fi

	###gui to display new IP address
	x=x
	while [ "$x" != y ]; do
		IP=`ifconfig $DEVICE | grep 'inet addr:' | sed 's/^[ ^t]*//' | sed 's/inet addr://g' | cut -f 1 -d ' '`
		export MAIN_DIALOG="
		 <vbox>
		  <text wrap=\"true\" width-chars=\"20\">
			<label>IP адрес: $IP</label>
		  </text>
		  <hbox>
		   <button ok></button>
			<button>
			<input file stock=\"gtk-refresh\"></input>
				<label>Повторить</label>
				<action type=\"exit\">REDO</action>
			</button>
			<button>
			<input file stock=\"gtk-go-back\"></input>
				<label>Пересканировать сети</label>
				<action type=\"exit\">RESCAN</action>
			  </button>
		  </hbox>
		 </vbox>
		"
		RESULTS2="`$GTKDIALOG --program=MAIN_DIALOG --center`"

		###rescan for new wireless networks if button selected
		if [ "`echo "$RESULTS2" | grep RESCAN`" != "" ]; then
			Pwireless &
			kill $X1PID 2>/dev/null
			exit 0
		fi

		###if getting an IP is unsuccessful you can click the try again button to run your dhcp client again
		if [ "`echo "$RESULTS2" | grep REDO`" != "" ]; then
			kill $X1PID 2>/dev/null
			export MAIN_DIALOG="
			<vbox>
			  <frame Progress>
				<text>
				  <label>Запуск $DHCP для получения IP адреса.</label>
				</text>
				<progressbar>
				  <label>запуск $DHCP</label>
				  <input>for i in \$(seq 0 10 100); do echo \$i; sleep 4; done</input>
				</progressbar>
			  </frame>
			 </vbox>
			"
			$GTKDIALOG --program=MAIN_DIALOG --center &
			X1PID=$!
			if [ "$PROFILE" = "" ]; then
				iwconfig $DEVICE channel $CHANNEL
			else
				$HOME/.config/Pwireless/$PROFILE-IWCONFIG
			fi
			if [ "$DHCP" = "pump -i" ]; then
				killall pump
			fi
			if [ "$DHCP" = dhcpcd ]; then
				killall dhcpcd
				rm -f /etc/dhcpc/dhcpcd-*
			fi
			if [ "$DHCP" = dhclient ]; then
				killall dhclient
			fi
				sleep 3
			if [ "$PROFILE" = "" ]; then
				$DHCP $DEVICE
			else
				$HOME/.config/Pwireless/$PROFILE-GETIP
			fi
		else
			x=y
		fi
	done
	kill $X1PID 2>/dev/null
fi

###all processes done so gui is being re-started
Pwireless &
exit 0
