#! /bin/sh
#
#  Copyright (c) 2004, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#
#   * Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#   * Neither the name of SWITCH nor the names of its contributors may be
#     used to endorse or promote products derived from this software without
#     specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
#  $Author: peter $
#
#  $Id: nfsen.rc 34 2005-07-18 12:53:55Z peter $
#
#  $LastChangedRevision: 34 $

# Include NFSEN settings
. /usr/local/etc/nfsen-shell-param

RunUser=`whoami`;

# Make sure all pid files are valid - the processes must exist and run
$BINDIR/pid_check.pl

case "$1" in
  start)
	echo -n "Starting nfcpad: "
	for line in $SOURCES ; do
    	ident=`echo $line | sed -e " s/:.*//"`;
    	port=`echo $line | sed -e " s/.*://"`;
		pidfile="$VARDIR/run/$ident.pid"
		if [ $RunUser = 'root' ]; then
			args="-w -D -I $ident -p $port -u $USER -g $GROUP $BUFFLEN -l $PROFILEDATADIR/live/$ident"
		elif [ $RunUser = $USER ]; then
			args="-w -D -I $ident -p $port -l $PROFILEDATADIR/live/$ident"
		else
			echo "Hello '$RunUser'"
			echo "Only user '$USER' or 'root' wants to run $0 start/stop";
			exit 1;
		fi
        profiler="$PREFIX/nfprofile -q -p $PROFILEDATADIR -s $ident -r %d/%f"
    	echo -n "$ident "
		if [ -f $pidfile ]; then
			echo "Already running! "
		else 
			$PREFIX/nfcapd $args -P $pidfile -x "$profiler"
		fi
	done

	echo "done."
	pidfile="$VARDIR/run/nfsen-run.pid"
	if [ -f $pidfile ]; then
		echo "nfsen-run is already running!"
	else 
		echo -n "Starting nfsen-run: "
		$BINDIR/nfsen-run
		echo "done."
	fi
	;;
  stop)

	if [ $RunUser != 'root' -o $RunUser != $RunUser ]; then
		echo "Hello '$RunUser'"
		echo "Only user '$USER' or 'root' wants to run $0 start/stop";
		exit 1;
	fi

	echo -n "Stopping nfsen-run: "
	pidfile="$VARDIR/run/nfsen-run.pid"
	if [ ! -f $pidfile ]; then
        echo "Missing pid file $pidfile to stop nfsen-run."
	else
		pid=`cat $pidfile`;
		echo -n "nfsen-run:${pid} "
		# if kill returns an error code, it could not signal nfsen-run
		# therefore process may not exists. Clean up old pid file
		kill -TERM $pid || echo Signal error $?

		# wait for nfsen-run to exit
		echo -n "Wait for nfsen-run to exit "
		x=0;     # initialize x to 0
		while [ -f $pidfile -a "$x" -le 120 ]; do
		    # increment the value of x:
		    x=$(expr $x + 1)
		    sleep 1
			echo -n "."
		done 
			
		if [ -f $pidfile ]; then
			echo
			echo "nfsen-run did not exit within 2 minutes - give up waiting"
		fi

	fi
	echo "done."

	echo -n "Stopping nfcpad: "
	for line in $SOURCES ; do
    	ident=`echo $line | sed -e " s/:.*//"`;
    	port=`echo $line | sed -e " s/.*://"`;
		pidfile="$VARDIR/run/$ident.pid"
		if [ ! -f $pidfile ]; then
			echo "Missing pid file $pidfile to stop ident: $ident"
		else
			pid=`cat $pidfile`;
			echo -n "${ident}:${pid} "
			# if kill returns an error code, it could not signal nfcapd
			# therefore process may not exists. Clean up old pid file
			kill -TERM $pid
		fi
	done
	echo "done."
	
	;;
  #reload)
	#
	#	not unsed
	#;;
  #restart|force-reload)
	#
	#	not unsed
	#;;
  *)
	"Usage: $0 { start | stop }"
	exit 1
	;;
esac

exit 0

