#!/bin/sh
#
# EmpireMUD 2.0 autorun script
# Based on CircleMUD 3.0 autorun script
# Contributions by Fred Merkel, Stuart Lamble, and Jeremy Elson
# Copyright (c) 1996 The Trustees of The Johns Hopkins University
# All Rights Reserved
# See license.doc for more information
#
#############################################################################
#
# This script can be used to run EmpireMUD over and over again (i.e., have it
# automatically reboot if it crashes).  It will run the game, and copy some
# of the more useful information from the system logs to the 'log' directory
# for safe keeping.
#
# You can control the operation of this script by creating and deleting files
# in Empire's root directory, either manually or by using the 'shutdown'
# command from within the MUD.
#
# Creating a file called .killscript makes the script terminate (i.e., stop
# rebooting the MUD).  If you want to shut down the MUD and make it stay
# shut down, use the "shutdown die" command from within the MUD.
#
# Finally, if a file called pause exists, the script will not reboot the MUD
# again until pause is removed.  This is useful if you want to turn the MUD
# off for a couple of minutes and then bring it back up without killing the
# script.  Type "shutdown pause" from within the MUD to activate this feature.
#

# The port on which to run the MUD
PORT=4000

# Default flags to pass to the MUD server (see admin.txt for a description
# of all flags).
FLAGS='-q'

# The directory where core files are located (EmpireMUD will keep the oldest
# core and newest 4 cores, but delete the rest to avoid filling the entire
# drive if it crashes repeatedly).
core_directory="lib"

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

while ( : ) do

  DATE=`date`
  echo "autorun starting game $DATE" >> syslog
  echo "running bin/empire $FLAGS $PORT" >> syslog

  bin/empire $FLAGS $PORT >> syslog 2>&1

  # after shutdown/crash:
  # check for multiple core files and delete all but the oldest 1 and newest 4
  if [ -n "$core_directory" ] && ls $core_directory/core* > /dev/null 2>&1; then
      # List core files, sort them by modification time (oldest to newest), and get the filenames
      core_files=$(ls -t $core_directory/core*)

      # Keep the oldest core file
      oldest_core=$(echo "$core_files" | tail -n 1)

      # Keep the 4 newest core files
      newest_cores=$(echo "$core_files" | head -n 4)

      # Remove any other core files
      for file in $core_files; do
          if [ ! "$(echo "$file" | grep -Fx "$oldest_core")" ] && [ ! "$(echo "$newest_cores" | grep -Ex "$file")" ]; then
              rm "$file"
              echo "Core cleanup deleted file: $file" >> syslog
          fi
      done

      if [ -n "$oldest_core" ]; then
        echo "Oldest core file: $oldest_core" >> syslog
      fi
      if [ -n "${newest_cores[0]}" ]; then
        echo "Newest core file: $(echo "$newest_cores" | head -n 1)" >> syslog
      fi
  fi

  # preserve the end of the syslog
  tail -30 syslog > syslog.CRASH

  # NOTE: If you update this list, also update comm.c
  fgrep "ABUSE:" syslog >> log/abuse
  fgrep "BAN:" syslog >> log/ban
  fgrep "CONFIG:" syslog >> log/config
  fgrep "GC:" syslog >> log/godcmds
  fgrep "VALID:" syslog >> log/validation
  fgrep "DEATH:" syslog >> log/rip
  fgrep "EVENT:" syslog >> log/event
  fgrep "BAD PW:" syslog >> log/badpw
  fgrep "DEL:" syslog >> log/delete
  fgrep "NEW:" syslog >> log/newplayers
  fgrep "SYSERR:" syslog >> log/syserr
  fgrep "LVL:" syslog >> log/levels
  fgrep "OLC:" syslog >> log/olc
  fgrep "WAR:" syslog >> log/war
  fgrep "SCRIPT ERR:" syslog >> log/scripterr
  fgrep "EMPIRE:" syslog >> log/empires
  rm log/syslog.old
  mv syslog log/syslog.old
  touch syslog

  # check for .killscript file and exist if found
  if [ -r .killscript ]; then
    DATE=`date`;
    echo "autoscript killed $DATE"  >> syslog
    rm .killscript
    exit
  fi

  sleep 1

  while [ -r pause ]; do
    sleep 1
  done

done
