#!/bin/sh

INTERFACE="eth1"
MODULE="ga_linuxdrv_211_imx_sdio"

LOOP_LIMIT=100     # rounds to wait for wlan device
DEBUG="off"        # switch debug: {on|off}
ETH0_DISABLE="yes" # disable eth0 first: {yes|no}
ETH0_ENABLE="yes"  # enable eth0: {yes|no}

### basic functions
### (called by start/stop/restart_hard/restart_soft)

# disable eth0 if ETH0_DISABLE=yes (ifup)
eth0_disable() {
    if [ "$ETH0_DISABLE" = "yes" ]; then
	ifdown eth0
    fi
}

# enable eth0 if ETH0_ENABLE=yes (ifdown)
eth0_enable() {
    if [ "$ETH0_ENABLE" = "yes" ]; then
	ifup eth0
    fi
}

# load wlan kernel driver (modprobe) and wait LOOP_LIMIT loops for wlan device
wlan_driver_load() {
    LOOP_RESULT="running"
    LOOP_COUNT=0

    if [ ! -d /sys/module/$MODULE ]; then
	modprobe $MODULE
	if [ ! -d /sys/module/$MODULE ]; then
	    echo "error while loading the wlan kernel module!"
	    exit 1
	fi
    fi

    while [ "$LOOP_RESULT" = "running" ]; do
	if [ -d /sys/class/net/$INTERFACE ]; then
	    LOOP_RESULT="$INTERFACE found"
	fi
	usleep 100000
	let LOOP_COUNT=$LOOP_COUNT+1
	if [ "$LOOP_COUNT" = "$LOOP_LIMIT" ]; then
	    LOOP_RESULT="timeout $INTERFACE not found"
	fi
    done
    if [ "$DEBUG" != "off" ]; then
	echo "Result: "$LOOP_RESULT" (Loop Count: "$LOOP_COUNT")"
    fi
}

# unload wlan kernel driver (modprobe -r)
wlan_driver_unload() {
    modprobe -r $MODULE
}

# bring up wlan network interface (ifup)
wlan_interface_up() {
    ifup $INTERFACE
}

# turn off wlan network interface (ifdown)
wlan_interface_down() {
    ifdown $INTERFACE
}

# start wpasupplicant
wpasupplicant_start() {
    wpa_supplicant -Dphilips -ieth1 -c/etc/wpasupplicant.cfg -B > /dev/null
}

# terminate wpasupplicant
wpasupplicant_stop() {
    wpa_cli terminate 2>&1 1> /dev/null
}

### start/stop/restart_hard/restart_soft
### (called by parameter switch)

start() {
    if [ "$DEBUG" != "off" ]; then
	echo "Starting BGW211 WLAN"
    fi

    eth0_disable
    wlan_driver_load
    wlan_interface_up
    wpasupplicant_start
}

stop() {
    if [ "$DEBUG" != "off" ]; then
	echo "Stopping BGW211 WLAN"
    fi

    wpasupplicant_stop
    wlan_driver_unload
    wlan_interface_down
    eth0_enable
}

restart_hard() {
    if [ "$DEBUG" != "off" ]; then
	echo "Restarting BGW211 WLAN (hard)"
    fi

    stop
    sleep 2
    start
}

restart_soft() {
    if [ "$DEBUG" != "off" ]; then
	echo "Restarting BGW211 WLAN (soft)"
    fi

    wpasupplicant_stop
    wpasupplicant_start
}

### parameter switch

case $1 in
    restart_hard)
	# restart everything (wpasupplicant, network interface, kernel driver)
	# with a stop-sleep-start sequence
	restart_hard
	;;
    restart_soft)
	# terminate and restart wpasupplicant only, don't touch wlan kernel
	# driver and network interfaces
	restart_soft
	;;
    stop)
	# terminate wpasupplicant, unload the kernel driver, turn off the
	# network interface and restart eth0 network interface (if configured)
	stop
	;;
    start|*)
	# disable eth0 (if configured), load kernel driver, bring up the
	# network interface and start wpasupplicant to connect to the ap
	start
	;;
esac
