#!/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

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

run_rcs() {
    for RC in /etc/hotplug/*.rc; do
	basename=${RC#/etc/hotplug/}
	name=${basename%.rc}
	if [ "$1" != status ]; then
	    if [ "$(eval echo \$HOTPLUG_RC_$name)" = no ]; then
		continue
	    fi
	    echo -n " $name"
	fi
	$RC $1 || echo -n "[failed] "
    done
}


case "$1" in
start)
    echo -n "Starting hotplug subsystem (runlevel $runlevel):"
    if [ -f /proc/sys/kernel/hotplug ]; then
      echo /sbin/hotplug > /proc/sys/kernel/hotplug
      run_rcs $1
    else
      echo " kernel has no hotplug support. skipped"
    fi
    echo "."
    ;;

stop)
    echo -n "Stopping hotplug subsystem (runlevel $runlevel):"
    if [ -f /proc/sys/kernel/hotplug ]; then
      case R"$runlevel" in
      R0|R6)
	;;
      *)
    	run_rcs $1
	;;
      esac
      echo /bin/true > /proc/sys/kernel/hotplug
    else
      echo " kernel has no hotplug support. skipped"
    fi
    echo "."
    ;;

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
