/
ScryMUD/mud/
ScryMUD/mud/grrmud/Boards/
ScryMUD/mud/grrmud/Help/
ScryMUD/mud/grrmud/Pfiles/
ScryMUD/mud/grrmud/PlayerSacks/
ScryMUD/mud/grrmud/PlayerShops/
ScryMUD/mud/grrmud/help_filter/
ScryMUD/mud/hegemon/
ScryMUD/mud/hegemon/data/
ScryMUD/mud/hegemon/data/help/battle/
ScryMUD/mud/hegemon/data/help/client/
ScryMUD/mud/hegemon/data/help/communications/
ScryMUD/mud/hegemon/data/help/skills/
ScryMUD/mud/hegemon/data/help/spells/
ScryMUD/mud/include/
ScryMUD/mud/lib/
ScryMUD/mud/lib/bitfield/
ScryMUD/mud/lib/log/
ScryMUD/mud/lib/string2/
#!/bin/bash

########################################################################

if [ ! -d ./log ]; then
    mkdir ./log
fi

#  Make sure this directory exists...
if [ ! -d ./PlayerShops ]; then
    mkdir ./PlayerShops
fi

while ( : ) do

    #  Don't let us use more than 20 MEG, if we do, it's probably a
    #  memory leak.  If the database ever gets huge, this will need to
    #  be raised.
    ulimit -d 20000

    echo "Starting ScryMUD at: `date`"

    # Just in case.
    chmod u+x ./gmud
    
    if [ -x ./gmud ]; then
       ./gmud >& gmud.log
    else
	exit 2
    fi
    
    RETVAL=$?

    echo "ScryMUD stopped at: `date`"
    sleep 1  # Make infinite loop not so bad.

    date >> ./gmud.log
    echo "************END OF CRASH FILE**************" >> ./gmud.log
    cat ./gmud.log >> ./GMUD.LOG

    tail -500 ./GMUD.LOG > /tmp/gmud.log
    mv /tmp/gmud.log ./GMUD.log

    # Keep a history of the log files that might have been created.
    if [ -f ./log/logfile.txt2 ]; then
	mv -f ./log/logfile.txt2 ./log/logfile.txt3
    fi

    if [ -f ./log/logfile.txt1 ]; then
	mv -f ./log/logfile.txt1 ./log/logfile.txt2
    fi

    if [ -f ./log/logfile.txt ]; then
	mv -f ./log/logfile.txt ./log/logfile.txt1
    fi

    # And 3 object pointer logs.
    if [ -f ./log/obj_ptr.log2 ]; then
	mv -f ./log/obj_ptr.log2 ./log/obj_ptr.log3
    fi

    if [ -f ./log/obj_ptr.log1 ]; then
	mv -f ./log/obj_ptr.log1 ./log/obj_ptr.log2
    fi

    if [ -f ./log/obj_ptr.log ]; then
	mv -f ./log/obj_ptr.log ./log/obj_ptr.log1
    fi

    # Core's take up too much space, just going to grap the useful info
    # with gdb.
    if [ -f ./gdb.out4 ]; then
	mv -f ./gdb.out4 ./gdb.out5
    fi

    if [ -f ./gdb.out3 ]; then
	mv -f ./gdb.out3 ./gdb.out4
    fi

    if [ -f ./gdb.out2 ]; then
	mv -f ./gdb.out2 ./gdb.out3
    fi

    if [ -f ./gdb.out1 ]; then
	mv -f ./gdb.out1 ./gdb.out2
    fi

    # Depending on the shell..one of these may exist.

    if [ -f ./core ]; then
	echo bt > /tmp/gdb_input
	echo quit >> /tmp/gdb_input
	gdb gmud ./core < /tmp/gdb_input > gdb.out1
    fi

    if [ -f ./gmud.core ]; then
	echo bt > /tmp/gdb_input
	echo quit >> /tmp/gdb_input
	gdb gmud ./gmud.core < /tmp/gdb_input > gdb.out1
    fi

    echo "Checking RETVAL:  ${RETVAL}"
    if [ $RETVAL = 2 ]; then
	echo "Checking for new world"
	if [ -f ./World/wrld.new.tar.gz ]; then
	    echo "Copying over new world database."
	    cd ./World
	    #zcat wrld.new.tar.gz | tar -xvf -
	    # Some platforms have screwed up zcat's it seems. --BEN
	    rm -f ./wrld.new.tar
	    gunzip ./wrld.new.tar.gz
	    tar -xf ./wrld.new.tar
	    cd ..
	fi

	echo "Checking for new binary."
	if [ -f ./gmud.new ]; then
	    echo "Copying over new server (gmud) binary."
	    mv ./gmud.new ./gmud
	fi
    fi

#    echo "Checking for fastboot."

#    if [ ! -r .fastboot ]; then
#	sleep 60
#    else
#	rm .fastboot
#	sleep 5
#    fi

    # Might want to change this to send email to another address.
    if [ ! -r ./.reboot ]; then	#reboot is explicit only
	mail ${USER}@localhost < shutdown_msg
	exit
    fi

done