# - private.bashlib -
# - WatchWB -
# Settings & functions private to this module
#
get_method() {
local funtag="[${FUNCNAME[0]}]"
	echo "$REPLY" | awk '
	/[ \042]*(GET )/	{ print "GET";		exit }
	/[ \042]*(POST )/	{ print "POST";		exit }
	/[ \042]*(PUT )/	{ print "PUT";		exit }
	/[ \042]*(HEAD )/	{ print "HEAD";		exit }
	/[ \042]*(DELETE )/	{ print "DELETE";	exit }
	/[ \042]*(OPTIONS )/	{ print "OPTIONS";	exit }
	' <<< $REPLY
}

get_instance() {
local funtag="[${FUNCNAME[0]}]"
	awk '
	match($0,"[[]Instance: ") {
		tail=substr($0,RSTART+RLENGTH)
		split(tail,parts,":")
		print parts[1]
	}' <<< $REPLY
}

get_insttype() {
local funtag="[${FUNCNAME[0]}]"
	awk '
	match($0,"]@") { print substr($0,RSTART+RLENGTH) }
	' <<< $REPLY
}

get_httpcode() {
local funtag="[${FUNCNAME[0]}]"
	awk 'match($0,/[ ]+[1-9][0-9]{2}[ ]+/) {
		print substr($0,RSTART+1,RLENGTH-2) 
	}
	' <<< $REPLY
}

write_affairs() {
local	u_seconds=`date +%s%6N`	
	
	$SQL "insert into affairs_by_ip (ip_addr, affair_num, rule, date_event, time_useconds, instance, ruleset)
		VALUES ('$1',$2,'$3', datetime(current_timestamp,'localtime'), $u_seconds, $4,'$5');"
}

get_sets() {
local funtag="[${FUNCNAME[0]}]"
local	sets

	sets=`find rules/ -type d | cut -f2 -d"/"`
	echo $sets
}

#
# Create the dispatcher 'filter'
#
filter() {
# $1	... the instance type
local funtag="[${FUNCNAME[0]}]"
local retcode rs
	# Call ruleset for the instance typ first
	# and the 'Common' ruleset as a catch-all
	if [ ! -z "$1" ]
	then
	#	for rs in "$1" Common
		for rs in Common "$1" 
		do	RULESET=$rs
			ruleset-$rs; retcode=$?
			[[ $retcode -ne 0 ]] && return $retcode
		done
	else	trace "$funtag No ruleset for $REPLY"
	fi

	# Never reached on match in a ruleset
	RULE=NO_RULE_MATCH
	return 0
}

