#!/bin/bash
# 
# Wireless Interface Script
# [ Prism54g & Orinoco ]
#
# bunker - http://rawlab.altervista.org
# 
IP="10.0.0.137"
GW="10.0.0.2"
KEY="off"
MAC="00:0E:6B:6C:1B:41"
NICK=`head -c 3 /dev/urandom | mimencode`

if [ $UID != 0 ]; then
	 echo "WARNING: only root can set wireless network!"
	 exit -1
fi

if [[ $# < 1 ]]; then
	 echo "Usage: $0 <ssid> [<channel>]"
	 exit -1
fi

echo "Clearing interfaces and firewall rules..."
killall dhcpcd
if [ -x /etc/rc.d/rc.firewall ]; then
   /etc/rc.d/rc.firewall stop
fi

echo "Starting wireless connection..."
ifconfig eth1 down
iwconfig eth1 key $KEY

# prism54
if [ $2 ]; then
    iwconfig eth1 mode Managed essid "$1" channel "$2"
# orinoco (v0.13)
else
    iwconfig eth1 mode Managed
    iwconfig eth1 essid "$1" nickname $NICK
fi

# Use this if you want dhcp (and comment the rest)
# dhcpcd eth1

# prism54 
if [ $2 ]; then
    ifconfig eth1 $IP netmask 255.255.255.0 up hw ether $MAC up
# orinoco (v0.13)
else
    ifconfig eth1 $IP netmask 255.255.255.0 up
fi

route add default gw $GW eth1
echo "Sleeping 3 seconds to assest network..."
sleep 3
iwconfig eth1
if [ -x /etc/rc.d/rc.firewall ]; then
   /etc/rc.d/rc.firewall start
fi
echo "Done!"
exit 0
