tinymush-3.1p2/game/backups/
tinymush-3.1p2/game/bin/
tinymush-3.1p2/game/data/
tinymush-3.1p2/game/modules/
tinymush-3.1p2/game/modules/old/
tinymush-3.1p2/src/modules/comsys/
tinymush-3.1p2/src/modules/hello/
tinymush-3.1p2/src/modules/mail/
tinymush-3.1p2/src/tools/
#!/bin/sh
#
# $Id: Startmush,v 1.32 2004/10/12 19:42:52 tyrspace Exp $
#
#	Startmush - Control the netmush process.
#
PATH=/usr/ucb:/bin:/usr/bin:.; export PATH
LTDL_LIBRARY_PATH=./modules:./bin:.:../src:../src/modules:../src/.libs:./modules/.libs; export LTDL_LIBRARY_PATH
#

#
#	If we get a 2nd parameter, it's the name of
#	our config file. Else, we use the default one.
#
config=$2
if [ -z "$config" ]; then
	config=mush
fi

#
#	If we don't get any parameters, assume 
#	historical behavior
#
param=$1
if [ -z "$param" ]; then
	param=start
fi

#
#	Read our configuration file.
#
if [ -r "$config.config" ]; then
	. $config.config
	. check_paths
else
	echo "'$config.config' not found."
	exit 1
fi

#
#	Get the current status of the MUSH.
#
if [ -r "$PIDFILE" ]; then
	oldpid=`cat $PIDFILE`
	if [ "$oldpid" -gt 1 ]; then
		nmush=`ps | grep $oldpid | grep netmush | wc -l`
	fi
fi

#
#	Parse our command line.
#
case "$param" in
	stop)
		if [ "$nmush" -gt 0 ]; then
			echo "The MUSH is shutting down."
			kill -s QUIT $oldpid
			wait $oldpid > /dev/null 2>&1
			echo "Shutdown completed."
			exit 0
		else
			echo "The MUSH is not running."
			exit 1
		fi
	;;
	restart)
		if [ "$nmush" -gt 0 ]; then
			echo "The MUSH is restarting."
			kill -s SIGUSR1 $oldpid
			wait $oldpid > /dev/null 2>&1
			echo "Restart completed."
			exit 0
		fi
	;;
	reboot)
		if [ "$nmush" -gt 0 ]; then
			echo "The MUSH is rebooting."
			kill -s QUIT $oldpid
			wait $oldpid> /dev/null 2>&1
		fi
	;;
	start)
		if [ "$nmush" -gt 0 ]; then
			echo "The MUSH already seems to be running."
			exit 1
		fi
	;;
	*)
		echo "Usage: `basename $0` [<start|stop|restart|reboot> [config_file]]"
		echo "Options:"
		echo "  start          Start the server."
		echo "  stop           Stop the server."
		echo "  restart        Restart the server."
		echo "                   Users currently connect should not be"
		echo "                   disconnected by this command."
		echo "  reboot         Do a complete shutdown and restart the server"
		echo "                   This command is the equivalent to doing a"
		echo "                   stop/start of the server."
		echo ""
		echo "Parameters:"
		echo "  config_file    The name of the configuration file without"
		echo "                 the .config suffix. Default is 'mush'. "
		exit 0
	;;
esac


#
#	If we get to this point, the MUSH should currently be
#	down, let's just check one last time to be sure.
#

if [ -r "$PIDFILE" ]; then
	oldpid=`cat $PIDFILE`
	if [ "$oldpid" -gt 1 ]; then
		nmush=`ps | grep $oldpid | grep netmush | wc -l`
		if [ "$nmush" -gt 0 ]; then
			echo "The MUSH already seems to be running."
			exit 1
		fi
	fi
fi

#
#	Make sure the indexes are up-to-date.
#
./Index

echo "Checking for database files and creating backups of old files."

#
#	Refuse to start if the last line of the logfile is a GDBM panic.
#
if [ -r $DATA/$GDBM_DB ]; then
    end="`tail -1 $LOGNAME`"
    if [ "$end" = "GDBM panic: write error" ]; then
	echo "Your log file indicates that the MUSH went down on a GDBM panic"
	echo "while trying to write to the database. This error normally"
	echo "occurs with an out-of-disk-space problem, though it might also"
	echo "be the result of disk-quota-exceeded, or an NFS server issue."
	echo "Please check to make sure that this condition has been fixed,"
	echo "before restarting the MUSH."
	echo
	echo "This error also indicates that the issue prevented the MUSH from"
        echo "writing out the data it was trying to save to disk, which means"
	echo "that you may have suffered from some database corruption."
 	echo "Please type the following now, to ensure database integrity:"
	echo
	echo "    ./Reconstruct"
	echo "    ./Backup"
	echo "    mv -f $LOGNAME $LOGNAME.old"
	echo
	echo "If this is all successful, you may type ./Startmush again to"
	echo "restart the MUSH. If the recovery attempt fails, you will"
	echo "need to restore from a previous backup."
	exit 1
    fi
fi

#
#	Refuse to start if a restart.db is present.
#
if [ -r restart.db ]; then
    echo "There is a restart database, restart.db, present."
    echo "Please delete it before attempting to start the MUSH."
    exit 1
fi

#
#	Handle a KILLED database. If it's complete, we have a flatfile
#	that we can Restore from. Otherwise we should rename it as
#	corrupt and move on.
#	
if [ -r $DATA/$INPUT_DB.KILLED ]; then
	echo
	DBDATE=`date +%m%d-%H%M`
	end="`tail -1 $DATA/$INPUT_DB.KILLED`"
	if [ "$end" = "***END OF DUMP***" ]; then
		NEWFILE=$BACKUP_DIR/$GAMENAME.$DBDATE
		mv $DATA/$INPUT_DB.KILLED $NEWFILE
		echo "A non-corrupt $INPUT_DB.KILLED file was present."
		echo "This means that a flatfile was successfully saved after the reception"
		echo "of a SIGTERM, possibly due to a machine reboot."
		echo "This file has been renamed to $NEWFILE"
		echo "Please type:  ./Restore $NEWFILE"
		echo "If this restoration is successful, you can then type ./Startmush again."
		exit 1
	else
		NEWFILE=$BACKUP_DIR/$GAMENAME.$DBDATE.CORRUPT
		mv $DATA/$INPUT_DB.KILLED $NEWFILE
		echo "A corrupt $INPUT_DB.KILLED file was present."
		echo "This means that there was an unsuccessful attempt to save a flatfile"
		echo "after the reception of a SIGTERM, likely due to a machine reboot."
		echo "This file has been renamed to $NEWFILE"
		echo "You may wish to check for potential problems, and run ./Backup"
		echo "before continuing and typing ./Startmush again."
		exit 1
	fi
fi

#
#	Handle a CRASH database. This is a flatfile created when we 
#	encounter something fatal and attempt to automatically restart.
#	The file might not be current, though, even if it's non-corrupt.
#	If it is corrupt, we've got to cope with that, too.
#
if [ -r $DATA/$CRASH_DB ]; then
	echo
	end="`tail -1 $DATA/$CRASH_DB`"
	if [ "$end" = "***END OF DUMP***" ]; then
		echo "A non-corrupt $CRASH_DB file is present."
		echo "This indicates a flatfile was successfully saved during a panic-restart."
		echo "This file might not be newer your current database."
		echo "Please check the file modification times:  ls -l $DATA"
		echo "If your current database is newer than the crash file, you should:"
		echo "  * Remove the crash file:  rm -f $DATA/$CRASH_DB"
		echo "  * Backup your data:  ./Backup"
		echo "If the crash file is newer, you should:"
		echo "  * Rename the crash file:  mv -f $DATA/$CRASH_DB ./$GAMENAME.flat"
		echo "  * Restore from backup:  ./Restore ./$GAMENAME.flat"
		echo "If this is successful, you can then type ./Startmush again."
		exit 1
	else
		DBDATE=`date +%m%d-%H%M`
		NEWFILE=$BACKUP_DIR/$GAMENAME.$DBDATE.CORRUPT
		mv -f $DATA/$CRASH_DB $NEWFILE 
		echo "A corrupt $CRASH_DB file is present."
		echo "This indicates an unsuccessful flatfile save during a panic-restart."
		echo "This file has been renamed to $NEWFILE"
		echo "You may wish to check for potential problems, and run ./Backup"
		echo "before continuing and typing ./Startmush again."
		exit 1
	fi
fi

for moddb in `ls $DATA/mod_*.db`; do
	echo "Saving old $moddb" | sed -e "s| $DATA/mod_| |; s/\\.db\$/ module db./"
	cp -f $moddb $moddb.old
done

#	Handle Linux sh brokenness; this isn't supposed to be an environment
#	variable, but somehow it stays set.
make_db=""

#	If we have a good checkpoint database, make it the input database.
#	If not, use the backup of the input database.
#
if [ ! -r $DATA/$GDBM_DB ]; then
	echo "No recent db. Will initialize new database."
	make_db="-s"
fi
#
#	Cleanup the old logfiles.
#
./Logclean
#
#	Kick off MUSH
#
touch $LOGNAME
$BIN/netmush $make_db -c $GAMENAME.conf -l $LOGNAME -p $PIDFILE -t $TEXT -b $BIN -d $DATA -g $GDBM_DB -k $CRASH_DB >>$LOGNAME 2>&1 &
echo "Process $!"
$BIN/logwatch -s "Cleanup completed." -l $LOGNAME