#! /bin/sh

# Suck in the configuration variables
if [ -f /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
fi
if [ -f /etc/rc.conf ]; then
	. /etc/rc.conf
fi

interface=$1

# tx rate to use
eval wi_txrate="\$wi_txrate_${interface}"
if [ -n "$wi_txrate" -a "x$wi_txrate" != "xNO" ]; then
  echo wicontrol -i $interface -t "$wi_txrate"
  wicontrol -i $interface -t "$wi_txrate"
  fi

# name of the service set the station wants to join
eval wi_networkname=\$wi_networkname_${interface}
if [ -n "$wi_networkname" -a "x$wi_networkname" != "xNO" ]; then
  echo wicontrol -i $interface -n "$wi_networkname"
  wicontrol -i $interface -n "$wi_networkname"
  fi

# the name of the station
eval wi_stationname=\$wi_stationname_${interface}
if [ -n "$wi_stationname" -a "x$wi_stationname" != "xNO" ]; then
  echo wicontrol -i $interface -s "$wi_stationname"
  wicontrol -i $interface -s "$wi_stationname"
  fi

# allow creation of a service set (yes/no)
eval wi_ssid=\$wi_ssid_${interface}
if [ -n "$wi_ssid" -a "x$wi_ssid" != "xNO" ]; then
  echo wicontrol -i $interface -c 1
  wicontrol -i $interface -c 1
  fi

# name of the service set to create
eval wi_ssidname=\$wi_ssidname_${interface}
if [ -n "$wi_ssidname" -a "x$wi_ssidname" != "xNO" ]; then
  echo wicontrol -i $interface -q "$wi_ssidname"
  wicontrol -i $interface -q "$wi_ssidname"
  fi

# port type
eval wi_porttype=\$wi_porttype_${interface}
if [ -n "$wi_porttype" -a "x$wi_porttype" != "xNO" ]; then
  echo wicontrol -i $interface -p "$wi_porttype"
  wicontrol -i $interface -p "$wi_porttype"
  fi

# key 1-4 to set up wep
for num in 1 2 3 4 ; do
  eval wi_wepkeyval=\$wi_wepkey${num}_${interface}
  if [ -n "$wi_wepkeyval" -a "x$wi_wepkeyval" != "xNO" ]; then
    if [ "x$wi_wepkeyval" = "xEMPTY" ]; then
      echo wicontrol -i $interface -k "" -v $num
      wicontrol -i $interface -k "" -v $num
    else
      echo wicontrol -i $interface -k "$wi_wepkeyval" -v $num
      wicontrol -i $interface -k "$wi_wepkeyval" -v $num
      fi
    fi
  done

# which wep key to use
eval wi_wepkey=\$wi_wepkey_${interface}
if [ -n "$wi_wepkey" -a "x$wi_wepkey" != "xNO" ]; then
  echo wicontrol -i $interface -T "$wi_wepkey"
  wicontrol -i $interface -T "$wi_wepkey"
  fi

# wep encryption
eval wi_wep=\$wi_wep_${interface}
if [ -n "$wi_wep" -a "x$wi_wep" != "xNO" ]; then
  echo wicontrol -i $interface -e 1
  wicontrol -i $interface -e 1
  fi

# what frequency to use
eval wi_frequency=\$wi_frequency_${interface}
if [ -n "$wi_frequency" -a "x$wi_frequency" != "xNO" ]; then
  echo wicontrol -i $interface -f "$wi_frequency"
  wicontrol -i $interface -f "$wi_frequency"
  fi

# use power management
eval wi_pm=\$wi_pm_${interface}
if [ -n "$wi_pm" -a "x$wi_pm" != "xNO" ]; then
  echo wicontrol -i $interface -P 1
  wicontrol -i $interface -P 1
  fi

# what timeout to use with power management
eval wi_sleep=\$wi_sleep_${interface}
if [ -n "$wi_sleep" -a "x$wi_sleep" != "xNO" ]; then
  echo wicontrol -i $interface -S "$wi_sleep"
  wicontrol -i $interface -S "$wi_sleep"
  fi

#
# ipfirewall
#
/sbin/ipfw /etc/ipfw.rules
