#!/bin/csh -f

set path = (/usr/ucb /usr/bin /usr/local/gnu $HOME/bin /usr/local/bin \
            /usr/local/mh )

if($#argv != 1) then
   set thisfile = $0
   echo " Usage : ${thisfile:t} game-name"
   exit 1
endif

set game = $1
set maild = ~zeus/Mail/$game
set races = ~zeus/$game/races:players
set verify = ~zeus/$game/X-ref
set gm = bampton@cs.utk.edu

folder +$game > /dev/null
cd $maild

foreach x ([1-9] [1-9]* )
  if ( -e $maild/$x ) then
    echo -n "Working on $x ... "
    set use = `return.awk < $maild/$x | cut -d"<" -f2 | sed 's/>.*//' | sed 's/(.*)//'`
    grep -is "^Subject: Galaxy:" $x 
    if ($status == 0) then # Using nonstandard format, sed it out, complain
      sed 's/^Subject: Galaxy:/Subject:/' $x > $x.new
      (echo "Galaxy:$game format not fully supported. Attempting to munge";
       echo "subject into proper format. Do not be surprised if it fails";
       echo "Changing:";
       grep ^Subject: $x ;
       echo "into";
       grep "^Subject: $x.new";) | \
         mail -s "$game: Bad subject" $use
       mv $x.new $x
    endif
    grep "^Subject:" $x | grep -i "${game}:" | grep -s -i "forwardlist:"
    if ($status == 0) then
      set race = `grep "^Subject:" $x | grep -i "${game}:" | grep -i "forwardlist:" | awk -F: '{print $4}'`
      echo "Forwarding list request for $game $race"
      p-Can-Fwd $game $race
      if ($status) then
        refile $x +help
      else
        refile $x +sent
      endif
      goto loopagain
    endif
    grep "^Subject:" $x | grep -i "${game}:" | grep -s -i "lastorders:"
    if (! $status) then # is a last set of orders request
 
# Get message number of last set of orders from player
set x =`scan <gamename>-Turns | grep -i <racename> | awk '{print $1}'`
# Get player's @ from trusted list
set use = `grep <racename> <gamename>/races:players | awk '{print $2}'`
mail -s "Most recient set of orders for <racename> $use < \
   $HOME/Mail/<gamename>-Turns
 

      
      goto loopagain
    endif
    grep "^Subject:" $x | grep -i "${game}:" | egrep -s -i ":fwd:|: fwd:"
    if ($status) then # Not a forward
      grep "^Subject:" $maild/$x | grep -i "$game" | grep -si "orders"
      if ($status) then # Not orders
        grep "^Subject:" $maild/$x | grep -s "${game}: Forwarded message" 
        if !($status) then # Someone replied to a forward
          grep "^Subject:" $maild/$x | \
            grep -s "${game}: Forwarded message from GM" 
          if !($status) then # Someone replied to a forward I sent
            (echo "Dufus reply to sender, wrong subject"; echo ""; \
             echo ""; echo ""; body.awk $maild/$x) |  \
            mail -s "$game- bad reply" $gm 
          else
            echo "game OK, bogus forward... HELP"
            (echo "game $game recognized, probable bogus forward... HELP"; \
             echo ""; echo ""; body.awk $maild/$x) |  \
            mail -s "$game- confusing subject?" $gm $use
          endif
        else # Who knows what the thing is
          echo "game OK, message type unknown... HELP"
          body.awk $maild/$x |  mail -s "$game- mangled subject?" $gm
        endif
        refile $x +help 
      else
        echo "might be a turn... refiling"
        grep ^Subject: $maild/$x | head -1 | good-subject game=$game 
        if ($status) then
          (echo "Probable bad set of orders "; echo "Should be" ; \
           echo "$game orders for <racename>"; echo ""; \
           body.awk $maild/$x) |  mail -s "$game bogus orders?" $use
          refile $x +help
        else 
          set race=`grep ^Subject: $maild/$x | head -1 | awk '{print $5}'`
          grep -si ^${race}: ~/$game/races:players
          if ($status) then
            (echo "Probable bad set of orders "; echo "Should be" ; \
             echo "$game orders for <racename>"; echo ""; \
             body.awk $maild/$x) |  mail -s "$game bogus orders?" $use
            refile $x +help
          else
            set doturn
            refile $x +${game}-turns
          endif
        endif
      endif
    else
      unset ref
      echo "it's a forward. "
      set who = `grep "Subject:" $maild/$x | head -1 | cut -f5 -d:`
      if ($#who == 0) then
        (echo "Missing subject component- too few fields "; \
         echo "or perhaps there is a typo?";\
         echo ""; echo "";\
         body.awk $maild/$x) |  mail -s "$game forward error" $use $gm
        set ref
      endif
      set ncolon=`grep "^Subject:" $maild/$x |head -1 | awk -F: '{print NF}'`
      if ( $ncolon != 5) then
        (echo "Forward field count wrong" ; \
         echo "There were $ncolon fields, instead of 5" ; echo ""; echo ""; \
         body.awk $maild/$x) |  mail -s "$game- colon count error" $use $gm
        set ref
      else
        echo "who: $who"
        foreach player ($who)
          set whom = `grep -i ^${player}: $races | cut -f2 -d:`
          # Need this in case we have 2 @#'s for a single player on a line
          set nummatch = `grep -i ^${player}: $races | wc -l`
          if ($nummatch == 1) then
            set send = `grep "Subject:" $maild/$x | head -1 | cut -f4 -d:`
            grep -si ${send}:$player $verify
            set s1 = $status
            grep -si ${player}:$send $verify
            if !($status || $s1 ) then
              awk 'BEGIN {flag = 0} /^$/ {flag++} (flag != 0) {print $0}' \
                $maild/$x | \
                mail -s "${game}: Forwarded message from ${send}" $whom
              echo "mailed $whom"
            else
              echo "No match for ${send}:$whom "
              (echo "Unable to verify contact status- mail GM for details"; \
               echo "or perhaps there is a typo?"; echo ""; echo "";\
               echo "No match for ${send}:$player " ; echo ""; echo "";\
               echo "Other forwards may have suceeded"; echo ""; echo "";\
               body.awk $maild/$x) |\
                  mail -s "Zeus forwarder matching error" $gm $use
               set ref
            endif
          else
            echo "No match for $player in races:player"
            (echo "No unique match for $player in database"; echo ""; echo "";\
             body.awk $maild/$x ) | \
              mail -s "Zeus forwarder error (Who?)" $gm
              #mail -s "Zeus forwarder error (Who?)" $use $gm
              set ref
            echo "mailed $gm"
          endif
        end
      endif
      if !($?ref) then
        set ackadd = `grep -i ^$send $races | awk -F: '{print $2}'`
        (echo "A message with these headers:"; \
         egrep "^Subject:|^Date:" $maild/$x ; \
         echo ""; echo ""; echo "was forwarded as of `date`" ; \
         echo "to the parties specified" ) | \
         mail -s "$game FWD ACK" $ackadd 
        refile $x +sent
      else
        refile $x +help
      endif
    endif
  endif
loopagain:
end

if ($?doturn) then
  cd ~/Mail/$game-turns
  Pre-proc -mail -silent
  folder +$game-turns > /dev/null
  refile all +$game-Turns > /dev/null
  folder +inbox > /dev/null
endif
