#!/bin/sh
# Copyright Martin Bergner, 2004
#
# You may distribute this under the terms of the GPL version 2; the 
# complete license is available at http://www.gnu.org/licenses/gpl.txt
#
# It requires you to make some arrangements before you can use it:
# 1. you need the wireless tools installed
# 2. you'll need sudo, if you want to run this skript as a normal user
# 3. you'll need a dhcp client if you want to use it
#
# Use man sudo to get an overview on how it works and make iwlist, iwconfig,
# your dhcp client and/or ifconfig either suid root or available via sudo, or
# simply run this script as root
#
# You'll need to change the following to fit your pc:

IWLIST="sudo /usr/sbin/iwlist"
IWCONFIG="sudo /usr/sbin/iwconfig" 
IFCONFIG="sudo /sbin/ifconfig"
# If you don't want dhcp, set this to 0
WANT_DHCP=1
DHCP="sudo /sbin/dhcpcd"
# And set your desired IP-Address
# IP_ADDRESS="192.168.0.33"

# This will be determined automatically, if you have two wlan-cards, you must
# specify it here!
# IFACE="eth0"

function scan()
{
$IWLIST $IFACE scan > /tmp/scan.ori
grep "No scan results" /tmp/scan.ori
if [ $? -eq 0 ]
then 
 echo "No accesspoints found, you might need to switch the card off and back on"
 echo "If you have done so, please press Return to repeat the scan"
 read $AOL
fi
$IWLIST $IFACE scan > /tmp/scan.ori
grep "No scan results" /tmp/scan.ori
if [ $? -eq 0 ]
then
 echo "Still no Accesspoint found, there might not be one, exiting"
 exit 1
fi
}
function choose_ap()
{
  echo "Please choose an accesspoint from the list above:"
  read AKTUELL
  read_ap
  
}

function set_ap()
{
  echo "The accesspoint will be configured."
  if [ $KEY -eq 1 ]
  then
        echo "This accesspoint is password-protected, please enter the password:"
        read PASSWORT
        $IWCONFIG $IFACE enc s:$PASSWORT open
  else 
        $IWCONFIG $IFACE enc off
  fi
  
  $IWCONFIG $IFACE essid $NAME
  echo "You need to switch the card off and on again."
  echo "Press Return to proceed."
  read LPO
 if [ $WANT_DHCP -eq 1 ]
 then
  echo "I will try to configure the card via DHCP"
  $DHCP $IFACE
 else
  echo "I will configure the card via ifconfig to $IP_ADDRESS"
  $IFCONFIG $IFACE $IP_ADDRESS
  $IFCONFIG $IFACE up
 fi 
 echo "The card should be configured"  

}
function read_ap()
{
  if [ $ANZAHL -ne 1 ]
  then
        TEMP=`expr $AKTUELL + 1`
        Z_ANFANG=`grep -n Cell$Address /tmp/scan.ori| cut -d: -f1| sed -n "$AKTUELL p"`
        if [ $ANZAHL -eq $AKTUELL ]
        then
                Z_END=`expr $Z_WHOLE - 1`
        else
                TEMP2=`grep -n Cell$Address /tmp/scan.ori| cut -d: -f1| sed -n "$TEMP p"`
                Z_END=`expr $TEMP2 - 1`
        fi

#       echo "Z_START=$Z_START, Z_END=$Z_END"
        sed -n "$Z_START,$Z_END p"< /tmp/scan.ori > /tmp/scan.tmp 
else
        head -n-1 /tmp/scan.ori > /tmp/scan.tmp  
        
fi
# Aus der Datei werden die Werte gelesen
  NR=`grep Cell /tmp/scan.tmp | awk '{print $2}'`
  NAME=`grep ESSID /tmp/scan.tmp | awk -F\" '{print $2}'`
  HW_ADDRESS=` grep Address /tmp/scan.tmp | awk -F: '{print $2,$3,$4,$5,$6,$7}' | sed -e s/\ /:/g | cut -c2-18`
  RATE=`tail -n1 /tmp/scan.tmp| awk -F: '{print $2}'`

  grep "Encryption key:off" /tmp/scan.tmp > /dev/null
  KEY=$?

  if [ $KEY -eq 1 ]
  then 
        PASS="Yes"
  else
        PASS="No"
  fi

  echo "$NR. \"$NAME\", MAC: $HW_ADDRESS Max. Bit.: $RATE Password: $PASS"
  let AKTUELL+=1
}

if test $IFACE -z
then 
  IFACE=`/usr/sbin/iwconfig 2>&1 | grep "IEEE 802"| cut -d " " -f1`
fi

echo "Ndiswrapper should be loaded as a module, I will try to configure your WLAN-card"
echo "I need some inforamtion for this, I guess the interface is $IFACE."
echo "At first, I will scan for any accesspoints (This might take some time)"

scan
COUNT=`grep Cell$Address /tmp/scan.ori | wc -l`
AKTUELL="1"
Z_START=0
Z_END=0
Z_WHOLE=`wc -l /tmp/scan.ori | cut -d " " -f1`
echo "I have found the following" $COUNT "accesspoint(s):"
while [ $AKTUELL -le $COUNT ]
do
read_ap
done

while [ 1 -eq 1 ]
do

  choose_ap
  echo "You have choosen the Accesspoint above? (y/n)"
  read ANTWORT

  if test $ANTWORT == y
  then
        set_ap
        exit 0
  fi
done

set_ap