#!/bin/bash
if [ "$1" == 'debug'  ]; then set -x;	shift; fi
if [ "$1" == 'debug2' ]; then set -xvT;	shift; fi
#
# Master module for log watchers 
# Shoot-up watcher modules & dynloaders ...
#
#-------------------------
REALPATH=`realpath $0`
WHERE=`dirname $REALPATH`
ME=`basename $REALPATH`
cd $WHERE
export MASTER_PATH=$WHERE
#-------------------------
. $MASTER_PATH/system.conf
. $MASTER_PATH/watcher.conf
. $MASTER_PATH/common.conf
. $MASTER_PATH/common.bashlib
. $ME.conf

declare -Ax watchermap
watchermap[product]=$PRODUCT
watchermap[revision]=$REVISION

trap cleanup 0 2 9 15
cleanup() {
	logger "$ME[$$]: Terminating - Startup job done"
}

get_count() {
local lists="`ipset -n list`"
local number
local total=0

	for l in $lists
	do
		number=`ipset -t list $l | grep '^Number' | cut -f2 -d":"`
		(( total += $number ))
	done
	echo $total
}

echo "$PRODUCT V$REVISION ... Running on $SYSTEM $SYSVERS $HOSTNAME"
: echo "Language $LANG, LC_ALL: $LC_ALL"

rm -f RAMDISK
if [ ! -z "$USE_RAMDISK" ]
then
	already=`df -lh | grep $ME-ramdisk`
	if [ -z "$already" ]
	then ./mkramdisk
	else echo "RAMdisk: |$already|"
	fi

	if [ ! -z "$USE_RDCOPIES" ]
	then
		if [ ! -d $POOL/bin ]; then mkdir -m 700 -p $POOL/bin; fi

		for c in `echo $RDCOPIES`
		do
			if [ ! -f $POOL/bin/$c ]
			then cp `which $c` $POOL/bin
			fi	
		done
		
		export POOLBIN=$POOL/bin
		export PATH=$POOLBIN:$PATH
		export IPSET=ipset
	fi
fi

# Let tools and modules know where we are
# `locate MASTER_PATH' and cat on the result are the same ...
echo $WHERE > MASTER_PATH

#
# Put tools in place
#
if [ ! -d $TOOLS_LINK ]; then mkdir -m 750 -p $TOOLS_LINK; fi

# Provide the tools in $TOOLSLINK
# 'TOOLS' are defined in watcher.conf
for tool in `echo $TOOLS`
do ln -sf $MASTER_PATH/bin/$tool $TOOLS_LINK
done

DYNLOADERS="`echo dynload/*`"
MODSINSTALLED="`echo modules/*`"
echo
echo -n "Present DynLoaders: "
for d in `echo $DYNLOADERS`
do echo -n "`basename $d` "
done
echo

echo -n "Present Modules   : "
for m in `echo $MODSINSTALLED`
do echo -n "`basename $m` "
done
echo

logger "$ME[$$]: Starting ..."
sleep 1 	# To supress a silly div-by-zero!

echo
echo "Loading firewall '$FIREWALL' ..."
FILL_START=`date +%s%N`
./FillFW
FILL_END=`date +%s%N`

count=`get_count`
diff=`echo "scale=3; ($FILL_END-$FILL_START)/10^9" | bc`
rate=`echo "$count/$diff" | bc`

printf "\t%8s %8.4f seconds\n" "Took" $diff
printf "\t%8s %8d total firewall DROPs\n" "for" $count
printf "\t%8s %8d DROPs per second\n" "Loadrate" $rate


#
## Everything is prepared ...
## Fire up modules after firewall is loaded
#
if [ ! -d $FIFO_BASE ]
then mkdir -m 700 -p $FIFO_BASE
fi

for mod in $MODULES
do
#	STARTMOD="Watch"$mod
	STARTMOD=$mod
	MODPATH=modules/$STARTMOD
	if [ ! -d modules/$STARTMOD ]
	then 	echo "$mod configured but $STARTMOD _not_ installed"
		echo "... skipping ..."
		continue
	else
		if [ ! -p $FIFO_BASE/$STARTMOD ]
		then mkfifo -m 600 $FIFO_BASE/$STARTMOD
		fi

		echo "Starting module $STARTMOD ..."
		# Fork module into background ...
		### Do not start modules with 'nohup' any longer
		### or the filter refresh triggered by a HUP
		### signal does not work!
		# nohup $MODPATH/$STARTMOD >/dev/null 2>&1 &
		$MODPATH/$STARTMOD >/dev/null 2>&1 &
	fi
done

#
# First of all after modules were started ...
#
MyRouter
Freeme
bin/Runtime > RUNTIME
