#!/bin/sh
#
# udevd-final	Perform final udevd startup steps.
#
# chkconfig: 2345 32 69
# description:	Retries udev events which could not be processed earlier \
#		during startup.

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE=/var/lock/subsys/udevd-final
RETVAL=0

start()
{
	local f t udev_root
	udev_root=$(udevadm info --run 2>/dev/null)
	[ -w /etc/udev/rules.d/ -a -n "$udev_root" ] &&
		for f in $udev_root/tmp-rules--*; do
			[ -s "$f" ] || continue
			t="${f##*/}"
			t="${t#tmp-rules--}"
			[ -n "$t" ] || continue
			cat "$f" >>/etc/udev/rules.d/"$t"
		done
	action "Handling remaining udev events:" udevadm trigger --type=failed \
		&& touch "$LOCKFILE"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	rm -f "$LOCKFILE"
}

restart()
{
	stop
	start
}

status()
{
	if [ -f "$LOCKFILE" ]; then
		echo "This service was last time (re-)started at $(LANG=C LANGUAGE=C /bin/ls -l --time-style='+%Y-%m-%d %H:%M:%S %z' "$LOCKFILE" |tr -s ' ' |cut --fields 6-8 -d' ')."
		echo "No other status information available."
	else
		echo "This service hasn't been started since stopped last time."
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload)
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart|condreload)
		# "condrestart" is called during package upgrade.
		# Nothing to do here - this is not a normal service.
		;;
	status)
		status
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|reload|status|condrestart|condreload|condstop}"
		RETVAL=1
esac

exit $RETVAL
