#!/bin/csh -f
#  $Header: /plaid/homes/zeus/Blind-2.45/scripts/RCS/Pre-proc,v 2.45 1997/01/01 00:47:35 zeus Exp zeus $
#
#  $Log: Pre-proc,v $
# Revision 2.45  1997/01/01  00:47:35  zeus
# *** empty log message ***
#
# Revision 2.45  1997/01/01  00:44:54  zeus
# *** empty log message ***
#
#
# A Blind Galaxy turn tester  (should work /w Std galaxy 2.x code too)
# Written by Howard Bampton, bampton@cs.utk.edu
# works with either mbox style mail files (all orders concatinated) or
# separate files for each player's orders.
# (Does it work all merged any more?)
# Notes: Does not work with a copy of blindrc for speed reasons, 
# your version of blind.exe (or blind.dbg, or whatever) should NOT call 
# the savegame() function. Otherwise you have to copy blindrc for every 
# set of orders

# vars to change
set gameaddr = "zeus@cs.utk.edu" # Send orders here
set gmaddr = "bampton@cs.utk.edu" # send GM q's here.
# Send major problem notices here
set gmaddrfatal = "bampton@cs.utk.edu"

set testd = /tmp         # test directory
set pre = $$.pre-proc    # preprocessor output
set post = $$.post-proc  # galaxy errors
set errors = $$.errors   # preprocessor errors
set reject = $$.reject   # ack message
set FILESIZE = 500k

set TOOMANYBAD = 120


if (-e ./.Preproc.config) then
  source ./.Preproc.config
else
  echo "No config file, exiting"
  exit 1
endif


# Ensure we clean up afterwards...
onintr done

set LOCKFILE = $testd/PP.LOCK.$game
lock-wait:
if ( -e $LOCKFILE ) then
  echo "Lock file exists; sleeping"
  sleep 30
  goto lock-wait
endif

touch $LOCKFILE
set runflag = e

set prog = $0
set prog = $prog:t

if ($#argv == 0 ) then
  echo Usage: $prog "-check [-retry] [-all] | -test [-retry] [-all] | -mail [-slient]" 
  exit 1
endif

nextarg:
  switch ($1)
  case '-check':
        set check
        shift
        goto nextarg
        breaksw
  case '-test':
        set test
        set runflag = f
        shift
        goto nextarg
        breaksw
  case '-mail':
        set mail
        shift
        goto nextarg
        breaksw
  case '-retry':
        set retry
        shift
        goto nextarg
        breaksw
  case '-silent':
        set silent
        shift
        goto nextarg
        breaksw
  case '-all':
        set all
        shift
        goto nextarg
        breaksw
  default:
        breaksw
  endsw


# var init's
if ( ! $?mailprog) then
  set mailprog = /usr/ucb/mail
endif
set x = 0
set barfed
set startd = $cwd
if !( $testd =~ $execd ) then
  cp $execd/$data $testd
  rm -f $testd/$exec
  ln -s  $execd/$exec $testd/$exec
endif
cd $testd
touch $testd/$reject

#main loop
while ($x <= 99)
  if ( -e $startd/$x ) then
    unset barfed # no errors, yet....
    echo -n "Preprocessor Working on $x ... "
    # feed orders to preproc script, stdout to $pre, stderr to $errors
    # This is totally redundant with the 2.40+ paradigm
    ($preproc $startd/$x >! $testd/$pre) >& $testd/$errors
    set order_barf = $status
    rm $testd/$reject
    touch $testd/$reject
    if ($order_barf == 100) goto barf1  # bad orders- missing headers
    if (`cat  $testd/$pre | wc -l` == 1) goto barf2 # no header?
    if ($order_barf) goto barf1a

    # try calling galaxy with a COPY of blindrc, this player's orders
    # do it in a test directory (depending upon configs)
  orders_resume:
    #cp $execd/blindrc $testd # uncomment if galaxy writes data on exit
    rm -f $testd/forecast.F # nuke forecast file
    # Run program with limit of FILESIZE and cmd line args e q
    # Drop output where desired.
    (run $testd/$exec ${FILESIZE} $testd/$pre $runflag q >! $testd/$post) >&! $testd/${errors}.dbg
    set count = $status
    #less $testd/$post   # debugging code, look at galaxy pgm output
    if ( $count == 127 ) goto barf1
    if ( ( $count > $TOOMANYBAD ) && ( $count < 127 ) ) goto barf4
    if ( $count ) goto barf3
    if ($?barfed) goto mail
    goto sucess

    barf1:
      # preprocessor found no header lines...
      echo "You probably did not sumbit your orders in the" >> $testd/$reject
      echo "proper format:" >> $testd/$reject
      echo "The subject should be:$game orders for <racename>" >> $testd/$reject
      echo "And the body of message, no <> s:" >> $testd/$reject
      echo "#Galaxy $game <race name>" >> $testd/$reject
      echo "orders" >> $testd/$reject
      echo "orders" >> $testd/$reject
      echo "#end" >> $testd/$reject
      echo "" >> $testd/$reject
      #cat $testd/$errors >> $testd/$reject
      cat $testd/${errors}.dbg >> $testd/$reject # FIX????
      echo "" >> $testd/$reject
      echo "" >> $testd/$reject
      set barfed
      goto mail

    # Not possible under V2.40+
    barf1a:
      # Bad orders, but recoverable?
      echo "One or more of your commands was malformed" >> $testd/$reject
      echo "Bad lines will be deleted"  >> $testd/$reject
      echo "Preparser syntax checking log:" >> $testd/$reject
      cat $testd/$errors >> $testd/$reject
      #cat $startd/$x >> $testd/$reject
      set barfed
      if ($order_barf == 101) then
        echo "Unable to recover,  aborting pass II" >> $testd/$reject
        goto mail
      endif
      if ($order_barf == 102) then
        echo "Assuming missing #end" >> $testd/$reject
        echo "q" >> $testd/$pre
      endif
      echo "Attempting to check parsable orders..." >> $testd/$reject
      goto orders_resume

    # Not possible under V2.40+
    barf2:
      # user forgot a "galaxy" line.....
      echo "One or more of your commands was malformed" >> $testd/$reject
      echo "You probably forgot to include the proper" >> $testd/$reject
      echo "beginning lines, so it could not tell where your" >> $testd/$reject
      echo "orders begin" >> $testd/$reject
      echo "" >> $testd/$reject
      echo "Body of mail message format:" >> $testd/$reject
      echo "#Galaxy <game> <racename>" >> $testd/$reject
      echo " orders" >> $testd/$reject
      echo " orders" >> $testd/$reject
      echo " orders" >> $testd/$reject
      echo "#end" >> $testd/$reject
      if ( -e $testd/$errors ) then
        cat $testd/$errors >> $testd/$reject
      endif
      #cat $startd/$x >> $testd/$reject
      set barfed
      goto mail

    barf3:
      # Galaxy code itself didn't like something....
      echo "One or more of your commands failed" >> $testd/$reject
      if ($count > $TOOMANYBAD ) then
        echo "Probable server error. Check orders or consult " >> $testd/$reject
        echo "with GM @ $gmaddr" >> $testd/$reject
      else
        echo "Estimate: $count commands misparsed..." >> $testd/$reject
      endif
      echo "This may give you some idea:" >> $testd/$reject
      cat $testd/${errors}.dbg >> $testd/$reject # FIX????
      echo "" >> $testd/$reject
      echo "" >> $testd/$reject
      echo "Any orders flagged above may be DELETED"   >> $testd/$reject
      echo "Please do NOT count on it being fixed by me" >> $testd/$reject
      echo "" >> $testd/$reject
      set barfed
      goto mail

    barf4:
      # Galaxy code misbehaved
      echo "Galaxy code failure? Error code $count" >> $testd/$reject
      echo "Probable GM error. Check orders or consult " >> $testd/$reject
      echo "with GM @ $gmaddr" >> $testd/$reject
      echo "Error code lookup suggests this error:" >> $testd/$reject
      if ( $count == 126 ) echo "Missing races:passwords file?" >>$testd/$reject
      if ( $count == 125 ) echo "Damaged/incompatable blindrc file?" >> \
          $testd/$reject
      if ( $count == 124 ) echo "Out of memory on compter?" >>$testd/$reject
      if ( $count == 123 ) echo "blindrc file save failure?" >>$testd/$reject
      if ( $count == 122 ) echo "Report file save failure?" >>$testd/$reject
      echo "" >> $testd/$reject
      echo "This may give you some idea:" >> $testd/$reject
      cat $testd/${errors}.dbg >> $testd/$reject # FIX????
      echo "" >> $testd/$reject
      echo "" >> $testd/$reject
      echo "Any orders flagged above may be DELETED"   >> $testd/$reject
      echo "Please do NOT count on it being fixed by me" >> $testd/$reject
      echo "" >> $testd/$reject
      set barfed

      # Inform GM of serious problem
      if ( $count == 126 ) echo "Missing races:passwords file?" | \
          $mailprog $gmaddrfatal
      if ( $count == 125 ) echo "Damaged/incompatable blindrc file?" | \
          $mailprog $gmaddrfatal
      if ( $count == 124 ) echo "Out of memory on compter?" | \
          $mailprog $gmaddrfatal
      if ( $count == 123 ) echo "blindrc file save failure?" | \
          $mailprog $gmaddrfatal
      if ( $count == 122 ) echo "Report file save failure?" | \
          $mailprog $gmaddrfatal
      goto mail

    sucess:
      # No bugs?!?
      echo "All your orders appear to be correct" >> $testd/$reject
      echo "orders received: " >> $testd/$reject
      echo "" >> $testd/$reject
      body.awk $startd/$x >> $testd/$reject

    mail:
      if ($?barfed && $?mail) then
        body.awk $startd/$x >> $testd/$reject
      endif
      echo "" >> $testd/$reject
      echo "This reply was automatically generated" >> $testd/$reject
      echo "Questions should go to $gmaddr" >> $testd/$reject
      echo "GM requests should go to $gmaddr" >> $testd/$reject
      echo "Orders should go to $gameaddr" >> $testd/$reject
      echo "Messages to be forwarded should go to $gameaddr" >> $testd/$reject

      # "create" a reply addr [needs fixing]
      set use = `return.awk < $startd/$x | cut -d"<" -f2 | sed 's/>.*//' | sed 's/(.*)//'`
      echo -n "$use "
      #set use = `grep "Return-Path: <" $startd/$x |head | cut -d"<" -f2 | sed 's/>//'`
      # Make sure this one isn't faked
      /usr/bin/grep -s "Return-Path: <" $startd/$x
      if ($status) then
        unset ack
      else
        set ack
      endif
      if ((($?mail && ! $?silent) || $?check) || ( $?test &&  $?barfed )) then
        #ln -s $testd/$reject $testd/${reject}.$x
        ln -s $testd/$errors.dbg $testd/${reject}.$x
        if (`cat $testd/$errors | wc -l ` != 0 ) then
          ln -s $testd/$errors $testd/$errors.$x
          less $testd/$errors.$x
        endif
        less $testd/$reject.$x 
      endif
      if ( $?test &&  $?barfed ) then
        echo $x needs to be fixed
        sleep 5
      endif
      if ($?mail && $?ack ) then
        touch $testd/forecast.F # send forecast file
        if ($use =~ $gameaddr ) then
          set use = $gmaddr
        endif
        if (! $?barfed ) then  # Orders have no errors
          rm -f /tmp/pp-barf.$$ ; touch /tmp/pp-barf.$$
          echo "HELO localhost" >> /tmp/pp-barf.$$
          echo "MAIL FROM:<$gameaddr>" >> /tmp/pp-barf.$$
          echo "RCPT TO: <$use>" >> /tmp/pp-barf.$$
          echo DATA >> /tmp/pp-barf.$$ 
          echo "To:  <$use>" >> /tmp/pp-barf.$$
          echo "From: Blind Galaxy Server <zeus@cs.utk.edu>" >> /tmp/pp-barf.$$
          echo "Subject: $game preprocessor results OK" >> /tmp/pp-barf.$$
          cat $testd/$reject  >> /tmp/pp-barf.$$
          if ( -f $testd/forecast.F ) cat  $testd/forecast.F >> /tmp/pp-barf.$$
          echo "" >> /tmp/pp-barf.$$
          echo "." >> /tmp/pp-barf.$$
          echo "quit" >> /tmp/pp-barf.$$
          /usr/lib/sendmail -bs < /tmp/pp-barf.$$ > /dev/null
        else # Buggy orders
          (cat $testd/$reject ; echo ""; cat  $testd/forecast.F ) | \
            $mailprog -s "$game : preprocessor results (Errors)" $use 
        endif
        rm -f $testd/forecast.F
        echo "Mailed\!"
      else
        echo "Done\!"
      endif
  endif
  if ($?retry && $?barfed && -e $startd/$x) then
    echo "Retrying $x"
  else
    @ x++
  endif
end

if ($?all) then
    $preproc $startd/[0-9] $startd/[1-9]?  | $testd/$exec | $PAGER
endif


# Do house cleaning
done:
onintr -
if !( $testd =~ $execd ) then
  rm -f $testd/$data $testd/$exec
endif
rm -f $testd/$$.pre-proc    # preprocessor output
rm -f $testd/$$.post-proc  # galaxy errors
rm -f $testd/$$.errors* # preprocessor errors
rm -f $testd/$$.reject   # ack message
rm -f $testd/$$.reject.*  # ack message fudges
rm -f $testd/forecast.F
rm -f /tmp/pp-barf.$$     # Sendmail stuff
rm $LOCKFILE

exit
