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

. /lib/lsb/init-functions
. /etc/default/rcS

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

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

run_rcs() {
    PRINTK=`cat /proc/sys/kernel/printk`
    [ "$VERBOSE" == no ] && echo "0 0 0 0" > /proc/sys/kernel/printk
    for RC in /etc/hotplug/*.rc; do
	basename=${RC#/etc/hotplug/}
	name=${basename%.rc}
	[ "$VERBOSE" != no ] && log_begin_msg "Running $basename..."
	if [ "$(eval echo \$HOTPLUG_RC_$name)" = no ]; then
	    [ "$VERBOSE" != no ] && log_end_msg 1 || true
	    continue
	fi
	set +e
	if [ "$VERBOSE" != no ]; then
		$RC $1
		RC_STATUS=$?
	else
		$RC $1 >/dev/null 2>&1
		RC_STATUS=$?
	fi
	set -e
	if [ "$1" = status ]; then
	    continue
	fi
	[ "$VERBOSE" != no ] && log_end_msg $RC_STATUS || true
    done
    echo "$PRINTK" > /proc/sys/kernel/printk
    return 0
}


case "$1" in
start)
    log_begin_msg "Starting hotplug subsystem..."
    echo /sbin/hotplug > /proc/sys/kernel/hotplug
    run_rcs $1
    log_end_msg 0
    ;;

stop)
    log_begin_msg "Stopping hotplug subsystem..."
    run_rcs $1
    echo /bin/true > /proc/sys/kernel/hotplug
    log_end_msg 0
    ;;

restart|force-reload)
    log_begin_msg "Restarting hotplug subsystem..."
    run_rcs stop
    run_rcs start
    log_end_msg 0
    ;;

status)
    run_rcs $1
    ;;

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

exit 0
