
#!/bin/sh
#
#  mailgo 1.57    E-mail go game management program by Adrian Mariano
#     6/5/92      I place it on the public domain.  Please send improvements
#                 to me at adrian@u.washington.edu.
# 
#  Syntax: mailgo [-r] filename
#          mailgo -n [filename]
#          mailgo -c 
# 
#  Filename should contain a mailgo go game
#  
#  -r resends the file to the opponent
#  -n start a new game, using data in filename to get address
#  -c removes temporary files left when abnormally terminated
#
#  Invokes the game processor in the environment variable MAILGO, or attempts
#  to run xgoban from the inherited path if MAILGO is not set.
#
#  System V Unix users should change the assignment to the MAILER variable
#  from 'mail' to 'mailx' or 'elm' or some other compatible mailer.
#

MAILER=mail


if [ $# -eq 0 ]
then
  echo ' '
  echo 'Syntax: mailgo [-r] filename'
  echo '        mailgo -n [filename]'
  echo '        mailgo -c '
  echo ' '
  echo 'Mailgo is a program to manage email go games.'
  echo 'Filename should contain a mailgo go game'
  echo '  '
  echo '  -r resends the file to the opponent'
  echo '  -n start a new game, using data in filename to get address'
  echo '  -c removes temporary files left when abnormally terminated'
  echo ' '
  exit
fi
case $1 in
  -c) if [ $# -ne 1 ]
       then
         echo Wrong number of parameters
         exit
       else
         rm -f MailGo* MailFile* MailOut*
         exit
       fi;;
  -r) if [ $# != 2 ]
       then
         echo Wrong number of parameters
         exit
       fi
       if grep "\---GoGaMeEnD---" $2 >/dev/null 2>&1
       then         
         name=`sed -n '2,5s/TO: //p' $2`
         hisfile=`sed -n '2,5s/TOFILE: //p' $2`
         echo Remailing response to $name, $hisfile
         $MAILER -s "Remailed Go Game: $hisfile" $name < $2
         exit
       else
         echo Invalid input file $2
         exit
       fi;;
  -n) if [ $# -gt 2 ]
       then
         echo Wrong number of parameters
         exit
       fi
       if [ $# -eq 2 ]
       then
         if [ ! -s $2 ]
         then
           echo File $2 not found.
           exit
         fi
         opponent=`sed -n '2,5s/TO: //p' $2`
         echo Opponent address: $opponent
         hisfile=`sed -n '2,5s/TOFILE: //p' $2`
         echo His game file: $hisfile
         me=`sed -n '2,5s/FROM: //p' $2`
         echo Your address: $me
         myfile=`sed -n '2,5s/FROMFILE: //p' $2`
         echo Your game file: $myfile
       else
         echo -n "Opponent address: "
         read opponent
         echo -n "Opponent game filename: "
         read hisfile
         echo -n "Your address: "
         read me
         echo -n "Your game filename: "
         read myfile
       fi
       echo -n "Black player name: "
       read bpname
       echo -n "             rank: "
       read bprank
       echo -n "White player name: "
       read wpname
       echo -n "             rank: "
       read wprank
       echo -n "Handicap: "
       read handicap
       echo -n "Komi: "
       read komi
       echo -n "Game record format (Long/Short/Extrashort): "
       read format
       case $format in
         L | l) format='L' ;;
         E | e) format='E' ;;
         *)     format='S' ;;
       esac
       started=`date|awk '{print $3, $2, $6}'`
       if grep "W\[\]" $myfile >/dev/null 2>&1  
       then
         echo -n "File $myfile contains game possibly in progress.  Clobber (y/N)? "
         read res
         case $res in
           Y | y) ;;
           *)  echo -n "Your game filename: "
               read myfile;;
         esac
       fi
       cat <<END_END >$myfile
---GoGaMeStArT---
FROM: $me
FROMFILE: $myfile
TO: $opponent
TOFILE: $hisfile
NEWGAME
FORMAT: $format
END_END
       cat <<END_END > MailGo$$
(
;
GaMe[1]
VieW[]
SiZe[19]
Comment[ Black: $bpname, $bprank
 White: $wpname, $wprank
 Handicap: $handicap
 Komi: $komi
 Started: $started
]
PlayerBlack[$bpname]
BlackRanking[$bprank]
PlayerWhite[$wpname]
WhiteRanking[$wprank]
KoMi[$komi]
DaTe[$started]
END_END
       case $handicap in
         2) echo "AB[dp][pd]" >> MailGo$$ ;;
         3) echo "AB[dp][pd][pp]" >> MailGo$$ ;;
         4) echo "AB[dd][dp][pd][pp]" >> MailGo$$ ;;
         5) echo "AB[dd][dp][jj][pd][pp]" >> MailGo$$ ;;
         6) echo "AB[dd][dj][dp][pd][pj][pp]" >> MailGo$$ ;;
         7) echo "AB[dd][dj][dp][jj][pd][pj][pp]" >> MailGo$$ ;;
         8) echo "AB[dd][dj][dp][jd][jp][pd][pj][pp]" >> MailGo$$ ;;
         9) echo "AB[dd][dj][dp][jd][jj][jp][pd][pj][pp]" >> MailGo$$ ;;
       esac
       if [ 0$handicap -ge 2 -a $handicap -le 9 ]
       then
           echo "PL[W]HA[$handicap]" >> MailGo$$ ;
       fi
       echo ")" >> MailGo$$

       case $format in
         L) sflag='';;
         *) sflag=-s;;
       esac
       if ${MAILGO-xgoban} $sflag -m MailOut$$ MailGo$$
       then
         if [ -s MailOut$$ ]
         then
           cat MailOut$$ >>$myfile
         else
          cat MailGo$$ >>$myfile
         fi
       else
         echo -n "No move made, or xgoban error. Send anyway (y/N)? "
         read res
         case $res in
           Y | y) cat MailGo$$ >>$myfile;;
           *) echo   NOT mailing new game.
              rm -f MailGo$$ MailOut$$
              exit;;
         esac
       fi
       rm -f MailOut$$ MailGo$$
       echo Mailing new game to $opponent, $hisfile
       echo >> $myfile
       echo "---GoGaMeEnD---" >> $myfile
       $MAILER -s "New Go Game: $hisfile" $opponent <$myfile
       exit
       ;;
esac
if [ $# -ne 1 ]
then
  echo Wrong number of parameters
  exit
fi
if [ ! -s $1 ]
then
  echo File $1 not found
  exit
fi
sed -n '/---GoGaMeStArT---/,/---GoGaMeEnD---/{s/---GoGaMe.*---//
p
}' $1 > MailGo$$
if [ ! -s MailGo$$ ]
then
  echo Invalid input file $1
  exit
fi
if sed -n 6,7p MailGo$$ | grep FORMAT > /dev/null 2>&1
then
  format=`sed -n '6,7s/FORMAT: //p' MailGo$$`
  if [ ! \( $format = 'L' -o $format = 'E' \) ] 
  then
    format='S'
  fi
else
  format='L'
fi
case $format in
  L) sflag='';;
  *) sflag='-s';;

#  E) sflag='-s';;
#  S) sflag='-s'
#     sed -n  -e 'H' -e '$g' -e '$s/\n;/;/g' -e '$s/^\n//' -e '$p' <MailGo$$ >MailGoX$$
#     mv -f MailGoX$$ MailGo$$ ;;

esac
if ${MAILGO-xgoban} $sflag -m MailOut$$ MailGo$$
then
  echo >> MailOut$$
  echo ---GoGaMeStArT--- > MailFile$$
  sed -n '1,/(/s/FROM/TO/p' MailGo$$ >> MailFile$$
  sed -n '1,/(/s/TO/FROM/p' MailGo$$ >> MailFile$$
  echo FORMAT: $format >> MailFile$$
  if [ $format = 'S' ]
  then
    sed 's/\(;[BC]\[\)/\
\1/g' MailOut$$ >>MailFile$$
  else
    cat MailOut$$ >> MailFile$$
  fi
  echo "---GoGaMeEnD---" >> MailFile$$
  name=`sed -n '2,5s/FROM: //p' MailGo$$`
  fileout=`sed -n '2,5s/TOFILE: //p' MailGo$$`
  tofile=`sed -n '2,5s/FROMFILE: //p' MailGo$$`
  if sed -n 6p MailGo$$ | grep NEW > /dev/null 2>&1 && [ -s $fileout ]
  then
    echo -n "File $fileout exists.  Clobber (y/N)? "
    read res
    case $res in
      Y | y) rm -f $fileout ;;
      *) echo -n "Output filename: "
         read newfileout
         sed "2,5s/FROMFILE: $fileout/FROMFILE: $newfileout/" MailFile$$>Mfil$$
         mv Mfil$$ MailFile$$
         fileout=$newfileout ;;
    esac
  fi
  echo Mailing response to $name, $tofile
  $MAILER -s "Go Game: $tofile" $name < MailFile$$ 
  rm -f MailGo$$ MailOut$$ $1
  if grep "\---GoGaMeEnD---" $fileout > /dev/null 2>&1 || [ ! -s $fileout ]
  then
    mv MailFile$$ $fileout
  else
    echo Save file $fileout doesn\'t look like a mailgo file.  
    echo "Overwrite it anyway (y/N)?"
    read res
    case $res in
      y | Y) mv -f MailFile$$ $fileout;;
      *)     echo Game record left in MailFile$$;;
    esac
  fi
else
  echo No move made, or xgoban error.  NOT mailing response.
  rm -f MailGo$$ MailOut$$
fi
