# A simple module dependancy system used to partially check the consistency
# of the loaded router configuration.  Any other .cf file should contain
# a call to 'require' and/or 'provide', as appropriate.  At the end of the
# load phase, the 'consist' function should be called with no arguments.
# If any cf facility (module, file, whatever) required hasn't been provided,
# the router will exit with an appropriate message.  This file should be
# the first to be loaded.

unset require
unset provide
require=()
provide=()

declare (plist, m) {
	lreplace $plist $m $m
}

require () {
	for x in $@
	do
		declare require $x
	done
}

provide () {
	for x in $@
	do
		declare provide $x
	done
}

consist () {
	local rval

#grind $require
#grind $provide

	rval=0
	# Scan for EVERY SECOND token of the array..
	while [ $#require -gt 0 ];
	do
		want=$(car $require)
		if [ -n "$(get provide $want)" ]; then
			:
		else
			echo $0: $want not provided
			rval=1
		fi
		require=$(cdr $(cdr $require))
	done
	return $rval
}

