#!/bin/sh
#
#	Startmush - Kick off the netmush process.
#
PATH=/usr/ucb:/bin:/usr/bin:.; export PATH
#
. mush.config
#
#	Make sure there isn't aready a MUSH running.
#
nmush=`ps ux | egrep netmush | wc -l`
if [ $nmush -gt 1 ]; then
	echo "MUSH already running."
	exit 0
fi
#
#	Make sure the indexes are up-to-date.
#
./mkindx news.txt news.indx
./mkindx help.txt help.indx
./mkindx wizhelp.txt wizhelp.indx
#
echo "Checking for database files and creating backups of old files."
#
#	Check for a panic dump.  If there is one and it is good, copy
#	it on top of the last checkpoint DB written by mush.  If it is bad,
#	just delete it.
#
if [ -r $CRASH_DB ]; then
	end="`tail -1 $CRASH_DB`"
	if [ "$end" = "***END OF DUMP***" ]; then
		mv $CRASH_DB $NEW_DB
	else
		rm $CRASH_DB
		echo "Warning: PANIC dump corrupt using older db."
		echo "Warning: PANIC dump failed on "`date` | mail $OWNER
	fi
fi
#
#	Save a copy of the previous input database and log.
#
if [ -r $INPUT_DB ]; then
	mv -f $INPUT_DB $SAVE_DB
else
	echo "No previous input database."
fi
#
if [ -r $LOGNAME ]; then
	mv -f $LOGNAME $LOGNAME.old
else
	echo "No previous log."
fi
#
#	If we have a good checkpoint database, make it the input database.
#	If not, use the backup of the input database.
#
if [ -r $NEW_DB ]; then
	mv $NEW_DB $INPUT_DB
else
	if [ -r $SAVE_DB ]; then
		echo "No recent checkpoint db. Using older db."
		cp $SAVE_DB $INPUT_DB
	else
		echo "No recent db. Will initialize new database."
		make_db="-s"
	fi
fi
#
#	Kick off MUSH
#
touch $LOGNAME
(nohup ./netmush $make_db $GAMENAME.conf >>$LOGNAME 2>&1 & echo Process $!)
tail -f $LOGNAME | awk '{ print $0 }; /Startup processing complete/ { exit };'
