#!/bin/bash

# Set inet values 
function set_inet()
{
  # I don't want a troube here
  # So ... let's always make a new script
  # Old script will be overwritten !!!!
  new_inet | sed "
  s#^DEVICE=.*#DEVICE='$interface'# 
  s#^IPADDR=.*#IPADDR='$IP'# 
  s#^NETMASK=.*#NETMASK='$MASK'# 
  s#^GATEWAY=.*#GATEWAY='$GATEWAY'# 
  s#^DHCP=.*#DHCP='$DHCP'# 
  s#^PROBE=.*#PROBE='no'#" > $inet_file
  
    sh $inet_file start 
    #| zenity --progress --text="Installing $1" --pulsate --auto-close
}

## Create a new inet script
function new_inet()
{
echo '
#!/bin/sh
# This file is supposed to be created by vnetadd
# and modified by vnetset.
# You can modify it by hand, but be careful ;-)
#
# GNU GPL  (c) Eko M. Budi, 2004
#          (c) VLocity Linux, 2004
#

###########################################################
## The settings
DEVICE=eth0
DHCP="yes"
IPADDR=""
NETMASK=""
GATEWAY=""
PROBE="no"

###########################################################
## The script
'
echo "$secure"
echo "$wpa" 
echo '
## You may make customized script here
## If not, source the standard network 
. /etc/rc.d/functions-network "$@"

'
}

## Create a new /etc/wpa_supplicant.conf
function new_wpa()
{
echo '
# See /usr/doc/wpa_supplicant-0.5.7/wpa_supplicant.conf.sample
# for many more options that you can use in this file.

# This line enables the use of wpa_cli which is used by rc.wireless
# if possible (to check for successful association)
ctrl_interface=/var/run/wpa_supplicant
# By default, only root (group 0) may use wpa_cli
ctrl_interface_group=0
eapol_version=1
ap_scan=1
fast_reauth=1
'
}

## find wireless interface
interface=$(/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z])
if [ x$interface = x ];then
  $(##try to find hardware before exiting
  b=/etc/wifi-radar/wifi-radar.conf.backup
  i=/etc/wifi-radar/wifi-radar.conf
  inter=`cat $i|grep interface|cut -d " " -f3`
  interface=`/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z]`
  connected=no
  ### we have many wireless modules some can cause us problems you can add more here
  ### if you remove "none" from the list the script will not exit properly
  list="ipw2100 ipw2200 ipw3945 zd1201 ndiswrapper none"
  if [ ! -f $b ]; then
    cp $i $b
  fi
  while [ "$connected" == no ]; do
    if [ ! "$interface" == "" ]; then
      connected=yes
    elif [ "$interface" == "" ]; then
      for a in $list; do
        if [ "$a" == none ]; then
          echo 100
          connected=yes
        fi
        modprobe $a 2>/dev/null
        if [ $a == ipw3945 ]; then
          /sbin/ipw3945d
        fi 
        sleep 1
        interface=$(/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z])
        if [ ! "$interface" == "" ]; then
          if [ $a == ndiswrapper ]; then
		  echo "/sbin/modprobe ndiswrapper" >> /etc/rc.d/rc.modules
	  fi
          connected=yes
          break
        fi
        rmmod $a 2>/dev/null
      done
    fi
  done)|$(zenity --title="Please Wait" --pulsate --progress --text="Probing modules for hardware" --auto-close)
fi
interface=$(/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z])
if [ x$interface = x ];then
  zenity --question --text "Would you like to insert a windows driver into ndiswrapper \
  \n and see if it works with your hardware?" 
  if [ $? = 0 ];then
    INF=$(zenity --file-selection --text "browse to your windows driver .inf file")
    dir=$(dirname "$INF")
    inf=$(basename "$INF")
    cd "$dir"
    ndiswrapper -i $inf
    modprobe ndiswrapper
    sleep 2
    interface=`/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z]`
    if [ ! "$interface" == "" ]; then
      echo "/sbin/modprobe ndiswrapper" >> /etc/rc.d/rc.modules
    fi
  fi
fi  
interface=$(/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z])
if [ x$interface = x ];then
	zenity --warning --text "no wireless cards found"
	exit 1
fi
if [ $(echo $interface|wc|cut -d " " -f7) -gt 1 ];then
  interface=$(zenity --list --title INTERFACE --column interface \
     --text "choose wireless interface" \
     $(/sbin/iwconfig 2>/dev/null| cut -d " " -f1 | grep [a-z]))
else
	zenity --info --text "wireless interface $interface was found \
	\n \n           Lets set it up"
fi

## keep devices from switching names in udev
ifconfig $interface up
rules=/etc/udev/rules.d/network-devices.rules
    rm $rules
    ifconfig|grep HWaddr|while read i;do
      iface=$(echo $i|cut -d " " -f1)
      mac=$(echo $i|cut -d " " -f5)
      echo "KERNEL==\"eth?\", SYSFS{address}==\"$mac\", NAME=\"$iface\"" >>$rules
    done

security=$(zenity --list --title WIFI-SECURITY --radiolist --column choice \
 --column Security --text "Choose type of wireless security" \
 TRUE none FALSE wep FALSE wpa)

case $security in
	none)
	ESSID="$(zenity --entry --text "enter essid to connect")"
	secure="iwconfig $interface essid $ESSID"
	;;
	wep)
	ESSID="$(zenity --entry --text "enter essid to connect")"
	KEY="$(zenity --entry --text "enter wep password")"
	secure="iwconfig $interface essid $ESSID key $KEY"
	;;
	wpa)
	ESSID="$(zenity --entry --text "enter essid to connect")"
	PASS="$(zenity --entry --text "enter wpa passphrase")"
	if $(grep -q $ESSID /etc/wpa_supplicant.conf 2>/dev/null);then
	  rm /etc/wpa_supplicant.conf
        else	       
	  mv /etc/wpa_supplicant.conf /etc/wpa_supplicant.bak 2>/dev/null
        fi
	new_wpa >/etc/wpa_supplicant.conf
	wpa_passphrase "$ESSID" "$PASS" >>/etc/wpa_supplicant.conf
	secure="iwconfig $interface essid $ESSID"
	wpa="wpa_supplicant -B -Dwext -i$interface -c/etc/wpa_supplicant.conf"
	;;
esac

OPT=$(zenity --list --title WIFI-OPTIONS --checklist --column choice \
 --column Option --separator=" " --text "Choose dhcp or static and whether to start interface at boot. \nDo Not choose dhcp and static!" \
 TRUE "start at boot" TRUE dhcp FALSE STATIC FALSE "set static DNS Nameserver")   
##setup rc.inet3 instead of serching for which rc.inet to use
  inet_file="/etc/rc.d/rc.inet3"

for i in $OPT;do
 case $i in
        boot)
	BOOT=yes
	;;
	dhcp)	
	DHCP=yes
	;;
	STATIC)
	IP=$(zenity --entry --text "enter static ip address" \
	 --entry-text="192.168.0.2")
	MASK=$(zenity --entry --text "enter netmask" \
	--entry-text="255.255.255.0")
	GATEWAY=$(zenity --entry --text "enter gateway" \
	--entry-text="192.168.0.1")
	DHCP=no
	;;
	DNS)
	DNS1=$(zenity --entry --text "enter primary DNS server" \
	--entry-text="208.67.222.222")
	DNS2=$(zenity --entry --text "enter secondary DNS server" \
	--entry-text="205.166.226.38")
	echo "nameserver $DNS1" >/etc/resolv.conf
	echo "nameserver $DNS2" >>/etc/resolv.conf
	;;
 esac
done
set_inet
if [ "$BOOT" = yes ];then
	chmod a+x $inet_file
	ifplug=/etc/ifplugd/ifplugd.conf
	cp $ifplug ${ifplug}.bak 
	. ${ifplug}.backup
	INTERFACES="$INTERFACES $interface"
	echo "INTERFACES=\"$INTERFACES\"" >$ifplug
	echo "ARGS=\"-fwI -u1 -d5 -pql\"" >>$ifplug
fi
##check for an ipaddress and let the user know
ipaddr=$(ifconfig|grep inet|grep -v 127.0.0.1|cut -d " " -f12|cut -d ":" -f2)
if [ "$ipaddr" != "" ];then
  zenity --info --title SUCCESS --text "wireless interface $interface is configured with address $ipaddr"
else 
  zenity --info --title FAILED --text "could not obtain an address for wireless interface $interface"
fi
exit 0 

