#!/bin/bash
if [[ "$1" == 'debug'  ]]; then set -x;		_DEBUG=$1; shift; fi
if [[ "$1" == 'debug2' ]]; then set -xvT;	_DEBUG=$1; shift; fi
if [[ "$1" == 'trace'  ]]; then 		_TRACE=$1; shift; fi
####################################################################
# - mkrulesets -

mk_ruleset() {
local	outfile="ruleset-$1"
local	rset
local	stamp

echo "# - Ruleset $1 -
# Do NOT edit this file as it is generated every time the module starts
# and provides the '$outfile' function
#"						 > $outfile

echo "$outfile() {"				>> $outfile
echo 'local funtag="[${FUNCNAME[0]}]"'		>> $outfile
echo ": FILTER_STATE=$STATE"        		>> $outfile
echo ": FILTER_DATE=$STAMP"     		>> $outfile

for rset in `echo rules/$1/*.rule`
do 
#	cat $rset				>> $outfile
	awk '
	/^[ \t]*#/	{ next }
	/^$/		{ next }
			{ print }
	' $rset	>>$outfile
done

echo '
RULE=NO_RULESET_MATCH-$funtag
return 0
}' >> $outfile
}

STATE=$1
STAMP="`date --iso=seconds`"

SETS=`find rules/ -type d | cut -f2 -d"/"`
: echo "Sets:" $SETS

#
# Create instance specific rulesets 
#
for s in `echo $SETS`
do
	SETNAME=`basename $s`
	mk_ruleset $SETNAME
done

