#!/bin/csh -f
#
# This is the DuneMUSH version of the PennMUSH restart script
# It's in csh, not sh, so we can use the unlimit command.
#
# usage: restart [auto [kill]]
# If auto is specified, this is an autorestart by email.
# If auto and kill are specified, send the MUSH a kill -9
# before restarting!
#

# Unlimit everything - we want complete core dumps.
unlimit
set nonomatch

#-- options

# If you are allowing automatic restarts, and want an email message
# sent to a mailing list when the MUSH is autorestarted (to prevent
# multiple attempts to autorestart), set this variable to the name
# of the address of the mailing list to mail:
#set MAILLIST=mush-admin@mush.com

# You will almost definitely want to change this. In general, it
#  should be the directory this script is in. Provide a full pathname.
set GAMEDIR = /dunemush/mush/game

# The config file
set CONF_FILE = dune.conf

# This is where the general error messages are placed.
set LOG = log/netmush.log

#-- database files

# These names must match those in the conf file.

# You probably want to change this. Usually for a running MUSH, it's indb.Z 
set INDB = indb

# This is where the database is dumped to when the game is active.
set OUTDB = outdb

# This is the directory and file where panic (crash) databases are placed.
set PANICDIR = data
set PANICDB =  PANIC.db

#-- start up everything

set mush = `/bin/ps ux | /bin/grep conf | /usr/ucb/wc -l`

# Uncomment the following only if you are RUNNING an RWHO server and want to 
# restart it as well
#set mwhod = `/bin/ps ux | /bin/grep mwhod | /usr/ucb/wc -l`

cd $GAMEDIR

#define Uncomment to following only to restart the RWHO server
#if ($mwhod == 1) then
#  echo restarting mud who daemon
#  (./mwhod -f muds.dat -n FooWHO >& mwhod.log &)
#endif

# If there's a lock, don't do autorestarts
if ($1 == "auto") then
  if (-r restart.lock) then
     echo "Failed autorestart (locked) `date`" >> restarts.log
     exit 0
  endif
endif

if ($mush > 1) then
  if ($2 == "kill") then
    # Kill the MUSH!
    /bin/kill -9 $mush
    echo "Mush auto-killed `date`" >> restarts.log
  else
    echo Mush already active.
    echo "Failed restart (active) `date`" >> restarts.log 
    exit 0
  endif
endif

# Other handy stuff, if you have the 'mugshot' script for extracting
# suspect stuff from the logs:
#mugshot log/command.log
#foreach file (SUSP*)
  #cat $file >> log/old/$file
  #/bin/rm -f $file
#end
#echo Mugshots taken!

echo Building text file indexes.
index txt/help
index txt/news
index txt/events

# If you use the txtindex script to make txt/index.txt for use
# with the index command:
#echo Building files for the index command
#txtindex txt/keywords txt/news.txt txt/events.txt > txt/index.txt
#index txt/index

echo Restarting Mush.

# Is there a panic db? If so, recover it, saving the current outdb
if (-r $PANICDIR/$PANICDB) then 
	set end = "`tail -1 $PANICDIR/$PANICDB`"
	if ("$end" == "***END OF DUMP***" ) then
		mv data/$OUTDB save/$OUTDB.old
		mv $PANICDIR/$PANICDB data/$OUTDB
                echo "PANIC dump successfully recovered."
	else
		rm $PANICDIR/$PANICDB
		echo "Warning: PANIC dump corrupt. Using older db."
	endif
endif

# Move netmush.log to netmush.old, but skip SUSPECT stuff
grep -v SUSPECT log/netmush.log > log/netmush.old

# Take everything from netmush.old except the connection stuff
# and append it to netmush.err
grep -v NET log/netmush.old >> log/netmush.err

# Now we can remove all the .log files.
rm -f log/*.log

# Is there an outdb? 
# If so: mv save/indb.old to save/indb.old.old
#	 mv data/indb to save/indb.old
#	 mv data/outdb to data/indb
#   This preserves the 2 previous databases, just in case
# If not: mention if save/indb.old or save/indb.old.old exist,
#         in case we want them instead of data/indb
if (-r data/$OUTDB) then
	mv save/$INDB.old save/$INDB.old.old
	mv data/$INDB save/$INDB.old
	mv data/$OUTDB data/$INDB
	echo "Restarted from $OUTDB"
else
        echo "No recent database found. Using $INDB."
	if (-r save/$INDB.old) then
		echo "Other db's available: save/$INDB.old"
	endif
	if (-r save/$INDB.old.old) then
		echo "Other db's available: save/$INDB.old.old"
	endif
endif

# Run the MUSH itself!
(unlimit; ./netmush $CONF_FILE >& $LOG) &

# Handle autorestart stuff, logging the date in restarts.log,
# and sending email to the admin mailing list.
#
if ($1 == "auto") then
  echo "`date` (email)" >> restarts.log
  if ($?MAILLIST) then
  if ($2 == "kill") then
    echo "Remote MUSH kill and restart at `date` in progress" | \
    mail -s"MUSH Kill/Restart (auto-message)" $MAILLIST
  else
    echo "Remote MUSH restart at `date` in progress" | \
    mail -s"MUSH Restart (auto-message)" $MAILLIST
  endif
  endif
else
  date >> restarts.log
endif
