#!/usr/bin/ksh       
# -*-ksh-*-

# Written by Mike Campbell (mcampbel@schubert.sbi.com).  The guts took me all
# of 20 minutes to write, so I'm releasing into the public domain.  Modify,
# spindle, fold, mutilate at will.  If you DO use this, all I ask is that you
# send me an email telling me.  (No, I don't keep track of who is using it; I
# just would like to say "hi" and thanks.)  If you add something to it, let me
# know!  I probably will find it useful too.  Thanks.

# Cat a uuencoded .trn file into this script and it will check the .trn using
# the phost -c option, and spit all of the output, good or bad, to the caller.
# It is then up to the caller to mail the output back to the sender.  This is
# done easily enough via procmail.  

# Preconditions:
# The environment vars HOST_DIR GAME_DIR and BIN_DIR are defined upon running
# this script.  HOST_DIR is where all the host file are kept,
# (i.e. beamspec.dat, engspec.dat, hullspec.dat, pconfig.src, plang.hst,
# torpspec.dat, truehull.dat, xyplan.dat).  GAME_DIR are where all the
# game-specific files are kept, and BIN_DIR is where the phost binary is kept.

# You can set them up in this script, (see the first set of code lines) or set
# them up prior to running.  Using the latter method, you can check any number
# of simultaneous games by simply resetting the vars before you run this.  The
# proto-typical case is setting them up in your .procmailrc if using procmail.

# I purposely exit 0 from a lot of places in this script.  I don't exit with an
# error (non-0 value) even when there is an error condition because I call this
# from procmail.  Procmail considers a non-0 exit an error (which it should),
# and considers the mail unprocessed, so it goes to the next processing rule.
# I want it to consider EVERYTHING processed, even errors, so I have this
# script spit out errors to stdout as well as sucess messages.  That way,
# procmail can capture whatever it gets, and send back to the sender.  I never
# see it.  This may not be what you want to do.  It works for me.  YMMV.

# Any errors leave around the temp file directory with the message in it.  (It
# gets cleaned up in the last line, so a premature non-0 exit leaves it there.)
# 

# I use this running ksh.  I've put in lines in comments showing how to do the
# same things using /bin/sh.  DO NOT USE csh!  Ever!  For anything!  (Why?
# Send me an email with subject 'send me csh whynot').

# Set up safe defaults (test game)  Override in .procmailrc.
test -z "$HOST_DIR" && HOST_DIR=/local/ftp/planets/portable-host/TEST-GAME
test -z "$GAME_DIR" && GAME_DIR=$HOST_DIR/game-dir
test -z "$BIN_DIR" && BIN_DIR=/local/ftp/planets/portable-host/bin

# Get to where we are going
cd $GAME_DIR
if test "$?" -ne 0; then
  echo Could not cd to $GAME_DIR.  Exiting.
  exit 0;
fi 

# Make a tmp dir
mkdir ./tmpdir.$$
if test "$?" -ne 0; then
  echo Could not make a tmp dir.  Exiting.
  exit 0;
fi 

# cat the thing.
cd tmpdir.$$
TMPDIR=`pwd`
cat > msg

# Attempt to uudecode
uudecode msg
if test "$?" -ne 0; then
  echo Got the turn, but could not uudecode it!  Please resend.
  exit 0;
fi

# Is it a .trn file?
turns=0
# for i in `/bin/ls *.trn *.TRN 2>/dev/null`; do  # for /bin/sh
for i in $(/bin/ls *.trn *.TRN 2>/dev/null); do
  num=`echo $i | /usr/bin/cut -f1 -d"." | tr PLAYER player | sed -e 's/player//'`
  if test "$num" -lt 1 -o "$num" -gt 11; then
    echo $i not in form playerX.trn, where X between 1 and 11.  Exiting.
    exit 0;
  fi
  turns=$(($turns + 1))  # turns=`expr $turns + 1` for /bin/sh
done

# was there only one?
if test "$turns" -gt 1; then
  echo There is more than one .trn that has been uudecoded.  Exiting.
  exit 0;
fi

# Canonicalize the name, and copy to game dir.

# Use the following if you don't have the perl "rename" script.  (Very handy,
# by the way; It does TONS more than just this!  Ask me for it if you want it.)

# for i in *.[Tt][Rr][Nn]; do 
#   j=$(echo $i | tr [A-Z] [a-z])  #   j=`echo $i | tr [A-Z] [a-z]` # for /bin/sh
#   if [[ "$i" != "$j" ]]; then    #   if test "$i" != "$j" ; then # for /bin/sh
#     mv $i $j
#   fi
# done

/optware/unsupported/bin/rename 'tr/A-Z/a-z/g;' *.[Tt][Rr][Nn] 2>/dev/null
cp player*.trn $GAME_DIR

# Check it using host.
$BIN_DIR/phost -c $num $GAME_DIR $HOST_DIR 2>&1
rc=$?
if test "$rc" -ne 0; then
  exit 0;
  rm $GAME_DIR/player$num.trn
  echo 
  echo Your turn had problems!  It will not be run.  pid was $$.
fi

# Should be ok from here.

rm -rf $TMPDIR
