#!/bin/bash
# $Id: coldplug,v 1.7 2004/07/29 09:38:59 hare Exp $
#
#
### BEGIN INIT INFO
# Provides:       boot.coldplug
# Required-Start:
# Should-Start:   boot.udev
# Required-Stop:
# Should-Stop:
# Default-Start: B
# Default-Stop:
# Short-Description: initializes fixed and already plugged devices
# Description:
### END INIT INFO
 
. /etc/rc.status
. /etc/sysconfig/hotplug

rc_reset
 
# We do some actions only at boottime.
# Therefore we check if we were called from init
test -n "$INIT_VERSION" && BOOT=yes

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

		echo -n Coldplug

		if [ "$BOOT" = yes ] ; then
			if ls /events/debug* >/dev/null 2>&1 ; then
			echo -ne "\nColdplug: cleaning /events "
			for ev in /events/debug.*; do
				rm -f $ev
				done
			fi
			NETWORK_RUN_FILE=/var/run/sysconfig/network
			if [ -f "$NETWORK_RUN_FILE" ] ; then
				echo -ne "\nColdplug: removing left over" \
				         "$NETWORK_RUN_FILE "
				rm -f "$NETWORK_RUN_FILE"
			fi
		fi

 		# We have to call this before replay of initial events, because
 		# we want network interfaces that are registered from devices
 		# with a static configuration become eth0, eth1 and so on. We
 		# use this explicitely in some update cases.
		# Hopefully we do not interfere to much with the order of
		# initial events
		for CFG in ${HWCFG_STUB}-static*; do
		    if [ -f $CFG ]; then
			/sbin/hwup ${CFG#$HWCFG_STUB-} ${CFG#$HWCFG_STUB-static-} -o auto
		    fi
		done

                if [ -d /lib/klibc/events ] ; then
		    # initramfs will store all events in /lib/klibc/events
		    echo -n " processing events"
		    if [ -f /dev/.udevdb/udevd.pid ]; then
			read UDEVD_PID < /dev/.udevdb/udevd.pid
			if [ -n "$UDEVD_PID" ] && [ "$UDEVD_PID" -ne 0 ]; then
			    kill -USR1 $UDEVD_PID
			fi
		    fi
		    /sbin/udevinitsend -d /lib/klibc/events
		    if [ "$HOTPLUG_DEV_ON_TMPFS" == "no" ]; then
			umount /lib/klibc/events
		    fi
		    if [ -n "$UDEVD_PID" ] && [ "$UDEVD_PID" -ne 0 ]; then
			kill -USR2 $UDEVD_PID
		    fi
		    rc_reset
                else

 		    # Rebuild the db in the background
		    # If /dev is in tmpfs, all nodes and symlinks are already there,
		    # but the udevdb in initramfs may differ from the one
		    # in the installed system.
		    # If /dev is on a disk, all nodes are already there,
		    # but the by-path/by-id symlinks may need an update.
		    export UDEV_NO_DEVD=1
		    export UDEV_NO_HOTPLUGD=1
		    /sbin/udevstart &
		    unset UDEV_NO_HOTPLUGD
		    unset UDEV_NO_DEVD

		    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
		fi

		rc_status -v
		;;

	stop)

		for CFG in ${HWCFG_STUB}-static*; do
		    if [ -f $CFG ]; then
			/sbin/hwdown ${CFG#$HWCFG_STUB-} ${CFG#$HWCFG_STUB-static-} -o auto
		    fi
		done

		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
