#!/bin/sh

EMPIREPORT=1617
EMPIREHOST=hpcvxst
EMPIREBASE=$HOME/games/empire
EMPIREDATA=$HOME/games/EMP
cd $EMPIREBASE/POGO

#  This script kills/restarts empire at scheduled times:
#
#  Every day at noon and midnight: restart emp_login and emp_tm.
#  Early Saturday morning: kill game and backup databases.
#  Saturday noon, Sunday morning, Sunday noon: kill game.
#
#  A log of activities is kept in $EMPIREDATA/data/scheduler.log.

DATE=`date`
DAY=`date +%w`
AMPM=`date +%p`
FNAME=`date +emp.%m%d%y.%H%M`
DOW=`date +%A`

if [ "x$1" != "x" ]; then
    DAY=$1
fi

echo >>$EMPIREDATA/data/login.log "\n*** $DATE ***\n"
echo >>$EMPIREDATA/data/scheduler.log "*** $DATE ***"

case "$DAY" in

  "start" )
    echo "Starting scheduler $DATE" | tee -a $EMPIREDATA/data/scheduler.log
    ;;

  "6" )
    # Saturday, insure game is down, then backup database
    ./killoff		
    echo Killed $DATE >>$EMPIREDATA/data/scheduler.log
    if [ "$AMPM" = "PM" ]; then
        cd $EMPIREDATA/data
        tar cf - * | compress >../$FNAME
    	echo Created backup $FNAME >>$EMPIREDATA/data/scheduler.log
    fi
    if [ -f ./emplock.on ]; then
    	./fireup
	./turnon
	echo Restarted $DATE >>$EMPIREDATA/data/scheduler.log
    fi
    ;;

  "0" )
    # Sunday, insure game is still down
    if [ -f ./emplock.on ]; then
    	./fireup
    	./turnon
    	echo Restarted $DATE >>$EMPIREDATA/data/scheduler.log
    else
    	./killoff		
    	echo Killed $DATE >>$EMPIREDATA/data/scheduler.log
    fi
    ;;

  * )	
    # Weekdays, insure game is up
    if [ -f ./emplock.off ]; then
	./killoff		
	echo Killed $DATE >>$EMPIREDATA/data/scheduler.log
    else
	./fireup
	./turnon
	echo Restarted $DATE >>$EMPIREDATA/data/scheduler.log
    fi
    ;;



esac

if [ "$AMPM" = "PM" ]; then
    # it's now noon
    echo "sh $EMPIREBASE/POGO/scheduler" |  at midnight
else
    # it's now midnight
    echo "sh $EMPIREBASE/POGO/scheduler" |  at noon
fi
