#!/sbin/sh

#
# Start/stop T.Rex daemons
#

PATH=/sbin:/usr/sbin:/usr/bin
export PATH

rval=0
set_return () {
	x=$?
	if [ $x -ne 0 ] ; then
		echo "FAILURE CODE: $x"
   		rval=1
	fi
}

killproc() {            # kill the named process(es)
	echo stopping $1
	findproc $1
	if [ "$1" = "webgate" ]; then
        	[ "$pid" != "" ] && kill -USR1 $pid
	else
        	[ "$pid" != "" ] && kill $pid
	fi
}

findproc() {            # return pid of the named process(es)
	if [ "$1" = "webgate" ]; then
		pid="`cat /etc/firewall/webgate.pid 2>/dev/null`"
		if [ ! -z "$pid" ]; then
			if [ -z "`ps -p $pid | grep webgate`" ]; then
				pid=""
			fi
		fi
	else
        	pid=`/usr/bin/ps -e |
			/usr/bin/grep -w "$1" |
			/usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	fi
}

case $1 in
start_msg)
	echo "Start T.Rex daemons"
	;;

stop_msg)
	echo "Stop T.Rex daemons"
	;;

start)

echo	"    starting T.Rex daemons"	>&2
echo	"    "	>&2

#
# Start fwmon
#

findproc fwmon
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/fwmon ] ; then
        /usr/local/etc/fwmon && echo "    starting fwmon" && echo "\t/usr/local/etc/fwmon" 
        if [ $? -ne 0 ] ; then
	    echo	"Error:  fwmon NOT started"	>&2
	    exit 1
        fi
    fi
else 
        echo "    starting fwmon"
    	echo "\tfwmon already started, using pid: $pid"
fi

#
# Start smwrapd
#

findproc smwrapd
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/smwrapd ] ; then
        /usr/local/etc/smwrapd && echo "    starting smwrapd" && echo "\t/usr/local/etc/smwrapd" 
        if [ $? -ne 0 ] ; then
	    echo	"Error:  smwrapd NOT started"	>&2
	    exit 1
        fi
    fi
else 
        echo "    starting smwrapd"
    	echo "\tsmwrapd already started, using pid: $pid"
fi

#
# Start fwpulsed
#

findproc fwpulsed
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/fwpulsed ] ; then
        grep -v "^[ 	]*#" /etc/firewall/fwpulse.conf >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            /usr/local/etc/fwpulsed && echo "    starting fwpulsed" && echo "\t/usr/local/etc/fwpulsed" 
            if [ $? -ne 0 ] ; then
	        echo	"Error:  fwpulsed NOT started"	>&2
	        exit 1
            fi
        fi
    fi
else 
        echo "    starting fwpulsed"
    	echo "\tfwpulsed already started, using pid: $pid"
fi

#
# Start fwpulse
#

findproc fwpulse
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/fwpulse ] ; then
        grep -v "^[ 	]*#" /etc/firewall/fwpulse.conf >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            /usr/local/etc/fwpulse && echo "    starting fwpulse" && echo "\t/usr/local/etc/fwpulse" 
            if [ $? -ne 0 ] ; then
	        echo	"Error:  fwpulse NOT started"	>&2
	        exit 1
            fi
        fi
    fi
else 
        echo "    starting fwpulse"
    	echo "\tfwpulse already started, using pid: $pid"
fi

#
# Start rpcproxy
#

findproc rpcproxy
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/rpcproxy ] ; then
        grep "^[ 	]*permit[ 	]" /etc/firewall/rpcproxy.conf >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            /usr/local/etc/rpcproxy && echo "    starting rpcproxy" && echo "\t/usr/local/etc/rpcproxy" 
            if [ $? -ne 0 ] ; then
	        echo	"Error:  rpcproxy NOT started"	>&2
	        exit 1
            fi
        fi
    fi
else 
        echo "    starting rpcproxy"
    	echo "\trpcproxy already started, using pid: $pid"
fi

#
# Start webgate
#

findproc webgate
if [ "$pid" = "" ]; then 
    if [ -f /usr/local/etc/webgate ] ; then
        grep "^[ 	]*permit[ 	]" /etc/firewall/webgate.conf >/dev/null 2>&1
        if [ $? -eq 0 ]; then
            /usr/local/etc/webgate && echo "    starting webgate" && echo "\t/usr/local/etc/webgate" 
            if [ $? -ne 0 ] ; then
	        echo	"Error:  webgate NOT started"	>&2
	        exit 1
            fi
        fi
    fi
else 
        echo "    starting webgate"
    	echo "\twebgate already started, using pid: $pid"
fi

	;;

stop)

	#
	# Determine PID of process(es) to stop
	#

	for i in rpcproxy fwpulse smwrapd fwmon spoofmon tcpdump webgate
	do
	    killproc $i
	done

	;;

*)
	echo "usage: $0 {start|stop}"
	;;
esac

exit $rval
