#! /bin/ash
# N.B. /bin/ash doesn't exist until after "busybox --install" has been run,
# so this line will prevent premature execution of this script.  The ordering
# of /etc/rc ensures that core filesystems (/, /dev, /proc, /sys, /tmp) are
# mounted at this time.

# Minimalist hotplug handler for DIET-PC.
# Copyright (C) 2005-2009 Paul A Whittaker <whitpa@users.sourceforge.net>

# Only the following variables are common to both 2.4 and 2.6 kernels:

PATH=/bin:/sbin:/usr/sbin:/usr/local/sbin
export PATH

# For kernels < 2.6.15 we need to use a helper program to generate events that
# udevd can detect.
KVER=`uname -r | sed 's/^\([0-9.]\+\).*$/\1/'`
KSERIES=`echo $KVER | cut -f1-2 -d.`
KMINOR=`echo $KVER | cut -f3 -d.`
[ \( "$KMINOR" -lt 15 -a "$KSERIES" = '2.6' \) -a -d /sys/block ] && udev

case "$1" in
pci)
    logger -t hotplug -p daemon.debug "$1 $ACTION request for ID $PCI_ID, subsystem $PCI_SUBSYS_ID, class $PCI_CLASS, slot $PCI_SLOT_NAME"
    ;;
usb)
    logger -t hotplug -p daemon.debug "$1 $ACTION request for product $PRODUCT, interface $INTERFACE, type $TYPE, device $DEVICE"
    sleep 1
    case "$ACTION" in
    add)
	usbmodules --device "$DEVICE" | while read MODULE; do
	    logger -t hotplug -p daemon.info "attempting to load $MODULE"
	    modprobe -q "$MODULE"
	done
	;;
    esac
    case "$INTERFACE" in
    # USB interface class 8 is mass storage.
    8/*)
	modprobe -q sd_mod
	;;
    esac
    ;;
# Trigger storage_detect for all Block Device Interface addition and removal
# events.  This will catch changes in virtual devices (such as RAID
# metadevices) as well as physical ones.
bdi)
    case "$ACTION" in
    add|remove)
	[ "`which storage_detect`" ] && storage_detect &
	;;
    esac
    logger -t hotplug -p daemon.debug "$1 $ACTION request for BDI (devpath $DEVPATH)"
    ;;
net)
    [ "$INTERFACE" != 'eth0' ] && case "$ACTION" in
    add)
	# Raise the interface to trigger any firmware loads.
	ifconfig $INTERFACE up
	sleep 1
	# If the interface is wireless, and wpa_supplicant is installed, and
	# there is a configuration file for this interface, then start
	# wpa_supplicant and hope for the best.
	if [ "`grep \\\<$INTERFACE\\\> /proc/net/wireless`" -a \
		"`which wpa_supplicant`" -a -f \
		/etc/wpa_supplicant/$INTERFACE.conf -a ! -e \
		/var/run/wpa_supplicant/$INTERFACE ]; then
	    wpa_supplicant -D wext -i $INTERFACE -c \
		    /etc/wpa_supplicant/$INTERFACE.conf
	    sleep 5
	fi
	# Start an additional instance of udhcpc to manage the interface.
	# Unlike eth0, /sbin/udhcpc-helper is granted full control of the
	# interface (/bin/discover cannot use or configure interfaces other
	# than eth0).
	if [ ! -f /var/run/udhcpc.$INTERFACE.pid ]; then
	    udhcpc -n -t 5 -i $INTERFACE -p \
		    /var/run/udhcpc.$INTERFACE.pid -s /sbin/udhcpc-helper \
		    >/dev/null 2>&1
	fi
	;;
    remove)
	[ -f /var/run/udhcpc.$INTERFACE.pid ] && kill -TERM `cat \
		/var/run/udhcpc.$INTERFACE.pid` 2>/dev/null
	[ -f /var/run/wpa_supplicant.$INTERFACE.pid ] && kill -TERM `cat \
		/var/run/wpa_supplicant.$INTERFACE.pid` 2>/dev/null
	;;
    esac
    logger -t hotplug -p daemon.debug "$1 $ACTION request for interface $INTERFACE"
    ;;
firmware)
    case "$ACTION" in
    add)
	if [ -f /lib/firmware/$FIRMWARE -a -f /sys/$DEVPATH/loading ]; then
	    echo 1 >/sys/$DEVPATH/loading
	    cp /lib/firmware/$FIRMWARE /sys/$DEVPATH/data
	    echo 0 >/sys/$DEVPATH/loading
	fi
	logger -t hotplug -p daemon.debug "$1 $ACTION request for firmware $FIRMWARE, device `basename $DEVPATH`"
	;;
    esac
    ;;
*)
    logger -t hotplug -p daemon.debug "$1 $ACTION request (devpath $DEVPATH)"
    ;;
esac
