#!/bin/sh -e
#
# description:	Starts and stops each hotpluggable subsystem.
#		On startup, may simulate hotplug events for devices
#		that were present at boot time, before filesystems
#		used by /sbin/hotplug became available.

PATH=/sbin:/bin

[ -x /sbin/hotplug ] || exit 0

if [ ! -f /proc/sys/kernel/hotplug ]; then
   echo "Kernel hotplug support not enabled."
   exit 0
fi

[ -e /etc/default/hotplug ] && . /etc/default/hotplug

run_rcs() {
    for RC in /etc/hotplug/*.rc; do
	basename=${RC#/etc/hotplug/}
	name=${basename%.rc}
	printf "   %-8s\n" $name
	if [ "$(eval echo \$HOTPLUG_RC_$name)" = no ]; then
	    printf "   %-8s [disabled]\n" $name
	    continue
	fi
	set +e
	$RC $1
	RC_STATUS=$?
	set -e
	if [ "$1" = status ]; then
	    continue
	fi
	if [ $RC_STATUS = 0 ]; then
	    printf "   %-8s [success]\n" $name
	else
	    printf "   %-8s [failed]\n" $name
	fi
    done
}


case "$1" in
start)
    echo "Starting hotplug subsystem:"
    echo /sbin/hotplug > /proc/sys/kernel/hotplug
    run_rcs $1
    echo "done"
    ;;

stop)
    echo "Stopping hotplug subsystem:"
    run_rcs $1
    echo /bin/true > /proc/sys/kernel/hotplug
    echo "done"
    ;;

restart|force-reload)
    echo -n "Restarting hotplug subsystem:"
    run_rcs stop
    run_rcs start
    echo "."
    ;;

status)
    run_rcs $1
    ;;

*)
    echo "Usage: $0 {start|stop|restart|status|force-reload}" >&2
    exit 1
    ;;
esac

exit 0
