#!/bin/bash
# $Id: coldplug,v 1.6 2004/04/02 12:10:34 adrian Exp $
#
#
### BEGIN INIT INFO
# Provides:       coldplug
# Required-Start:
# Should-Start: hotplug
# Required-Stop: syslog
# Should-Stop:
# Default-Start: 1 2 3 5
# Default-Stop:
# Short-Description: initializes fixed and already plugged devices
# Description:
### END INIT INFO
 
. /etc/rc.status
rc_reset
 
START_EVENTS=/etc/sysconfig/hardware/coldplug.start.events
STOP_EVENTS=/etc/sysconfig/hardware/coldplug.stop.events
HWCFG_STUB=/etc/sysconfig/hardware/hwcfg-
RUNFILE=/var/run/hotplug/coldplug
HOTPLUGDIR=/etc/hotplug

shopt -s nullglob

case "$1" in

	start)

		# If booting with coldplug locks the system,
		# set NOCOLDPLUG at the Boot-Prompt
		if [ -n "$NOCOLDPLUG" ] ; then
			echo -n "Coldplug will not be executed due to NOCOLDPLUG=$NOCOLDPLUG"
			rc_failed 5
			rc_status -v
			rc_exit
		fi

		touch $RUNFILE
		echo -n coldplug

		for CFG in ${HWCFG_STUB}static*; do
			/sbin/hwup ${CFG#$HWCFG_STUB} -o auto
		done

		if [ -r $START_EVENTS ] ; then
			# echo "Coldplug: triggering start events:"
			while read CLASS ENV; do
				case $CLASS in \#*|"") continue;; esac
				echo " trigger event $CLASS ACTION=add $ENV"
				export ACTION=add
				for VAR in $ENV; do
					export $VAR
				done
				/sbin/hotplug $CLASS
				rc_status -v
				echo -n "        "
				rc_reset
			done < $START_EVENTS
		fi

		for RC in $HOTPLUGDIR/*.rc; do
			for SKIP in $COLDPLUG_DONT_START_SUBSYSTEMS; do
				test `basename $RC .rc` = $SKIP \
					&& continue 2
			done
			echo -n " scanning `basename $RC .rc`: "
			$RC start
			rc_status -v
			echo -n "        "
			rc_reset
		done

		echo -n " .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  ."
		rc_status -v
		;;

	stop)

		for RC in $HOTPLUGDIR/*.rc; do
			for SKIP in $COLDPLUG_DONT_STOP_SUBSYSTEMS; do
				test `basename $RC .rc` = $SKIP \
					&& continue 2
			done
			$RC stop
			rc_status
		done

		for SUBS in $COLDPLUG_STOP_SUBSYSTEMS; do
			test -x /etc/hotplug/${SUBS}.agent || continue
			/etc/hotplug/${SUBS}.agent stop
			rc_status
		done

		if [ -r $STOP_EVENTS ] ; then
			echo "Coldplug: triggering stop events:"
			while read CLASS ENV; do
				case $CLASS in \#*|"") continue;; esac
				echo "    $CLASS ACTION=remove $ENV"
				export ACTION=remove
				for VAR in $ENV; do
					export $VAR
				done
				/sbin/hotplug $CLASS
				rc_status
			done < $STOP_EVENTS
		fi

		for CFG in ${HWCFG_STUB}static*; do
			/sbin/hwdown ${CFG#$HWCFG_STUB} -o auto
		done

		rm -f $RUNFILE

		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 if Coldplug was executed "
		test -f $RUNFILE
		rc_status -v
		;;
	*)
		echo $"Usage: $0 {start|stop|[try-]restart|[force]-reload}"
		rc_failed
		;;
esac
rc_exit
