#!/bin/csh -f

# Set the port number.
set port = 9000
if ( "$1" != "" ) set port="$1"

# Change to area directory.
cd ../area

# Set limits.
nohup
nice
limit stack 1024k
ulimit -Sc unlimited
if ( -e shutdown.txt ) rm -f shutdown.txt

while ( 1 )
    # If you want to have logs in a different directory,
    #   change the 'set logfile' line to reflect the directory name.

    cp ../log/start2.log ../log/start2.bak
    cp ../system/hiscores.dat ../system/hiscores.bak

	set logfile = ../log/start2.log

    # Record starting time
    date > $logfile
    date > ../area/boot.txt

    # Record initial charges
    # charges >> $logfile

    # Run SMAUG.
    # NO! Run DBSaga!
    ../src/dbsaga 9001 >&! $logfile
    
#    ../src/smaug 7654 >&! $logfile
#    ../src/smaug $port >&! $logfile
#    ../src/smaug >>&! $logfile

    # Record ending charges
    # charges >> $logfile

#    # Delete this out if no adb.
#    if ( -e core ) then
#	    echo '$c' | adb ../src/smaug
#    endif

    # Let's see if this works...
    # Using old index value, would be nice to have
    # it be the date for the file name
    if ( -e core ) then
      set index = 1000
      while ( 1 )
        set crashfile = ../crash/$index.crash
	    if ( ! -e $crashfile ) break
	    @ index++
      end
      \mv core ../src
      cd ../src
      date > $crashfile
      gdb -batch -x commands dbsaga core >> $crashfile
      rm -f core
      cd ../area
    endif

    # Restart, giving old connections a chance to die.
    if ( -e shutdown.txt ) then
	rm -f shutdown.txt
	exit 0
    endif
    sleep 5
end

