#! /bin/sh
#
# Restart LPMud a lot of times, in case of a crash.
#
NICE_VAL="-19"
MUDHOME=/sherlock/u1/home/sherlock/curmud
MUDUSER=sherlock
PROCIDFILE=$MUDHOME/.procs
#
# Let's do everything from the mud directory
#
cd $MUDHOME
#
case `whoami` in
  $MUDUSER ) : ;;
       * ) echo 'permission denied' >&2 
           exit 0 ;;
esac

umask 007

while [ $# -gt 0 ]; do
    case $1 in
	-k ) KILL_MUD=1 ;;
	-n ) shift ; NICE_VAL="$1" ;;
	[1-9][0-9]* ) NUM_TO_RESET=$1 ;;
	* ) echo $1 ": unknown switch to" $0 ; exit 1;;
    esac
    shift
done

#
# If a number is not given on the command line, set a default
#
if [ "${NUM_TO_RESET}" -eq "" ]; then
    NUM_TO_RESET=100
fi

# let's do it!
if [ "${KILL_MUD}" -ne "1" ]; then
    echo $$ > $PROCIDFILE
    (/bin/nice $NICE_VAL ./make_list $PROCIDFILE ) &
    echo "Restart ${i}." >> mudlib/lpmud.log
    date >> mudlib/lpmud.log
    echo "The game has been up since" > mudlib/UPSINCE
    date >> mudlib/UPSINCE
    /bin/nice $NICE_VAL ./parse >> mudlib/lpmud.log 2>&1
    tail -20 mudlib/debug.log >> mudlib/lpmud.log
fi
#
# This is the Killing section
#
for i in `cat $PROCIDFILE ` ;  do
    if [ $i != $$ ] ; then
	kill -9 $i
	if [ $? != 0 ]; then
	    echo "Process ID $i killed"
	fi
    fi
done
if [ "${KILL_MUD}" -eq "1" ]; then
    echo "Mud taken down"
    exit 0
fi
exec $0 -n $NICE_VAL `expr $NUM_TO_RESET - 1`