#!/bin/sh
#######################################################################
#
# Program     : wireless
# Version     : 1.00
# Date        : 22 April 2004
# Author      : 
#
# Description : Start a wireless interface.
#
# Usage       : wireless device {up|down}
#
# Notes       : 
#
########################################################################

. /etc/sysconfig/rc 
. $rc_functions

network=/etc/sysconfig/network

boot_mesg "Scanning for access points that $1 interface can use..."

ACCESSPOINTS=`iwlist "$1" scanning | grep ESSID | cut -f2 -d: | sed -e 's/"//g'`
evaluate_retval

for ap in $ACCESSPOINTS; do
    if [ -e ${network}/SSID/$ap ]; then
        . ${network}/SSID/$ap

        ESSID=$ap

        case $2 in
            up )
                echo "Setting ESSID to $ap on $1 interface..."
                iwconfig $1 essid $ESSID
                evaluate_retval

                if [ -n "$RATE" ]; then
                    echo "Setting Rate to $RATE on $1 interface..."
                    iwconfig $1 rate $RATE
                    evaluate_retval
                fi

                if [ -n "$CHANNEL" ]; then
                    echo "Setting Channel to $CHANNEL on $1 interface..."
                    iwconfig $1 channel $CHANNEL
                    evaluate_retval
                fi

                if [ -n "$MODE" ]; then
                    echo "Setting Mode to $MODE on $1 interface..."
                    iwconfig $1 mode $MODE
                    evaluate_retval
                fi

                if [ -n "$WEP_KEY1" ]; then
                    echo "Setting WEP Key 1 to $WEP_KEY1 on $1 interface, security mode to $SECURITY_MODE..."
                    iwconfig $1 $SECURITY_MODE $WEP_KEY1 [1]
                    evaluate_retval
                fi

                if [ -n "$WEP_KEY2" ]; then
                    echo "Setting WEP Key 2 to $WEP_KEY2 on $1 interface, security mode to $SECURITY_MODE..."
                    iwconfig $1 $SECURITY_MODE $WEP_KEY2 [1]
                    evaluate_retval
                fi

                if [ -n "$WEP_KEY3" ]; then
                    echo "Setting WEP Key 3 to $WEP_KEY3 on $1 interface, security mode to $SECURITY_MODE..."
                    iwconfig $1 $SECURITY_MODE $WEP_KEY3 [3]
                    evaluate_retval
                fi

                if [ -n "$WEP_KEY4" ]; then
                    echo "Setting WEP Key 4 to $WEP_KEY4 on $1 interface, security mode to $SECURITY_MODE..."
                    iwconfig $1 $SECURITY_MODE $WEP_KEY4 [4]
                    evaluate_retval
                fi
            ;;

        down )
            ;;

        * )
            echo "Usage: $0 [interface] {up|down}"
            exit 3;
            ;;
        esac
    fi
done
