#!/bin/sh
#
# initscript for lambdamoo, by Jonathan Walther <krooger@debian.org>
#
# Edit /etc/moos.conf to change lambdamoo's configuration.

test -f /etc/lambdamoo.conf || exit 0

# xxx Read config file.
# xxx This is WRONG
# xxx in the next version, will have to parse this file and do some
# xxx other funky stuff.
. /etc/lambdamoo.conf

# Sanity check on config file.
if [ "$PORT" = "" ] || [ "$LOGFILE" = "" ] || [ "$TIMEOUT" = "" ]; then
	echo "LambdaMoo: bad /etc/lambdamoo.conf !" ;
	exit
fi

# See how we were called.
case "$1" in
  start)
        # Check what the file /var/lib/lambdamoo/moo.db points to.
        DBFILE=`update-alternatives --display moo.db | \
                grep "currently points to" | \
                cut -d " " -f 6`
        test -f "$DBFILE" || exit 0

	# Clean up old databases.
	cd `dirname $DBFILE`
	if [ ! -r "$DBFILE" ] ; then
		echo "Bad database: $DBFILE"
		exit 1
	fi
	if [ -r "$DBFILE.new" ] ; then
		mv $DBFILE $DBFILE.old
		mv $DBFILE.new $DBFILE
		rm -f $DBFILE.old.gz
		gzip $DBFILE.old &
	fi

	echo -n "Starting lambdamoo servers: "

	# Important to do this so we don't get busy mount points or other
	# problems.
	cd /

	# Check if there is a process to match what's in the pid file,
        # by sending signal 0, which has no effect. This also checks to see
        # if there is a pid file at all, btw.
        if start-stop-daemon --quiet --stop --signal 0 --user daemon \
		--pidfile /var/run/lambdamoo.pid --name lambdamoo 2>/dev/null
	then
		echo " already running."
		exit
	fi

	touch $LOGFILE
	chown daemon.daemon $LOGFILE

	su daemon -c "\
		/sbin/start-stop-daemon --start --quiet --user daemon \
			--exec /usr/sbin/lambdamoo \
			--startas /usr/sbin/lambdamoo -- \
			-l $LOGFILE $DBFILE $DBFILE.new $PORT" \
			>/dev/null 2>/dev/null </dev/null &
	echo $! > /var/run/lambdamoo.pid
	echo "$DBFILE" > /var/run/lambdamoo.db
	echo "."
	;;
  stop)
	echo -n "Stopping LambdaMoo servers: "
	# Check if there is a process to match what's in the pid file,
	# by sending signal 0, which has no effect. This also checks to see
	# if there is a pid file at all, btw.
	if start-stop-daemon --quiet --stop --signal 0 --user daemon \
		--pidfile /var/run/lambdamoo.pid --name lambdamoo 2>/dev/null
	then
		# Load up the filename of the db file that was last used.
		DBFILE=`cat /var/run/lambdamoo.db` 2>/dev/null

		# Signal 2 means dump db and die.
		start-stop-daemon --quiet --stop --signal 2 \
			--user daemon --name lambdamoo \
			--pidfile /var/run/lambdamoo.pid 

		# Wait until the timeout for the server to die.
		count=$TIMEOUT
		newfile=no
		fivecount=0
		pid=`cat /var/run/lambdamoo.pid`
		while ([ $count != 0 ]) do
			let count=$count-1
			let fivecount=fivecount+1
			if kill -0 "$pid" 2>/dev/null ; then
				sleep 1
				# Every five seconds, check so see if it
				# has started saving db, if not, resend the
				# signal 2.
				if [ $fivecount = 5 -a "$newfile" != "yes" ] 
				then 
					fivecount=0
					if [ -z "`ls $DBFILE.new.\#*\# 2>/dev/null`" ]
					then
						start-stop-daemon --quiet --stop --signal 2 \
							--user daemon\
							--pidfile /var/run/lambdamoo.pid \
							--name lambdamoo 2>/dev/null
						echo -n retry
						let count=$count-4
					else
						newfile=yes
					fi
				fi
			else
				count=0
			fi
			echo -n .
		done

		# If it's not dead yet, kill it.
		if  kill -0 "$pid" 2>/dev/null ; then
			echo " TIMEOUT!"
			start-stop-daemon --quiet --stop \
				-exec /usr/sbin/lambdamoo --user daemon \
				--pidfile /var/run/lambdamoo.pid \
				--name lambdamoo 2>/dev/null
		else
			echo "done."
		fi
	else
		echo " not running.";
	fi

	rm -f /var/run/lambdamoo.pid /var/run/lambdamoo.db

	;;
  force-reload|restart)
	# There doesn't appear to be a better way (like a signal we could send.)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: /etc/init.d/lambdamoo {start|stop|restart|force-reload}"
	exit 1
esac

exit 0
