#!/bin/sh
# hotplug agent script

DAEMON_NAME=wpa_supplicant

DAEMON=/usr/sbin/$DAEMON_NAME
CLI=/usr/sbin/wpa_cli
CFG=/etc/wpa_supplicant.conf
HOTPLUG_INTERFACES="all"

[ -x $DAEMON ] || exit 0

HOTPLUGFUNCS=/etc/hotplug/hotplug.functions
[ -f $HOTPLUGFUNCS ] || exit 1
. $HOTPLUGFUNCS
DEBUG=yes export DEBUG

[ "$INTERFACE" ] || { mesg Bad invocation: \$INTERFACE is not set ; exit 1 ; }
case "$INTERFACE" in
    # ath* is for Atheros devices using the madwifi driver
    eth*|wlan*|ath*)
        :
        ;;
    *)
        exit 0
        ;;
esac

[ -f $CFG ] || { mesg No $DAEMON_NAME configuration file ; exit 1 ; }

case $ACTION in
add|register)
    for IF in $HOTPLUG_INTERFACES ; do
        if [ "$INTERFACE" = "$IF" -o "$IF" = "all" ] ; then
            # Driver detection; incomplete and untested apart from ndiswrapper
            DRIVER=""
            # hostap = Host AP driver (Intersil Prism2/2.5/3)
            [ -d /proc/net/hostap/$INTERFACE ] && DRIVER="hostap"

            # prism54 = Prism54.org driver (Intersil Prism GT/Duette/Indigo)
            # TODO: find detection method for prism54

            # madwifi = MADWIFI 802.11 support (Atheros, etc.)
            case "$INTERFACE" in
                ath*)
                    DRIVER="madwifi"
                    ;;
            esac

            # atmel = ATMEL AT76C5XXx (USB, PCMCIA)
            # TODO: find detection method for atmel

            # wext = Linux wireless extensions (generic)
            [ -d /proc/net/p80211/$INTERFACE ] && DRIVER="wext"

            # ndiswrapper = Linux ndiswrapper
            [ -d /proc/net/ndiswrapper/$INTERFACE ] && DRIVER="ndiswrapper"

            # ipw = Intel ipw2100/2200 driver
            # TODO: find detection method for ipw

            [ -n $DRIVER ] || exit 0 # No driver detected? Not supported

            debug_mesg Invoking $DAEMON_NAME for $INTERFACE, driver: $DRIVER

            $DAEMON -c$CFG -i$INTERFACE -D$DRIVER -B 
            break
        fi
    done
    ;;
remove|unregister)
    debug_mesg Stopping $DAEMON_NAME for $INTERFACE
    $CLI -i$INTERFACE terminate
    ;;
esac
