#!/bin/bash
# $Id: hotplug,v 1.7 2004/04/03 16:03:38 zoz Exp $
#
#
### BEGIN INIT INFO
# Provides:       hotplug
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 1 2 3 5
# Default-Stop:
# Short-Description: enables automatic initialisation of hotplugged devices
# Description:
### END INIT INFO
 
. /etc/rc.status
rc_reset
 
HOTPLUGDIR=/etc/hotplug
PROCFILE=/proc/sys/kernel/hotplug

shopt -s nullglob

# When booting show another message, because we don't really enable anything
# Therefore determine the base, follow a runlevel link name ...
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
# ... and compare them
BOOT=no
if [ $link != $base ] ; then
	BOOT=yes
fi

case "$1" in

	start)

		# If booting with hotplug locks the system,
		# set NOHOTPLUG at the Boot-Prompt
		if [ -n "$NOHOTPLUG" ] ; then
			echo -n "Hotplug will be disabled due to NOHOTPLUG=$NOHOTPLUG"
			echo /sbin/hotplug-stopped > $PROCFILE
			rc_failed 5
		else
			if [ -f $PROCFILE ] ; then
				if [ "$BOOT" = yes ] ; then
					echo -n "Hotplug is already active  (disable with" \
					        " NOHOTPLUG=1 at the boot prompt)"
				else
					echo -n "enabling Hotplug "
					touch /var/run/hotplug/ignore_nohotplug
				fi
				echo /sbin/hotplug > $PROCFILE
			else
				echo -n "Hotplug: ... no hotplug support in kernel"
				rc_failed 6
			fi
		fi

		rc_status -v
		;;

	stop)

		echo -n "disabling Hotplug "
		if [ -f $PROCFILE ] ; then
			echo /sbin/hotplug-stopped > $PROCFILE
		fi
		rm -f /var/run/hotplug/ignore_nohotplug

		rc_status -v
		;;

	try-restart)

		if $0 status; then
			$0 restart
		else
			rc_reset        # Not running is not a failure.
		fi

		rc_status
		;;

	restart|force-reload)

		$0 stop
		$0 start

		rc_status
		;;

	reload)

		;;

	status)

		echo -n "checking Hotplug "
		if [ -f $PROCFILE ] ; then
			test "`cat $PROCFILE`" = /sbin/hotplug || rc_failed
		else
			echo -n "... no hotplug support in kernel"
			rc_failed 3
		fi

		rc_status -v
		;;

	*)

		echo $"Usage: $0 {start|stop|[try-]restart|[force]-reload}"
		rc_failed
		;;

esac
rc_exit
