#!/bin/sh
#
# $NetBSD: rndctl,v 1.3 2009/04/21 16:08:57 joerg Exp $
#

# PROVIDE: rndctl
# BEFORE:  DISKS ike ipsec sshd
# REQUIRE: wdogctl

$_rc_subr_loaded . /etc/rc.subr

name="rndctl"
rcvar=$name
command="/sbin/${name}"

start_cmd="rndctl_startcmd"

rndctl_startcmd()
{
	# $rndctl_flags can contain multiple semicolon-separated
	# segments in which each segment contains optional flags
	# followed by one or more device or type names.  If none of the
	# -c/-C/-e/-E flags is specified, then "-c -e" is used.  If
	# neither of the -d/-t flags is specified, then "-d" is used.
	#
	# For example, given
	#	rndctl_flags="wd0 wd1; -t tty; -c -t net"
	# we will perform the following commands:
	#	rndctl -c -e -d wd0
	#	rndctl -c -e -d wd1
	#	rndctl -c -e -t tty
	#	rndctl -c -t net

	local args arg flags

	# Split $rndctl_flags on semicolons
	oIFS="$IFS"
	IFS=';'
	set -- $rndctl_flags
	IFS="$oIFS"
	# The outer "for args" loop cycles once per semicolon-separated
	# segment; the inner "for arg" loop cycles once per word in a
	# segment.
	for args in "$@"; do
		#echo >&2 "${name} DEBUG: Parsing segment: $args";
		flags=''
		for arg in ${args}; do
			case "${arg}" in
				-*)
					flags="${flags} ${arg}"
					;;
				*)
					# We have a device or type name.
					# If none of -c/-C/-e/-E flags was
					# specified, add "-c -e".  If neither
					# of -d/-t was specified, add "-d".
					# Then perform the command with the
					# specified device or type name.
					#
					# Note that -d/-t flag must be last.
					#
					case "${flags}" in
					*[cCeE]*) ;;
					*)	flags="-c -e ${flags}" ;;
					esac
					case "${flags}" in
					*[dt]*) ;;
					*)	flags="${flags} -d" ;;
					esac
					#echo >&2 "${name} DEBUG: running:" \
					#    "$command $flags $arg"
					$command ${flags} ${arg}
					;;
			esac
		done
	done
}

load_rc_config $name
run_rc_command "$1"
