#!/bin/sh
# /etc/rc.d/rc.wireless
#
# Wireless network card setup.
#
# This script sets up PCI, USB, and 32-bit Cardbus wireless devices
# NOT 16-bit PCMCIA cards!  Those are configured in /etc/pcmcia/.
# Single parameter to this script is the name of a network interface.
# Normally this script is called from rc.inet1 rather than run
# directly.
#
# This script is a modified '/etc/pcmcia/wireless' script
# 09/apr/2004 by Eric Hameleers
#

if [ -z $1 ] ; then
  echo "Usage: rc.wireless <interface>"
  return 1 2> /dev/null || exit 1
fi

INTERFACE=$1

LOGGER=${LOGGER:-cat}

# Find the path where wireless tools are installed
for IWPATH in /usr/{bin,sbin} /usr/local/{bin,sbin} /sbin ; do
    if [ -x $IWPATH/iwconfig ] ; then break ; fi
done

# Set all desired settings via iwconfig
IWCOMMAND="$IWPATH/iwconfig ${INTERFACE}"
IFCOMMAND="/sbin/ifconfig ${INTERFACE}"

is_wireless_device ()
{
    [ -x $IWPATH/iwconfig ] || return 1
    LC_ALL=C $IWPATH/iwconfig $1 2>&1 | \
        grep -q "no wireless extensions" || return 0
    return 1
}

# Is the device wireless?  If not, exit this script.
is_wireless_device ${INTERFACE} || return 0 2> /dev/null || exit 0

# Bring interface up - to avoid 'not ready' errors when calling iwconfig and
# for determining the HWADDR
$IFCOMMAND up
sleep 2

# Get the MAC address for the interface
HWADDR=`/sbin/ifconfig ${INTERFACE} | sed -ne 's/.*\(..:..:..:..:..:..\).*/\1/p'`

# Read the configuration information for the card with address $HWADDR
# from /etc/rc.d/rc.wireless.conf:
. /etc/rc.d/rc.wireless.conf

[ -n "$VERBOSE" -a -n "$INFO" ] && echo "$0:  $1 is '$INFO'"

# Mode needs to be first : some settings apply only in a specific mode!
if [ -n "$MODE" ] ; then
	echo "$0:  $IWCOMMAND mode $MODE" | $LOGGER
	$IWCOMMAND mode $MODE
fi
# This is a bit hackish, but should do the job right...
if [ ! -n "$NICKNAME" ] ; then
    NICKNAME=`/bin/hostname`
fi
if [ -n "$ESSID" -o -n "$MODE" ] ; then
	echo "$0:  $IWCOMMAND nick $NICKNAME" | $LOGGER
	$IWCOMMAND nick $NICKNAME
fi
# Regular stuff...
if [ -n "$NWID" ] ; then
	echo "$0:  $IWCOMMAND nwid $NWID" | $LOGGER
	$IWCOMMAND nwid $NWID
fi
if [ -n "$FREQ" ] ; then
	echo "$0:  $IWCOMMAND freq $FREQ" | $LOGGER
	$IWCOMMAND freq $FREQ
elif [ -n "$CHANNEL" ] ; then
	echo "$0:  $IWCOMMAND channel $CHANNEL" | $LOGGER
	$IWCOMMAND channel $CHANNEL
fi
if [ -n "$KEY" ] ; then
	echo "$0:  $IWCOMMAND key ************" | $LOGGER
	$IWCOMMAND key $KEY
fi
if [ -n "$SENS" ] ; then
	echo "$0:  $IWCOMMAND sens $SENS" | $LOGGER
	$IWCOMMAND sens $SENS
fi
if [ -n "$RATE" ] ; then
	echo "$0:  $IWCOMMAND rate $RATE" | $LOGGER
	$IWCOMMAND rate $RATE
fi
if [ -n "$RTS" ] ; then
	echo "$0:  $IWCOMMAND rts $RTS" | $LOGGER
	$IWCOMMAND rts $RTS
fi
if [ -n "$FRAG" ] ; then
	echo "$0:  $IWCOMMAND frag $FRAG" | $LOGGER
	$IWCOMMAND frag $FRAG
fi
# More specific parameters
if [ -n "$IWCONFIG" ] ; then
	echo "$0:  $IWCOMMAND $IWCONFIG" | $LOGGER
	$IWCOMMAND $IWCONFIG
fi
if [ -n "$IWSPY" ] ; then
	echo "$0:  $IWCOMMAND $IWSPY" | $LOGGER
	$IWCOMMAND $IWSPY
fi
if [ -n "$IWPRIV" ] ; then
	echo "$0:  $IWCOMMAND $IWPRIV" | $LOGGER
	$IWCOMMAND $IWPRIV
fi

# ESSID need to be last : most devices re-perform the scanning/discovery
# when this is set, and things like encryption keys had better be
# defined if we want to discover the right set of APs/nodes.
# NOTE: when automatic association does not work, but you manage to get
# an IP address by manually setting the ESSID and then calling dhcpcd,
# then the cause might be the incorrect definition of your ESSID="bla"
# parameter in rc.wireless.conf.
# Debug your wireless problems by running 'iwevent' while the card
# is being configured.
if [ -n "$ESSID" ] ; then
	echo "$0:  $IWCOMMAND essid $ESSID" | $LOGGER
	$IWCOMMAND essid $ESSID
fi

