#!/bin/bash

touch .wifi
freply=.wifi
if [ $DISPLAY ]; then
  DIALOG=Xdialog
else
  DIALOG=dialog
fi

interface=`cat /etc/interface`
interface=`/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z]`


$DIALOG --backtitle "$BACKTITLE" --title "Network Name" \
--inputbox "\n
Enter the ESSID for the network you want to connect " \
10 70 2> $freply
result=$?
[ $result != 0 ] && return $result 
  ssid="`cat $freply`"
#echo enter essid
#read ssid
#interface=`/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z]`
/sbin/iwconfig $interface essid $ssid

$DIALOG --backtitle "$BACKTITLE" --title "Network Key" \
--inputbox "\n
Enter the encryption key for the network you want to connect\n 
or enter with no text for none" \
10 70 2> $freply
result=$?
[ $result != 0 ] && return $result 
  enckey="`cat $freply`"
#echo enter key or leave blank for none
#read enckey
if [ $enckey ]; then
  /sbin/iwconfig $interface key $enckey
fi
sleep .5
/sbin/dhcpcd -k $interface
/sbin/dhcpcd $interface
result=$?
if [ $result != 0 ]; then
  /sbin/dhcpcd $interface 
fi

## uncomment out this next 3 lines if your router gets in tha way of DNS
#grep -v ^search /etc/resolv.conf >/tmp/resolv
#grep -v "nameserver 192" /tmp/resolv >/etc/resolv.conf
#rm /tmp/resolv 2>/dev/null

/sbin/ifconfig
sleep 2
rm $freply 2>/dev/null
