#!/bin/sh
#
# udev	This is a minimal non-LSB version of a UDEV startup script.  It
#	was derived by stripping down the udev-058 LSB version for use
#	with buildroot on embedded hardware using Linux 2.6.34+ kernels.
#
#	You may need to customize this for your system's resource limits
#	(including startup time!) and administration.  For example, if
#	your early userspace has a custom initramfs or initrd you might
#	need /dev much earlier; or without hotpluggable busses (like USB,
#	PCMCIA, MMC/SD, and so on) your /dev might be static after boot.
#
#	This script assumes your system boots right into the eventual root
#	filesystem, and that init runs this udev script before any programs
#	needing more device nodes than the bare-bones set -- /dev/console,
#	/dev/zero, /dev/null -- that's needed to boot and run this script.
#

DAEMON="udevd"
PIDFILE="/var/run/$DAEMON.pid"

SETTLE_TIMEOUT=30
UDEVD_ARGS=""

# shellcheck source=/dev/null
[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON"

start() {
	printf "Starting %s: " "$DAEMON"
	[ -e /proc/sys/kernel/hotplug ] && printf '\000\000\000\000' > /proc/sys/kernel/hotplug
	# shellcheck disable=SC2086 # we need the word splitting
	start-stop-daemon --start --background --make-pidfile \
		--pidfile "$PIDFILE" --exec "/sbin/$DAEMON" \
		-- $UDEVD_ARGS
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	udevadm trigger --type=subsystems --action=add
	udevadm trigger --type=devices --action=add
	udevadm settle --timeout=$SETTLE_TIMEOUT || echo "udevadm settle failed"
	return "$status"
}

stop() {
	printf "Stopping %s: " "$DAEMON"
	udevadm control --exit
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
		return "$status"
	fi
	while start-stop-daemon --stop --test --quiet --pidfile "$PIDFILE" \
		--exec "/sbin/$DAEMON"; do
		sleep 0.1
	done
	rm -f "$PIDFILE"
	return "$status"
}

restart() {
	stop
	start
}

reload() {
	printf "Reloading %s config: " "$DAEMON"
	udevadm control --reload
	status=$?
	if [ "$status" -eq 0 ]; then
		echo "OK"
	else
		echo "FAIL"
	fi
	return "$status"
}

case "$1" in
	start)
		start;;
	stop)
		stop;;
	restart)
		restart;;
	reload)
		reload;;
	*)
		echo "Usage: $0 {start|stop|restart|reload}"
		exit 1
esac

exit $?
