#!/bin/sh
###############################################################################
## empire-batch -- Runs GEET in batch mode, suitable for use from a cron job
## 
## AFSID           : $__Header$
## Author          : Lynn Slater
## Created On      : Sun Nov 11 12:32:52 1990
## Last Modified By: Lynn Slater x2048
## Last Modified On: Thu Feb 14 19:16:34 1991
## Update Count    : 74
## Status          : GEET General Release 2d Patch 0
## 
## HISTORY
## PURPOSE
##  This script is to run batch empire commands in a low priority while
##  placing a self-limiting kill so prevent runaways. Note that jobs run by
##  cron and sometimes by rsh do not go through the same .login and .cshrc
##  that user shells go through. Thus, to run empire from a cron job
##  requires setting some variables that are otherwise automatically set.
##  This also applies to some rlogin or rsh commands.
##
##  This script takes 5 arguments
##    COMMAND -> The name of the command recognized by emp-batch.el. Look
##      for lines like
##        (setq command-switch-alist (cons (cons "-fire" 'batch-sink-ships)
##   				                 command-switch-alist))
##      The first string is the name recognized on the command line, the
##      second name is the name of the lisp function doing that command.
##    SAVEDIR -> This is the fully qualified name of the dir in which the
##      save file is located.
##    SAVEFILE -> This is the name of the empuire save file.
##    SLEEP -> This is the amount of time to sleep before killing the emacs
##      job. This is to prevent runaways.
##    RUNPRI -> This is the priority of the batch job.
##
##  Examples:
##    empire-batch -fire /home/lrs/emp dworld7 300 14
##    empire-batch -adjust /home/lrs/emp dworld2 600 15
##
##  Before a world is suitable for batch play, the empire-client-command
##  variable must be set. Remember that no environment settings, such as
##  EMPIREHOST, are passed to this job. The client commands I use are like
##  the following:
##    (setq empire-client-command 
##      "(setenv COUNTRY Auto7; setenv PLAYER seven;setenv EMPIREHOST emerald;emp_client)")
##
##  To run this from a cron job, edit the file empire.crontab. The example
##  manages 5 countries, two of which are defensively mobilized; your world
##  will probably be simplier.  Notes:
##    There are no comments in cron tables.
##    Fully qualify the name of the empire-batch command as the PATH is not
##      the same to cronjobs.
##  To enable, run the shell command
##    cron empire.crontab
##
## You will probably need to edit the PATH setting below. Commands needed
## are sh, date, renice, emacs, and whatever the emp_client.
##
## Depending upon your system, you may consider removing the echo's to
## console indicating the job startup and completion.  
###############################################################################

# Noted the time
date

# Cron jobs start with no .login, .cshrc, or any other init. Do it all here.
PATH=.:$HOME/bin:/usr/ucb:/usr/bin:/bin:/etc:/usr/etc:/usr/local/bin:/usr/local/games
export PATH

# Check args
if test $# -ne 5
then cat <<!
usage: $0 command savedir savefile max_run_time runpri
  e.g. $0 -fire /home/lrs/emp dworld7 300 12
!
exit 1
fi

COMMAND=$1
SAVEDIR=$2
SAVEFILE=$3
SLEEP=$4
RUNPRI=$5

if [ ! -d $SAVEDIR ]
 then echo $0: "Save directory does not exist" $SAVEDIR
      exit 1
fi

if [ ! -f $SAVEDIR/$SAVEFILE ]
 then echo $0: "Save file does not exist" $SAVEDIR/$SAVEFILE
      exit 2
fi


# The next line is needed so emacs gets the shell correctly
SHELL=/bin/csh
export SHELL

# Lower the priority
renice $RUNPRI $$

# cd to the place where the dump files live
cd $SAVEDIR

#echo emacs -batch $HOME/.emacs -l emp-batch $COMMAND $SAVEFILE 
emacs -batch -l $HOME/.emacs -l emp-batch $COMMAND $SAVEFILE &
pid=$!

echo emacs pid is $pid
echo $SAVEFILE "  	" $COMMAND "	: Start   " `date "+%m/%d@%T"` ", PID=$$/$pid" >>  $SAVEDIR/emplog
echo " "

# Sleep for awhile before killing the process, if it is still running
#echo sleep $SLEEP
sleep $SLEEP
kill $pid
echo -n "kill of $pid done at "
date
echo $SAVEFILE "  	" $COMMAND "	: Complete" `date "+%m/%d@%T"` ", PID=$$" >>  $SAVEDIR/emplog

