#!/bin/sh
# @vwireless: Wireless Network
# @level: root
# @description: work with wireless interface  
# 
# (c) JohnVan, 2004
# (c) VLocity Linux, 2004
#
# Released under GNU GPL

# Path to files
wdir=/etc/wireless
. $wdir/interface.conf

vdir=$(dirname $0)
. $vdir/vasm-functions

check_root

function W_status()
{
	
	t=`iwgetid $INTERFACE`
	if a=`echo $t | grep ESSID` ; then 
	    iface=`echo $t | cut -d " " -f1`
	    a=`echo $t | cut -d '"' -f2`
	fi
	if [ $1 = "short" ]; then	
	    WSTATUS="$iface connected to $a"
	else
	    WSTATUS=`sh /etc/rc.d/rc.wireless status`
	fi
	if b=`echo $t | grep '""'` ; then
	    WSTATUS="$iface not connected"
	fi
	if [ -z "$t" ]; then
	    WSTATUS="no wireless interface"
	fi
	$DCMD --backtitle "$TITLE" --title "STATUS" --msgbox "$WSTATUS" 0 0 2> $freply
}

function edit_interface()
{
 while [ 1 ]; do
	DIMENSION="17 60 8"
	TITLE="Interface Change"
	TEXT="\n
	Select Field to change"

	$DCMD $LEFT_ALIGN --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
  	"INTERFACE" " = \"$INTERFACE\" ---- what is the name of the wireless interface (wlan0, eth0, ath0) " \
  	"DRIVER"  " = \"$DRIVER\" ---- which WPA_supplicant driver - if card supports" \
	"DONE" "Finished Changing" \
  	2> $freply
  
  	STATUS=$?
  	[ $STATUS != 0 ] && return $STATUS
  	choice=$(cat $freply)
  
  case $choice in
  	INTERFACE) inputwizard "Wireless interface (wlan0, eth0, ath0, etc)" "INTERFACE" "$INTERFACE" 
		STATUS=$?
		[ $STATUS != 0 ] && return $STATUS
    		INTERFACE=$(cat $freply)	
	;;
	DRIVER) inputwizard "WPA_supplicant driver: hostap, prism54, atmel, ndiswrapper" "DRIVER" "$DRIVER" 
		STATUS=$?
		[ $STATUS != 0 ] && return $STATUS
    		DRIVER=$(cat $freply)
	;;
	DONE) return 0
	;;
  esac
  continue 
done
}

save_interface(){
DIMENSION="10 60 2"
TITLE="OKAY TO SAVE?"
TEXT="\n
Is it okay to save information"

$DCMD $LEFT_ALIGN --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
  "Yes" "I'm Satisfied " \
  "No"  "Do not save" \
  2> $freply
  
  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS
  
  SAVE=$(cat $freply)
  IFACE_file=$wdir/interface.conf
  if [ $SAVE = "No" ]; then return 1 ; fi
  cp IFACE_file $wdir/interface.bkp
  cat $IFACE_file | sed "
  s/INTERFACE=.*/INTERFACE=\"$INTERFACE\"/
  s/DRIVER=.*/DRIVER=\"$DRIVER\"/" > $IFACE_file 
 return 0
 } 

build_defined_essids()
{
  Available="`ls $wdir | grep -F .essid.conf | grep conf$ | cut -f1 -d"." `"

  for item in $Available; do
    echo "$item \"defined\" \\"
    let count++
  done
}

get_defined_ESSID()
{
  DIMENSION="17 60 8"
  TITLE="SELECT CONNECTION"
  TEXT="\n
This is the list of the active wireless connections.\n
Select the one you want to $1 then press OK."

  Available="`ls $wdir | grep -F .essid.conf | grep .conf$`"
  if [ -z "$Available" ]; then
     errorbox "No wireless connection. Add one first, mate !"
     return 1
  fi

  echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
  build_defined_essids >> $fmenu
  echo '2> $freply' >> $fmenu

#  cat $fmenu
  source $fmenu
  
  status=$?
  [ $status != 0 ] && return $status

  ESSID=$(cat $freply)
  }
  
build_available_essids()
{
  count=0
  Available=`iwlist $INTERFACE scan | grep ESSID | cut -f2 -d'"'`
  for item in $Available; do
  	flag="Not Defined - Visible"
  	if [ -f $wdir/$item.essid.conf ]; then flag="Defined and Visible";fi
  	echo "$item \"$flag\" \\"
  let count++
  done
  if [ $count == 0 ]; then # maybe there is a defined one that does not broadcast
     build_defined_essids 
  fi
}

get_available_ESSID()
{

  Available=`iwlist $INTERFACE scan | grep ESSID | cut -f2 -d'"'`
  if [ -z "$Available" ]; then
     Available="`ls $wdir | grep -F .essid.conf | grep .conf$`"
    if [ -z "$Available" ]; then
	 errorbox "No available wireless connections."
	return 1
    fi
  fi

  DIMENSION="20 70 8"
  TITLE="SELECT CONNECTION"
  TEXT="\n
This is the list of the available wireless connections.\n
Defined ESSIDs are those where we have parameters defined.\n
Visible ESSIDS are those we can see by scanning.\n
Select the one you want to connect to and then press OK."

  echo '$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \' > $fmenu
  build_available_essids >> $fmenu
  echo '2> $freply' >> $fmenu

#  cat $fmenu
  source $fmenu
  
  status=$?
  [ $status != 0 ] && return $status

  ESSID=$(cat $freply)
  
  }

  
 # ASK ESSID Name
prompt_New_ESSID()
{
while [ 1 ]; do
  TITLE="ADD WIRELESS CONNECTION"
  TEXT="ESSID name for the new connection:"

  inputbox "$TEXT" "$TITLE" "$ESSID"
  STATUS=$?
  [ $STATUS != 0 ] && return $STATUS

  ESSID=$(cat $freply)

  if [ -z "$ESSID" ]; then
    continue   
  elif [ -f "$wdir"/"$ESSID".essid.conf ]; then
    retrybox "Connection '$ESSID' already exists"
    continue
  else
    return 0
  fi
done
}

#########################################################################
menuA()
{
while [ 0 ]; do
DIMENSION="20 76 10"
TITLE="WIRELESS INTERFACE CONFIGURATION"
TEXT="\n
This configurator helps you setup connections for wireless cards.
Connections are identified by their ESSID. System will attempt to 
connect at startup (using /etc/rc.d/rc.wireless).
Configurations are stored in /etc/wireless.
It provides support for Plain text, WEP and WPA/RSN (Private Shared Key).
Please select the menu:"

$DCMD --backtitle "$BACKTITLE" --title "$TITLE" --menu "$TEXT" $DIMENSION \
 "DONE" "bye bye wireless configurator" \
 "INTERFACE" "Change Interface Settings" \
 "ADD" "Add a Connection" \
 "DEL" "Delete a Connection" \
 "CHANGE" "Change a Connection" \
 "CONNECT" "Connect An Available ESSID" \
 "STOP" "Stop the Wireless Connection" \
 "STATUS" "Status of the Wireless Connection" \
 "ENABLE" "Enable Automatic Startup" \
 "DISABLE" "Disable Automatic Startup" \
 2> $freply

  status=$?
  [ $status != 0 ] && return $status

  case `cat $freply` in
      INTERFACE)
       edit_interface && save_interface
	;;
      ADD)
        prompt_New_ESSID 
        if [ $? == 0 ]; then $vdir/vwirelessset $ESSID ; fi
        ;;
      CHANGE)
	get_defined_ESSID Change
        if [ $? == 0 ]; then $vdir/vwirelessset $ESSID ; fi
	;;
      DEL)
      	get_defined_ESSID Delete # and move to backup file for potential recovery
        if [ $? == 0 ]; then 
		mv $wdir/$ESSID.essid.conf $wdir/$ESSID.essid.bkp
		if [ -f $wdir/$ESSID.wpa.conf ]; then
			mv $wdir/$ESSID.wpa.conf $wdir/$ESSID.wpa.bkp
		fi
	fi
        ;;	
      CONNECT)
	get_available_ESSID
	if [ $? == 0 ]; then 
		sh /etc/rc.d/rc.wireless stop
		sh /etc/rc.d/rc.wireless $ESSID
		W_status "short"
	fi
	;;
      ENABLE)
      	chmod o+x /etc/rc.d/rc.wireless
	;;
      DISABLE)
      	chmod -x /etc/rc.d/rc.wireless
	;;
      STOP)
        sh /etc/rc.d/rc.wireless stop
	W_status "short"
	;;
      STATUS)
        W_status "long"
	;;
      DONE)
        return 0
	;;
      *) 
      ;;
    esac
done
}

####################################################################################
# MAIN PROGRAM

menuA
