#!/bin/ash

PATH=/bin:/sbin:/usr/bin:.
PS1='AP:$PWD # '
export PATH PS1
echo "Mounting proc-fs"
mount none /proc -tproc -n
mount none /tmp -ttmpfs -osize=1048576 -n
echo "Reading config file..."
if [ ! -f "config/config" ]; then
  echo "mounting disk..."
  mount -t msdos /dev/fd0 /config
fi
. config/config
#Do PCMCIA-Voodoo
/sbin/insmod /lib/modules/pcmcia_core.o $CORE_OPTS
/sbin/insmod /lib/modules/$PCIC.o $PCIC_OPTS
/sbin/insmod /lib/modules/ds.o
/sbin/insmod /lib/modules/hostap_crypt.o
/sbin/insmod /lib/modules/hostap.o
/sbin/insmod /lib/modules/hostap_crypt_wep.o

/sbin/cardmgr -v -o -m /lib/modules -s /tmp/stab -p /tmp/pid
sleep 1

if [ "$HAVE_IP" != "yes" -o "$MYIP" = "" ]; then
  MYIP="0.0.0.0"
fi

ifconfig eth0 "$MYIP" netmask "$NETMASK" up
ifconfig wlan0 0.0.0.0 up
iwconfig wlan0 channel "$CHANNEL"
iwconfig wlan0 essid "$ESSID"

if [ "$TXPOWER" != "-1" ]; then
  iwconfig wlan0 txpower fixed
  iwconfig wlan0 txpower "$TXPOWER"
fi

prism2_param wlan0 antsel_tx "$ANTSEL_TX"
prism2_param wlan0 antsel_rx "$ANTSEL_RX"
iwconfig wlan0 enc "$WEP_KEY"

if [ "$MODE" = "routing" ]; then
  ifconfig eth0 "$MYIP" netmask "$NETMASK" up
  ifconfig wlan0 "$WLAN_IP" netmask "$WLAN_NM" up
  echo 1 > /proc/sys/net/ipv4/ip_forward
  if [ "$FW_RULES" != "" ]; then
    OLDIFS="$IFS"
    IFS="`echo`"
    for i in $FW_RULES; do
      eval $i
    done
    IFS="$OLDIFS"
  fi
  iptables -L
else
  # Bridging
  echo "Adding bridge..."
  brctl addbr br0
  echo "Switching spanning tree $STP"
  brctl stp br0 "$STP"
  #This is ugly
  echo "Setting IP of br0 to 0.0.0.0 (needed for bridging on nfsroot)"
  ifconfig br0 0.0.0.0
  echo "Adding eth0 to bridge"
  brctl addif br0 eth0
  ifconfig wlan0 0.0.0.0 up
  if [ "$HAVE_IP" = "yes" ]; then
    ifconfig br0 "$MYIP" netmask "$NETMASK" up
    echo "Zeroing eth0 IP"
    ifconfig eth0 0.0.0.0 up
  else
    ifconfig eth0 0.0.0.0 up
    ifconfig br0 0.0.0.0 up
  fi
  echo "Adding wlan0 to bridge"
  brctl addif br0 wlan0
fi

if [ "$GATEWAY" != "" ]; then
  route add default gw "$GATEWAY"
fi

echo

iwconfig wlan0

exec /bin/ash
