#!/bin/bash

#########################################
# start_net script                      #
# acx100 project                        #
# acx100.sourceforge.net                #
# edited by arnie <urnotwelcome@gmx.de> #
#########################################

# Please edit here

DEV=wlan0

ESSID="any" # THIS IS CASE SeNsItIvE!! any == associate to any ESSID
# Default rate configured as 11Mbps to not cause connection problems with non-22Mbps hardware...
RATE=11M
CHAN=6 # it's useful to try to stick to channels 1, 6 or 11 only, since these don't overlap with other channels
#TXPOWER=16 # 16 == 16.5dBm, 18 == 18dBm (default)
MODE=Managed
DEBUG=0xb
#KEY="B401CD21B44CCD21DEADBEEF11" # WEP128
ALG=open # open == Open System, restricted == Shared Key

USE_DHCP=0 # set to 1 for auto configuration instead of fixed IP setting
IP=192.168.0.10
NETMASK=255.255.255.0
GATEWAY=192.168.0.254

# Usually no editing should be required below this line...
# -- unless you want this script to fetch stuff with dhcp

#########################################


if test "$UID" != "0"; then echo "You are not root. To insert the module into your kernel, you need to be root. Enter su and try again. Bailing..."; exit 1; fi

SYNC=`which sync`
INSMOD=`which insmod`
IFCONF=`which ifconfig`
IWCONF=`which iwconfig`
IWPRIV=`which iwpriv`
ROUTE=`which route`
SCRIPT_AT=`dirname $0`

if test -z "$SYNC"; then echo "sync not found. Go get a sane Linux system. Bailing..."; exit 1; fi
if test -z "$INSMOD"; then echo "insmod not found. Go get a sane Linux system. Bailing..."; exit 1; fi
if test -z "$IFCONF"; then echo "ifconfig not found. I can insert the module for you, but you won't be able to configure your interface."; CONTINUE=ASK; fi
if test -z "$IWCONF"; then echo "iwconfig not found. Make sure it is installed. The interface might work without, though."; CONTINUE=ASK; fi

if test -n "$CONTINUE"; then echo -n "Problems encountered. Do you want to continue? [n] "; read ANSWER
case $ANSWER in  ( y | Y | Yes | YES | yes | j | J | ja | Ja | JA ) ;;
                 ( * ) exit 1 ;;
esac
fi

MODULE_AT="${SCRIPT_AT}/../src/acx100_pci.o"

if test ! -r "$MODULE_AT"; then echo "Module not found or not readable. Have you built it? This script expects it to be at ../src/acx100_pci.o, relative to the script's location. Bailing..."; exit 1; fi

# FIRMWARE_AT has to be given as an absolute path!!
FIRMWARE_AT="${SCRIPT_AT}/../firmware"

if test ! -r "$FIRMWARE_AT/WLANGEN.BIN" & test ! -r "$FIRMWARE_AT/TIACX111.BIN"; then echo "Firmware not found or not readable. Have you placed it in the firmware directory or run make extract_firmware once? This script expects it to be at ../firmware/WLANGEN.BIN, relative to the script's location. Bailing..."; exit 1; fi

if test "$RATE" != "11M"; then echo "Transfer rate is not 11 Mbps, but $RATE. If something doesn't work, try 11 Mbps."; fi


# for better debugging
# set -x
#echo 8 > /proc/sys/kernel/printk


# just in case ;)
$SYNC
sleep 1

if test -n "`lsmod |grep acx100_pci`"; then ${SCRIPT_AT}/stop_net; fi

$INSMOD $MODULE_AT debug=$DEBUG firmware_dir=$FIRMWARE_AT
if test "$?" = "0"; then echo "Module successfully inserted."; else echo "Error while inserting module! Bailing..."; exit 1; fi

if test -n "$IWCONF"; then

if test -n "$RATE"; then
  echo Setting rate to $RATE.
  $IWCONF $DEV rate $RATE
  test "$?" != "0" && echo Failed.
fi
if test -n "$CHAN"; then
  echo Setting channel $CHAN.
  $IWCONF $DEV channel $CHAN
  test "$?" != "0" && echo Failed.
fi
if test -n "$TXPOWER"; then
  echo Setting Tx power level to $TXPOWER dBm.
  $IWCONF $DEV txpower $TXPOWER
  test "$?" != "0" && echo Failed.
  sleep 1
fi

echo Trying to join or setup ESSID $ESSID.
$IWCONF $DEV essid "$ESSID"
test "$?" != "0" && echo Failed.

if test -n "$MODE"; then
  echo Setting mode to $MODE.
  $IWCONF $DEV mode $MODE
  test "$?" != "0" && echo Failed.
fi

if test -n "$KEY"; then
  echo Setting key to $KEY, algorithm $ALG.
  $IWCONF $DEV key $ALG "$KEY"
  test "$?" != "0" && echo Failed.
fi

fi

# for notebook use - a power LED is sooo useless anyway ;-))
#test -n "$IWPRIV" && "$IWPRIV" $DEV set_led_power 0


if test $USE_DHCP -eq 1; then
  # fetch an IP address from DHCP
  rm -f /etc/dhcpc/dhcpcd-$DEV.pid > /dev/null
  dhcpcd -d $DEV -t 5
  # OR
  # pump -i $DEV
else
  # Hehe, this can be done after iwconfigs now :)
  $IFCONF $DEV $IP netmask $NETMASK
  if test "$?" != "0"; then echo "Error in \"$IFCONF $DEV $IP netmask $NETMASK\". Bailing..."; exit 1; else echo "Interface has been set up successfully."; fi
  
  test -n "$GATEWAY" && $ROUTE add default gw $GATEWAY
fi

# Finally, let's do some tweaking to make sure we don't have any
# buffer management problems (yeah, it's an ugly workaround!)
$IFCONF $DEV mtu 576
if test "$?" != "0"; then echo "Error in \"$IFCONF $DEV mtu 576\". Bailing..."; exit 1; fi

# just in case ;)
$SYNC
