#!/bin/sh

# Buyer beware! This is really only useful if you want to use
# wpasupplicant in roaming mode. 
#
# PLEASE READ /usr/share/doc/wpasupplicant/README.modes for 
# details of the modes of operation of wpasupplicant.
#

# To activate the wpasupplicant daemon, set ENABLE_ROAMING_MODE to 1

ENABLE_ROAMING_MODE=1

# Location of the wpa_supplicant.conf configuration file to be used
# by the daemon.
#
# See the wpa_supplicant.conf(5) manpage for detailed information
# about the wpa_supplicant configuration file. Example configuration
# files are located at /usr/share/doc/wpasupplicant/examples.
#
# Failure to set CONFIG to the full pathname of your configuration
# file will cause the daemon to exit immediately.

CONFIG="/etc/wpa_supplicant.conf"

# Specify the network interface that the daemon will attach to.
#
# Examples: INTERFACE="eth1"
#           INTERFACE="ath0"
#
# Failure to set INTERFACE to a valid network interface name will
# cause the daemon to fail or exit immediately.

INTERFACE="ath0"

# DRIVER specifies the driver type of the interface defined above.
#
# If DRIVER is not set, the daemon will default to the "wext" driver
#
# Currently, the following drivers are supported:
#  hostap = Host AP driver (Intersil Prism2/2.5/3)
#  madwifi = MADWIFI 802.11 support (Atheros, etc.)
#  atmel = ATMEL AT76C5XXx (USB, PCMCIA)
#  wext = Linux wireless extensions (generic, ipw2100/2200/3495, linux >= 2.6.14)
#  ndiswrapper = Linux ndiswrapper
#  ipw = Intel ipw2100/2200 driver (linux kernels 2.6.13 or older only)
#  wired = wpa_supplicant wired Ethernet driver

DRIVER="madwifi"

# End user defined section

PATH=/sbin:/bin:/usr/sbin:/usr/bin

DAEMON="/sbin/wpa_supplicant"
PNAME="wpa_supplicant"

# exit silently if daemon is not installed/executable
[ -x $DAEMON ] || exit 0

if [ "$ENABLE_ROAMING_MODE" = "0" ]; then
        exit 0;
fi

if [ -z "$INTERFACE" ]; then
        echo "No INTERFACE was defined, not starting.";
        exit 1;
fi

if [ ! -r "$CONFIG" ]; then
        echo "No configuration file found, not starting."; 
        exit 1; 
fi

if [ -z "$DRIVER" ]; then
        echo "No DRIVER was defined, not starting.";
        exit 1;
fi

# the pidfile name is important for avoiding unwanted interactions
# with the wpasupplicant pre-up script
PIDFILE="/var/run/wpa_supplicant.$INTERFACE.pid"

OPTIONS="-B -w -i $INTERFACE -D $DRIVER -c $CONFIG -P $PIDFILE"

set -e

case "$1" in
        start)
                if [ -f "$PIDFILE" ]; then
			echo "$PNAME not starting, $PIDFILE already exists."
			exit 1
		fi
		echo "Starting $PNAME."
                start-stop-daemon --start --name $PNAME \
                        --oknodo --startas $DAEMON -- $OPTIONS
                ;;
        stop)
                echo "Stopping $PNAME."
                start-stop-daemon --stop --name $PNAME \
                        --oknodo
                if [ -f "$PIDFILE" ]; then
                        rm -f $PIDFILE;
                fi
                ;;
        reload|force-reload)
                echo "Reloading $PNAME."
                start-stop-daemon --stop --signal HUP \
                        --name $PNAME
                ;;
        restart)
                echo "Stopping $PNAME."
                start-stop-daemon --stop --name $PNAME \
                        --oknodo
                if [ -f "$PIDFILE" ]; then
                        rm -f $PIDFILE;
                fi

                echo "Starting $PNAME."
                start-stop-daemon --start --name $PNAME \
                        --oknodo --startas $DAEMON -- $OPTIONS
                ;;
        *)
                echo "Usage: wpasupplicant {start|stop|restart|reload|force-reload}" >&2
                exit 1
                ;;
esac

exit 0 
