#! /bin/sh
#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian GNU/Linux
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
#		Improved killing of sfscd and added way to run sfsagent for some
#		users at boot time -- Marc <marcsoft@merlins.org> 2001/08/01
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/sfscd
NAME=sfscd
DESC="SFS Client software"

test -f $DAEMON || exit 0

set -e

# This is devel code from CVS, we want core files to be generated if necessary
cd /var/lib/sfs/
ulimit -c unlimited

case "$1" in
  start)

        if ! [ "`ps ax |grep [p]ortmap`" ] ; then
	    echo "no portmap running, please start it first"
	    exit 0
	fi
	
        echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON
	echo "$NAME."

	if [ -f /etc/default/sfs-client-run-agent ]; then
	    OLDIFS=$IFS
	    IFS="
"
	    echo -n "Sleeping a few seconds until sfscd is ready before we start any sfsagents"
	    count=0
	    while test ! -f /sfs/.version
	    do
		count=`expr $count + 1`
		if [ $count -gt 30 ]
		then
			echo -n "sfscd not ready, not waiting any longer..."
			break
		fi
	    	echo -n "."
	    	sleep 1
	    done
	    echo

	    for line in `grep -v "^#" /etc/default/sfs-client-run-agent | grep "[a-zA-Z]"`
	    do
		IFS=" "
		set `echo $line`
		echo "Running sfsagent for user $1 with keyfile $2"
		su - $1 -c "sfsagent $2"
	    done
	    IFS=$OLDIFS
	fi
	;;
  stop)
	if [ -d /sfs/.linuxmnt/ ]; then
	   if [ "`ls /sfs/.linuxmnt/ | wc -l`" -gt 0 ]; then
	      set +e
	      cwd=`ls -al /proc/[0-9]*/cwd 2>/dev/null | grep " /sfs/"`
	      fd=`ls -al /proc/[0-9]*/fd/* 2>/dev/null| grep " /sfs/"`
	      set -e
	      if [ z"$fd$cwd" = z ]; then
		echo "Self Certifying paths still mounted but unused. Umount and kill should be clean"
	      else
		echo "Self Certifying paths still mounted"
		echo "Not trying to kill sfscd, as it will only die once no one is using /sfs anymore."
		echo "Do not kill nfsmounter with SIGKILL, you will not be able to recover from that"
		echo -e "Processes having their current directory in /sfs (if any):\n$cwd"
		echo -e "Processes having opened files in /sfs (if any):\n$fd"
		exit 1
	      fi
	   fi	
	else
	   echo "No /sfs/.linuxmnt/ found - making educated guess that";
	   echo "sfs client is not running - exiting...";
	   exit 0
	fi
	#ok, no paths in use and sfscd is propably running, so we try to stop
	echo -n "Sending stop signal to $DESC: "
	start-stop-daemon --oknodo --stop --quiet --pidfile /var/run/$NAME.pid \
	$DAEMON
	echo "$NAME."
	;;	
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo "Reloading $DESC configuration files."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: "
	#start-stop-daemon --stop --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
	$0 stop
	$0 start
	#sleep 1
	#start-stop-daemon --start --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
	echo "$NAME."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
