/******************************************************************************
$Id: mkbox.c,v 3.2 1992/03/25 20:29:00 nates Exp nates $

this is a program to make an authorized template file for the purpose of
sending it to nbastats-admin@athena.mit.edu.  a raw template file is input
as an argument.  it will write to the screen for checking the template or
into a formatted boxscore file for posting to rec.sports.basketball.pro.

  TO USE:

1. mkbox  rawinputfile                (echos report to screen & writes out
   -------------------                 the template visGGhomGG.DDmonYR file)

2. mkbox  rawinputfile  reportfile    (creates report file & writes out
   -------------------------------     visGGhomGG.DDmonYR file as well)

it is important that the rawinputfile name NOT be visGGhomGG.DDmonYR format.
                                           ~~~
if there is anything wrong internally, there will be a warning message.
this will point you to where the problem is.

if nothing is wrong, then you will get your prompt back and both files
are ready.

to build:
             cc -o mkbox mkbox.c

$Log: mkbox.c,v $
 * Revision 3.2  1992/03/25  20:29:00  nates
 * drove fresh Stake into bench technical bug's heart.
 * enabled COMMENT & MISSING in boxscores
 * expanded separator list with TAB, RETURN, FORMFEED, BKSPACE.
 *
 * Revision 3.1  1992/03/20  22:34:08  nates
 * added 2 subroutines, prnplyr() and boxplyr() to handle missing data.
 * added warning about team turnovers incorrectly using total individual.
 *
 * Revision 3.0  1992/03/19  19:22:54  nates
 * fixed strnlike to stop at anything < blank.
 * fixed chkref so that blank lines in the officials file are okay.
 * added some more checks on file pointers.
 * added check of time format
 * fixed the big bench technical gaff
 * spruced up exit line.
 *
 * Revision 2.6  1992/03/18  14:40:20  nates
 * converted mkbox from redirected stdio to specified input and output
 * filenames as parameters in the command line.  changed all gets( to
 * fgets(fin,  and all  printf( to printf(box,.
 * made a string compare of the input file name against the template name.
 * revised the descriptive text at the top to reflect these changes.
 *
 * Revision 2.5  1992/03/18  13:44:11  nates
 * converted '?' in player lines to '-3'.
 * allow completion to creation of template on WARNINGS
 * made FATAL errors for file opening trouble.
 * add check for template file existing before trashing it.
 * slightly altered opening words.
 *
 * Revision 2.4  1992/03/13  18:02:02  nates
 * made all user messages begin with ** WARNING **
 * fixed bug in bench technical coach name
 *
 * Revision 2.3  1992/03/12  14:30:21  nates
 * changed parsing of player line to report appropriate
 * messages in case of errors and to allow omission of
 * first names in those player name abbreviations that
 * are unique within the team roster.
 *
 * Revision 2.2  1992/03/12  00:36:10  nates
 * changed player name checking to dump all matching names to
 * screen and tell user which one it picked (the last one).
 * this allows the user to re-edit the template and clear up
 * ambiguities such as "roberts" for milwaukee.
 *
 * Revision 2.1  1992/03/11  14:53:15  nates
 * added player name checking,
 * added coach name checking,
 * added referee name checking,
 * added TEXT keyworded BOXSCORE text option
 * AND continued stderr output to user of msgs.
 *
 * Revision 1.9  1992/03/03  23:27:11  nates
 * fixed home team bench technical bug.
 * made either fga-fgm or fgm-fga columns work (and 3p, ft).
 * checked all missed shots against all rebounds.
 * checked quarters scores against all other total scores.
 * improved checking of GAME line to require DDmonYR dates.
 * continued to divert user msgs to stderr instead of stdout.
 *
 * Revision 1.8  1992/02/28  00:17:59  nates
 * fixed BOXscore bug on missing linefeed after team rebound line.
 *
 * Revision 1.7  1992/02/26  21:51:07  nates
 * able to read past e-mail headers now and find GAME line.
 *
 * Revision 1.6  1992/02/21  19:18:10  nates
 * lowercased filename (with date) and all player names.
 * fixed TOK bug in 2-number team rebound lines.
 * forced in a missing team turnovers between t.rebounds,t.techs.
 * stripped out all unnecessary line in the BOXSCORE output.
 *
 * Revision 1.5  1992/02/20  23:41:19  nates
 * fixed bug in supplying missing comma.
 * made procedure technicals line only print if not zero.
 * added blank back into field separator list.
 *
 * Revision 1.4  1992/02/20  00:12:39  nates
 * made it not care of there were 2 or 3 numbers in the team rebs line
 * made it strip out blanks or substitute them with commas if appropriate
 *
 * Revision 1.3  1992/02/12  22:17:39  nates
 * added stuff to deal with team turnovers
 * added check for interractive entry with simple carriagereturn
 * for input - message will come out now before segmentation,core dump
 *
 * Revision 1.2  1992/02/12  18:00:33  nates
 * correct spelling of attendance
 *
 * Revision 1.1  1992/02/11  15:09:05  nates
 * Initial revision
 *
             
*****************************************************************************/
#include <stdio.h>
extern char *malloc();
extern char *strtok();
extern char *strchr();

#ifndef NULL
#define NULL      0
#endif
#define LINESIZE 96

FILE *fout, *fin, *box;
int current=0, homeindex=0;
struct tot
{
  char name[24];
  int min, fga, fgm, tha, thm, fta, ftm, or, tr, as, pf, st, to, bk, tc, ej;
} vtot, htot, player[24], *tp, *pp;
char line[LINESIZE];
char buff[LINESIZE];
char outl[LINESIZE];
char template[20];
char vis[4], hom[4], date[32], time[6], ref1[12],ref2[12],ref3[12];
char TOKS[] = ", \n\t\r\f\b";
char *vprs[32], *hprs[32];
int vc=0, hc=0;

int loadp()
{
  FILE *rost;
  char row[LINESIZE], *cp;
  int haveboth, j;

  haveboth = 0;
  rost = fopen("roster","r" );
  if( rost == NULL )
    {
fprintf(stderr,"** WARNING ** unable to find roster file\n");
      return( haveboth );
    }
  while( fgets( row, LINESIZE, rost ) != NULL )
    {
      if( strncmp( row, vis, 3 ) == 0 )
        {
          if( strlen( row ) < 5 )
            {
              j = 0;
              fgets( row, LINESIZE, rost );
              while( (row[0] & 040) && (j < 31) )
                {
                  cp = strtok( row, TOKS );
                  vprs[j] = malloc( 32 );
                  strncpy( vprs[j], cp, 32 );
                  cp = strtok(  0 , TOKS );
                  strcat( vprs[j], "," );
                  strcat( vprs[j], cp );
                  j++;
                  if( fgets( row, LINESIZE, rost ) == NULL )
                    {
                      fclose( rost );
                      return( haveboth );
                    }
                }
              vc = j;
              haveboth++;
              while( (j < 31) && (row[0] > '@') )
                {
                  cp = strtok( row, TOKS );
                  vprs[j] = malloc( 32 );
                  strncpy( vprs[j], cp, 32 );
                  j++;
                  if( fgets( row, LINESIZE, rost ) == NULL )
                    {
                      fclose( rost );
                      return( haveboth );
                    }
                }
              vprs[j] = NULL;        /* flag end of list */
              if( haveboth == 3 )
                {
                  fclose( rost );
                  return( haveboth );
                }
            }
        }
      else if( strncmp( row, hom, 3 ) == 0 )
        {
          if( strlen( row ) < 5 )
            {
              j = 0;
              fgets( row, LINESIZE, rost );
              while( (row[0] & 040) && (j < 31) )
                {
                  cp = strtok( row, TOKS );
                  hprs[j] = malloc( 32 );
                  strncpy( hprs[j], cp, 32 );
                  cp = strtok(  0 , TOKS );
                  strcat( hprs[j], "," );
                  strcat( hprs[j], cp );
                  j++;
                  if( fgets( row, LINESIZE, rost ) == NULL )
                    {
                      fclose( rost );
                      return( haveboth );
                    }
                }
              hc = j;
              haveboth += 2;
              while( (j < 31) && (row[0] > '@') )
                {
                  cp = strtok( row, TOKS );
                  hprs[j] = malloc( 32 );
                  strncpy( hprs[j], cp, 32 );
                  j++;
                  if( fgets( row, LINESIZE, rost ) == NULL )
                    {
                      fclose( rost );
                      return( haveboth );
                    }
                }
              hprs[j] = NULL;        /* flag end of list */
              if( haveboth == 3 )
                {
                  fclose( rost );
                  return( haveboth );
                }
            }
        }
    }
  fclose( rost );
  return( haveboth );
}
void blast( s )
     char *s;
{
  char *t, *nb;
  t = outl;
  nb = s;
  while( *s > 037 )
    {
      if( *s == ' ' )
        {
          if( *nb != ',' )
            {
              *t++ = ',';
              *(++nb) = ',';
            }
          s++;
          while( *s == ',' ) s++;
        }
      else
        {
          nb = s;
          *t++ = *s++;
        }
    }
  *t = '\0';
  fprintf( fout,"%s\n", outl );
  return;
}
char *readplyr( s )
     char *s;
{
  char *cp, *t;
  int i, j;

  pp = &player[current++];
  cp = s;
  while( *cp && ((*cp < '0') || (*cp > '9')) )
    cp++;
  if( *cp == '\0' )
      return "no data found\n";
  t = cp;
  while( (*(cp-1) < 'a') && (cp-s) )
    cp--;
  if( cp == s )
      return "no name in line\n";
  *cp = '\0';
  strncpy( pp->name, s, 23 );
  cp = strtok( t, TOKS );   /* minutes    */
  if( cp == NULL )
      return "no data\n";
  i = atoi( cp );
  tp->min += i;
  if( *cp == '?' )
    pp->min = -3;
  else
    pp->min = i;
  cp = strtok( 0, TOKS );   /* fga        */
  if( cp == NULL )
      return "missing 15 numbers\n";
  if( *cp == '?' )
    i = -1;
  else
    i = atoi( cp );
  cp = strtok( 0, TOKS );   /* fgm        */
  if( cp == NULL )
      return "missing 14 numbers\n";
  if( *cp == '?' )
    j = -1;
  else
    j = atoi( cp );
  if( j > i )
    {
      if( j > 0 )
        tp->fga += j;
      pp->fga = j;
      if( i > 0 )
        tp->fgm += i;
      pp->fgm = i;
    }
  else
    {
      if( i > 0 )
        tp->fga += i;
      pp->fga = i;
      if( j > 0 )
        tp->fgm += j;
      pp->fgm = j;
    }
  cp = strtok( 0, TOKS );   /* tha        */
  if( cp == NULL )
      return "missing 13 numbers\n";
  if( *cp == '?' )
    i = -1;
  else
    i = atoi( cp );
  cp = strtok( 0, TOKS );   /* thm        */
  if( cp == NULL )
      return "missing 12 numbers\n";
  if( *cp == '?' )
    j = -1;
  else
    j = atoi( cp );
  if( j > i )
    {
      if( j > 0 )
        tp->tha += j;
      pp->tha = j;
      if( i > 0 )
        tp->thm += i;
      pp->thm = i;
    }
  else
    {
      if( i > 0 )
        tp->tha += i;
      pp->tha = i;
      if( j > 0 )
        tp->thm += j;
      pp->thm = j;
    }
  cp = strtok( 0, TOKS );   /* fta        */
  if( cp == NULL )
      return "missing 11 numbers\n";
  if( *cp == '?' )
    i = -1;
  else
    i = atoi( cp );
  cp = strtok( 0, TOKS );   /* ftm        */
  if( cp == NULL )
      return "missing 10 numbers\n";
  if( *cp == '?' )
    j = -1;
  else
    j = atoi( cp );
  if( j > i )
    {
      if( j > 0 )
        tp->fta += j;
      pp->fta = j;
      if( i > 0 )
        tp->ftm += i;
      pp->ftm = i;
    }
  else
    {
      if( i > 0 )
        tp->fta += i;
      pp->fta = i;
      if( j > 0 )
        tp->ftm += j;
      pp->ftm = j;
    }
  cp = strtok( 0, TOKS );   /*  or        */
  if( cp == NULL )
      return "missing 9 numbers\n";
  j = atoi( cp );
  tp->or  += j;
  if( *cp == '?' )
    pp->or = -3;
  else
    pp->or = j;
  cp = strtok( 0, TOKS );   /*  tr        */
  if( cp == NULL )
      return "missing 8 numbers\n";
  i = atoi( cp );
  if( j > i )
      return "or > tr\n";
  tp->tr  += i;
  if( *cp == '?' )
    pp->tr = -3;
  else
    pp->tr = i;
  cp = strtok( 0, TOKS );   /*  as        */
  if( cp == NULL )
      return "missing 7 numbers\n";
  i = atoi( cp );
  tp->as  += i;
  if( *cp == '?' )
    pp->as = -3;
  else
    pp->as = i;
  cp = strtok( 0, TOKS );   /*  pf        */
  if( cp == NULL )
      return "missing 6 numbers\n";
  i = atoi( cp );
  tp->pf  += i;
  if( *cp == '?' )
    pp->pf = -3;
  else
    pp->pf = i;
  if( i > 6 )
      return "fouls > 6\n";
  cp = strtok( 0, TOKS );   /*  st        */
  if( cp == NULL )
      return "missing 5 numbers\n";
  i = atoi( cp );
  tp->st  += i;
  if( *cp == '?' )
    pp->st = -3;
  else
    pp->st = i;
  cp = strtok( 0, TOKS );   /*  to        */
  if( cp == NULL )
      return "missing 4 numbers\n";
  i = atoi( cp );
  tp->to  += i;
  if( *cp == '?' )
    pp->to = -3;
  else
    pp->to = i;
  cp = strtok( 0, TOKS );   /*  bk        */
  if( cp == NULL )
      return "missing 3 numbers\n";
  i = atoi( cp );
  tp->bk  += i;
  if( *cp == '?' )
    pp->bk = -3;
  else
    pp->bk = i;
  cp = strtok( 0, TOKS );   /*  tc        */
  if( cp == NULL )
      return "missing 2 numbers\n";
  i = atoi( cp );
  tp->tc  += i;
  if( *cp == '?' )
    pp->tc = -3;
  else
    pp->tc = i;
  if( i > 2 )
      return "technicals > 2\n";
  cp = strtok( 0, TOKS );   /*  ej        */
  if( cp == NULL )
      return "missing 1 number\n";
  if( (*cp & 01) )
    {
      if( *cp == '?' )
        pp->ej = -3;
      else
        {
          tp->ej++;
          pp->ej = 1;
        }
    }
  else
    {
      pp->ej = 0;
    }
  if( strtok( 0 , TOKS ) )
fprintf(stderr,"** WARNING ** extraneoua stuff at end of %s line, ignored.\n",pp->name);

  return (char *)NULL;
}
void prnplyr( )
{
  int *ip, j;
  
  strcpy( outl, pp->name );
  ip = &pp->min;
  for( j=0; j<16; j++ )
    {
      if( *ip < 0 )
        sprintf( date, ",??" );
      else
        sprintf( date, ",%d", *ip );
      strcat( outl, date );
      ip++;
    }
  fprintf( fout,"%s\n", outl );
  return;
}
void boxplyr( i )
     int i;
{
  int j;
  
  sprintf( outl,"%-17.17s", pp->name );
  j = pp->min;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, " %2d", j );
  strcat( outl, date );                 /* min */
  j = pp->fgm;
  if( j < 0 )
    sprintf( date, "    -" );
  else
    sprintf( date, " %4d", j );
  strcat( outl, date );                 /* fgm */
  j = pp->fga;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* fga */
  j = pp->thm;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* thm */
  j = pp->tha;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* tha */
  j = pp->ftm;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* ftm */
  j = pp->fta;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* fta */
  j = pp->or;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* or  */
  j = pp->tr;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* tr  */
  j = pp->as;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* as  */
  j = pp->pf;
  if( j < 0 )
    sprintf( date, "   -" );
  else
    sprintf( date, " %3d", j );
  strcat( outl, date );                 /* pf  */
  j = pp->st;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* st  */
  j = pp->to;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* to  */
  j = pp->bk;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* bk  */
  j = pp->tc;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* tc  */
  j = pp->ej;
  if( j < 0 )
    sprintf( date, "  -" );
  else
    sprintf( date, "%3d", j );
  strcat( outl, date );                 /* ej  */
  sprintf( date, " %4d", i );
  strcat( outl, date );                 /* pts */

  fprintf( box,"%s\n", outl );
  return;
}
int strnlike( s, t )
     char *s, *t;
{
  char *cp;
  int j;

  j = 0;
  cp = strchr( s, *t );
  while( cp && *cp )
    {
      cp++;
      j++;
      t++;
      cp = strchr( cp, *t );
    }
  return( j );
}
int chkref( r1, r2, r3 )
     char *r1, *r2, *r3;
{
  int j1, j2, j3, k, kmax1, kmax2, kmax3;
  FILE *fref;
  char *cp;
  char o1[24], o2[24], o3[24], line[24];

  kmax1 = kmax2 = kmax3 = 0;
  j1 = strlen( r1 );
  j2 = strlen( r2 );
  j3 = strlen( r3 );
  strncpy( o1,"??",24);
  strncpy( o2,"??",24);
  strncpy( o3,"??",24);
  *r1 &= 0337;
  *r2 &= 0337;
  *r3 &= 0337;
  fref = fopen("officials","r");
  if( fref == NULL )
    {
fprintf(stderr,"** WARNING ** file of officials missing, unable to check ref names.\n");
      return( 0 );
    }
  while( fgets( line, 24, fref ) != NULL )
    {
      cp = strtok( line,"\n" );
      if( cp )
        {
          k = strnlike( cp, r1 );
          if( k > kmax1 )
            {
              strncpy( o1, cp, 23 );
              kmax1 = k;
            }
          k = strnlike( cp, r2 );
          if( k > kmax2 )
            {
              strncpy( o2, cp, 23 );
              kmax2 = k;
            }
          k = strnlike( cp, r3 );
          if( k > kmax3 )
            {
              strncpy( o3, cp, 23 );
              kmax3 = k;
            }
        }
    }
  fclose( fref );
  k = 3;
  if( (j1 > kmax1) || (j1 < strlen( o1 )) )
    {
fprintf(stderr,"** WARNING ** unable to find <%s> in officials file.\n",r1 );
fprintf(stderr,"** WARNING ** closest match was <%s>.\n", o1 );
      k--;
    }
  if( (j2 > kmax2) || (j2 < strlen( o2 )) )
    {
fprintf(stderr,"** WARNING ** unable to find <%s> in officials file.\n",r2 );
fprintf(stderr,"** WARNING ** closest match was <%s>.\n", o2 );
      k--;
    }
  if( (j3 > kmax3) || (j3 < strlen( o3 )) )
    {
fprintf(stderr,"** WARNING ** unable to find <%s> in officials file.\n",r3 );
fprintf(stderr,"** WARNING ** closest match was <%s>.\n", o3 );
      k--;
    }
  return( k );
}
int checkplyr( name, prs )
     char *name, *prs[];
{
  int i, j, k, len, kmax, imax, found, imat=0;
  char *cp;

  i = 0;
  kmax =0;
  imax = 0;
  found = 0;
  len = strlen( name );
  while( prs[i] )
    {
      k = strnlike( prs[i], name );
      if( k  >= len )
        {
          if( found )
fprintf(stderr,"** WARNING ** <%s> matches <%s>.\n",name,prs[imat] );
          found++;
          imat = i;
        }
      else if( k > kmax )
        {
          imax = i;
          kmax = k;
        }
      if( found == 0 )
        {
          cp = name;
          j = len;
          while( kmax < j )
            {
              cp++;
              j--;
              k = strnlike( prs[i], cp );
              if( k > kmax )
                {
                  imax = i;
                  kmax = k;
                }
            }
        }
      i++;
    }
  if( found )
    {
      if( found > 1 )
        {
fprintf(stderr,"** WARNING ** <%s> matches <%s>.\n", name, prs[imat] );
fprintf(stderr,"** WARNING ** using <%s>.\n",prs[imat] );
        }
      strncpy( name, prs[imat], 23 );
      return( 0 );
    }
fprintf(stderr,"** WARNING ** unable to find <%s> in team roster.\n",name );
fprintf(stderr,"** WARNING ** closest match was <%s>.\n",prs[imax] );
  return( 1 );
}

main( argc, argv )
     int argc;
     char *argv[];
{
  int chkref(), loadp();
  char *readplyr();
  int checkplyr();
  void prnplyr(), boxplyr();
  int state, rosco;
  char *cp, mon[4];
  int vg, hg, vs, hs, vw, vl, hw, hl, i, j, k, ots=0, att;
  int qv1=0,qv2=0,qv3=0,qv4=0,ov[8],qh1=0,qh2=0,qh3=0,qh4=0,oh[8];
  int vmisses=0, hmisses=0, trebs, textout=0;
  int *ip;
  double fgp, thp, ftp;
  
  if( argc < 2 )
    {
      fprintf(stderr,"USAGE:  mkbox  rawinput  [boxoutput]\n");
      exit();
    }
  fin = fopen( argv[1],"r");
  if( fin == NULL )
    {
fprintf(stderr,"** FATAL ** unable to read <%s>.\n", argv[1] );
      exit();
    }
  if( argc > 2 )
    {
      box = fopen( argv[2],"w");
    }
  else
    box = stdout;

  ip = &vtot.min;
  for( i=0; i<16; i++ )
    *ip++ = 0;
  ip = &htot.min;
  for( i=0; i<16; i++ )
    *ip++ = 0;
  trebs = 0;

  if( fgets( line, LINESIZE, fin ) != NULL )
    {
      while( strncmp( line,"GAME",4 ) )
        {
          if( fgets( line, LINESIZE, fin ) == NULL )
            {
fprintf(stderr,"** FATAL ** missing GAME line.\n");
              fclose( fin );
              exit();
            }
        }
      for(i=5; i<22; i++ )
        line[i] |= 040;
      strcpy( buff, line );
      cp = strtok( line, TOKS );
      cp = strtok(  0 , TOKS );
      if( sscanf( cp, "%3s%d%3s%d.%s", vis, &vg, hom, &hg, date ) != 5 )
        {
fprintf(stderr,"** FATAL **  bad file name - must be visGGhomGG.DDmonYR format.\n");
          exit();
        }
      vis[3] = '\0';
      hom[3] = '\0';
      date[7] = '\0';
      if( sscanf( date,"%1d%1d%3s%1d%1d", &i, &j, mon, &i, &j ) != 5 )
        {
fprintf(stderr,"** FATAL **  bad date format - must be DDmonYR format.\n");
          fclose( fin );
          exit();
        }
      if( strcmp( cp, argv[1] ) == 0 )
        {
fprintf(stderr,"** FATAL ** file <%s> cannot be input.\nplease rename it first.\n",cp);
          fclose( fin );
          exit();
        }
      strcpy( template, cp );
      fout = fopen( cp,"w" );
      if( fout )
        blast( buff );
      else
        {
fprintf(stderr,"** FATAL ** unable to open <%s>\n", cp );
          fclose( fin );
          exit();
        }
      cp = strtok(  0 , TOKS );
      if( sscanf( cp,"%d-%d", &vs, &hs ) != 2 )
        {
fprintf(stderr,"** WARNING ** bad final score - must be NN-MM format, visitor first.\n");
        }
      cp = strtok( 0, TOKS );
      if( sscanf( cp,"%d", &att ) != 1 )
        {
fprintf(stderr,"** WARNING ** bad attendance, <%s>\n",cp);
        }
      cp = strtok( 0, TOKS );
      if( cp == NULL )
        {
fprintf(stderr,"** WARNING ** missing 3 refs & time.\n");
        }
      strncpy( ref1, cp,12 );
      cp = strtok( 0, TOKS );
      if( cp == NULL )
        {
fprintf(stderr,"** WARNING ** missing 2 refs & time.\n");
        }
      strncpy( ref2, cp,12 );
      cp = strtok( 0, TOKS );
      if( cp == NULL )
        {
fprintf(stderr,"** WARNING ** missing last ref & time.\n");
        }
      strncpy( ref3, cp,12 );
      cp = strtok( 0, TOKS );
      if( cp == NULL )
        {
fprintf(stderr,"** WARNING ** missing time.\n");
        }
      strcpy( time, cp );
      if( sscanf( time,"%1d:%1d%1d",&i,&j,&k) != 3 )
fprintf(stderr,"** WARNING ** time not in H:MM format.\n");
      rosco = chkref( ref1, ref2, ref3 );
      rosco = loadp();
      if( rosco == 0 )
        {
fprintf(stderr,"** WARNING ** neither team found in roster: %s %s\n",vis,hom);
fprintf(stderr,"** WARNING ** will be UNABLE to check any player names.\n");
        }
      else if( rosco == 1 )
        {
fprintf(stderr,"** WARNING ** home team problems in roster: %s\n",hom);
fprintf(stderr,"** WARNING ** will be UNABLE to check %s player names.\n",hom);
        }
      else if( rosco == 2 )
        {
fprintf(stderr,"** WARNING ** visiting team problems in roster: %s\n",vis);
fprintf(stderr,"** WARNING ** will be UNABLE to check %s player names.\n",vis);
        }
      state = 1;
      fprintf(box,"%-3s %d, %-3s %d   %s\n\n",vis,vs,hom,hs, date);
      while( state == 1 )  /* text lines */
        {
          if( fgets( line, LINESIZE, fin ) == NULL )
            {
fprintf(stderr,"** FATAL ** no VISITOR line found.\n");
              fclose( fin );
              fclose( fout );
              exit();
            }
          if( strncmp( line,"VISI",4 ) == 0 )
            {
              blast( line );
              state = 2;
              tp = &vtot;
              strncpy( tp->name, vis, 16 );
              strcat( tp->name," totals" );
              cp = strtok( line, TOKS );
              cp = strtok(  0 , TOKS );
              if( sscanf( cp,"%d-%d", &vw, &vl ) != 2 )
                {
fprintf(stderr,"** WARNING ** bad won-lost record - must be WW-LL format.\n");
                }
              if( (vw + vl) != vg )
                {
fprintf(stderr,"** WARNING ** wins+losses != games: %2d + %2d != %2d\n", vw, vl, vg );
                }
              if( textout )
                fprintf(box,"\n");
              fprintf(box,
  "%-3s (%2d-%2d)      min  fgm fga  3m 3a ftm fta or tr  as  pf st to bk tc ej  pts\n",
       vis, vw, vl );
            }
          else /* do text line */
            {
              cp = strtok( line,"\n" );
              if( strncmp( line,"BOX",3) )
                {
                  fprintf( fout,"%s\n", line );
                  if( strncmp( line,"COMMENT",7) == 0 )
                    {
                      fprintf(box, "%s\n",&line[8] );
                      textout = 1;
                    }
                  else if( strncmp( line,"MISSING",7) == 0 )
                    {
                      fprintf(box, "%s\n", line );
                      textout = 1;
                    }
                }
              else
                {
                  fprintf(box, "%s\n",&line[4] );
                  textout = 1;
                }
            }
        }
      while( state == 2 )
        {
          if( fgets( line, LINESIZE, fin ) == NULL )
            {
fprintf(stderr,"** WARNING ** no END line found.\n");
            }
          if( strncmp( line, "END", 3 ) == 0 )
            {
              blast( line );
              state = 3;
              i = vtot.ftm + vtot.thm + 2*vtot.fgm;
              if( i != vs )
                {
fprintf(stderr,"** WARNING ** individual points didnt match team total: %d, %d\n",
                         i, vs );
                }
fprintf(box,"-------------------------------------------------------------------------------\n");
fprintf(box,"%-16.16s %3d %4d %3d %3d%3d %3d%3d %3d%3d %3d %3d%3d%3d%3d%3d%3d %4d\n",
                     vtot.name,
                     vtot.min,
                     vtot.fgm,
                     vtot.fga,
                     vtot.thm,
                     vtot.tha,
                     vtot.ftm,
                     vtot.fta,
                     vtot.or,
                     vtot.tr,
                     vtot.as,
                     vtot.pf,
                     vtot.st,
                     vtot.to,
                     vtot.bk,
                     vtot.tc,
                     vtot.ej,
                     i );
              vmisses = vtot.fga - vtot.fgm + vtot.fta - vtot.ftm;
              trebs += vtot.tr;
              i = 0;
              cp = strtok( line, TOKS );  /* END  */
              cp = strtok(   0 , TOKS );  /* 1stQ */
              qv1 = atoi( cp );
              i += qv1;
              cp = strtok(   0 , TOKS );  /* 2ndQ */
              qv2 = atoi( cp );
              i += qv2;
              cp = strtok(   0 , TOKS );  /* 3rdQ */
              qv3 = atoi( cp );
              i += qv3;
              cp = strtok(   0 , TOKS );  /* 4thQ */
              qv4 = atoi( cp );
              i += qv4;
              cp = strtok(   0 , TOKS );  /* OT?  */
              j = atoi( cp );
              ots = 0;
              while( j >= 0 )
                {
                  ov[ots] = j;
                  ots++;
                  i += j;
                  cp = strtok(   0 , TOKS );  /* OT? */
                  j = atoi( cp );
                }
              if( i+j )
                {
fprintf(stderr,"** WARNING ** quarters and OTs didnt add up to total: %d %d\n", i, -j );
fprintf(stderr,"** WARNING ** remember to end in NEGATIVE total\n");
                }
              if( i != vs )
                {
fprintf(stderr,"** WARNING ** quarters and OTs didnt match score in GAME line: %d %d\n", i, vs );
                }
              i = 240 + 25*ots;
              if( i != vtot.min )
                {
fprintf(stderr,"** WARNING ** total minutes didnt match: %d %d\n",i,vtot.min);
                }
              if( fgets( line, LINESIZE, fin ) == NULL )
                {
fprintf(stderr,"** WARNING ** no team rebounds line found.\n");
                }
              if( sscanf( line,"%d,%d,%d",&i, &j, &k ) != 3 )
                {
                  j = 0;
                  if( sscanf( line,"%d,%d", &i, &k ) != 2 )
                    {
fprintf(stderr,"** WARNING ** team rebounds, turnovers, technicals error\n");
                    }
                  cp = strtok( line, TOKS );
                  cp = strtok(  0,  TOKS );
                  cp = strtok(  0,  "\n" ); /* move cp w/o cutting bench Ts */
                }
              else
                {
                  cp = strtok( line, TOKS ); /* tm reb */
                  cp = strtok(  0 , TOKS );  /* tm TOs */
                  cp = strtok(  0 , TOKS );  /* tm tch */
                  cp = strtok(  0 , "\n" );  /* benchT */
                }
              if( j >= vtot.to )
fprintf(stderr,"** WARNING **  team turnovers incorrectly including individual TO's\n");
              sprintf( buff, "%d,%d,%d",i,j,k );
              line[0] = '\0';
              if( cp )
                {
                  cp = strtok( cp, TOKS );
                  while( cp )
                    {
                      strcpy( date, cp );
                      if( checkplyr( date, &vprs[vc] ) )
fprintf(stderr,"** WARNING ** bench technical coach <%s> not found.\n",cp);
                      strcat( buff,"," );
                      strcat( buff, date );
                      strcat( line,"," );
                      strcat( line, date );
                      cp = strtok(  0 , TOKS );
                    }
                }
              blast( buff );
              sprintf( buff,"FG, 3P, FT averages:    " );
              fgp = (double)vtot.fgm/vtot.fga;
              if( fgp < 1.0 )
                sprintf( date," .%03d", (int)(1000*fgp+.5) );
              else
                sprintf( date,"%5.3f", fgp );
              strcat( buff, date );
              if( vtot.tha > 0 )
                thp = (double)vtot.thm/vtot.tha;
              else
                thp = 0;
              if( thp < 1.0 )
                sprintf( date,"   .%03d", (int)(1000*thp+.5) );
              else
                sprintf( date,"  %5.3f", thp );
              strcat( buff, date );
              if( vtot.fta > 0 )
                ftp = (double)vtot.ftm/vtot.fta;
              else
                ftp = 0;
              if( ftp < 1.0 )
                sprintf( date,"   .%03d     ", (int)(1000*ftp+.5) );
              else
                sprintf( date,"  %5.3f     ", ftp );
              strcat( buff, date );
              sprintf( date,"  team rebs %2d", i );
              strcat( buff, date );
              if( j > 0 )
                {
                  sprintf( date,", team TO's %d\n", j );
                  strcat( buff, date );
                }
              else
                strcat( buff,"\n" );
              fprintf(box, buff );
              trebs += i;
              if( strlen( line ) )
                cp = &line[1];
              else
                cp = NULL;
              if( cp && k )
                fprintf(box,"procedure technicals %d, bench technicals: %s\n", k, cp );
              else if( k )
                fprintf(box,"procedure technicals %d\n", k );
              else if( cp )
                fprintf(box,"bench technicals: %s\n", cp );
              fgets( line, LINESIZE, fin );
              if( strncmp( line,"HOME",4 ) )
                {
fprintf(stderr,"** WARNING ** expected HOME line next, got <%s>\n", line);
                }
              blast( line );
              tp = &htot;
              strncpy( tp->name, hom, 16 );
              strcat( tp->name," totals" );
              homeindex = current;
              cp = strtok( line, TOKS );
              cp = strtok(  0 , TOKS );
              if( sscanf( cp,"%d-%d", &hw, &hl ) != 2 )
                {
fprintf(stderr,"** WARNING ** bad won-lost record - must be WW-LL format.\n");
                }
              if( (vw + vl) != vg )
                {
fprintf(stderr,"** WARNING ** wins+losses != games: %2d + %2d != %2d\n", vw, vl, vg );
                }
fprintf(box,"\n%-3s (%2d-%2d)      min  fgm fga  3m 3a ftm fta or tr  as  pf st to bk tc ej  pts\n",
       hom, hw, hl );
            }
          else   /* process visitor player record */
            {
              cp = line;
              while( *cp )
                *cp++ |= 040;
              cp = readplyr( line );
              if( cp )
                {
fprintf(stderr, cp );
fprintf(stderr,"** WARNING ** %s\n", line );
                }
              if( rosco & 01 )
                checkplyr( pp->name, vprs );
              prnplyr();
              i = pp->ftm + pp->thm + 2*pp->fgm;
              boxplyr( i );
            }
        }
      while( state == 3 )
        {
          if( fgets( line, LINESIZE, fin ) == NULL )
            {
fprintf(stderr,"** WARNING ** no END line found.\n");
            }
          if( strncmp( line, "END", 3 ) == 0 )
            {
              blast( line );
              state = 4;
              i = htot.ftm + htot.thm + 2*htot.fgm;
              if( i != hs )
                {
fprintf(stderr,"** WARNING ** individual points didnt match team total: %d, %d\n",
                         i, hs );
                }
fprintf(box,"-------------------------------------------------------------------------------\n");
fprintf(box,"%-16.16s %3d %4d %3d %3d%3d %3d%3d %3d%3d %3d %3d%3d%3d%3d%3d%3d %4d\n",
                     htot.name,
                     htot.min,
                     htot.fgm,
                     htot.fga,
                     htot.thm,
                     htot.tha,
                     htot.ftm,
                     htot.fta,
                     htot.or,
                     htot.tr,
                     htot.as,
                     htot.pf,
                     htot.st,
                     htot.to,
                     htot.bk,
                     htot.tc,
                     htot.ej,
                     i );
              hmisses = htot.fga - htot.fgm + htot.fta - htot.ftm;
              trebs += htot.tr;
              i = 0;
              cp = strtok( line, TOKS );  /* END  */
              cp = strtok(   0 , TOKS );  /* 1stQ */
              qh1 = atoi( cp );
              i += qh1;
              cp = strtok(   0 , TOKS );  /* 2ndQ */
              qh2 = atoi( cp );
              i += qh2;
              cp = strtok(   0 , TOKS );  /* 3rdQ */
              qh3 = atoi( cp );
              i += qh3;
              cp = strtok(   0 , TOKS );  /* 4thQ */
              qh4 = atoi( cp );
              i += qh4;
              cp = strtok(   0 , TOKS );  /* OT?  */
              j = atoi( cp );
              ots = 0;
              while( j >= 0 )
                {
                  oh[ots] = j;
                  ots++;
                  i += j;
                  cp = strtok(   0 , TOKS );  /* OT? */
                  j = atoi( cp );
                }
              if( i+j )
                {
fprintf(stderr,"** WARNING ** quarters and OTs didnt add up to total: %d %d\n", i, -j );
fprintf(stderr,"** WARNING ** remember to end in NEGATIVE total\n");
                }
              if( i != hs )
                {
fprintf(stderr,"** WARNING ** quarters and OTs didnt match score in GAME line: %d %d\n", i, hs );
                }
              i = 240 + 25*ots;
              if( i != htot.min )
                {
fprintf(stderr,"** WARNING ** total minutes didnt match: %d %d\n",i,htot.min);
                }
              if( fgets( line, LINESIZE, fin ) == NULL )
                {
fprintf(stderr,"** WARNING ** no team rebounds line found.\n");
                }
              if( sscanf( line,"%d,%d,%d",&i, &j, &k ) != 3 )
                {
                  j = 0;
                  if( sscanf( line,"%d,%d", &i, &k ) != 2 )
                    {
fprintf(stderr,"** WARNING ** team rebounds, turnovers, technicals error\n");
                    }
                  cp = strtok( line, TOKS );
                  cp = strtok(  0,  TOKS );
                  cp = strtok(  0,  "\n" ); /* move cp w/o cutting bench Ts */
                }
              else
                {
                  cp = strtok( line, TOKS ); /* tm reb */
                  cp = strtok(  0 , TOKS );  /* tm TOs */
                  cp = strtok(  0 , TOKS );  /* tm tch */
                  cp = strtok(  0 , "\n" );  /* benchT */
                }
              if( j >= htot.to )
fprintf(stderr,"** WARNING **  team turnovers incorrectly including individual TO's\n");
              sprintf( buff, "%d,%d,%d",i,j,k );
              line[0] = '\0';
              if( cp )
                {
                  cp = strtok( cp, TOKS );
                  while( cp )
                    {
                      strcpy( date, cp );
                      if( checkplyr( cp, &hprs[hc] ) )
fprintf(stderr,"** WARNING ** bench technical coach <%s> not found.\n",cp);
                      strcat( buff,"," );
                      strcat( buff, date );
                      strcat( line,"," );
                      strcat( line, date );
                      cp = strtok(  0 , TOKS );
                    }
                }
              blast( buff );
              sprintf( buff,"FG, 3P, FT averages:    " );
              fgp = (double)htot.fgm/htot.fga;
              if( fgp < 1.0 )
                sprintf( date," .%03d", (int)(1000*fgp+.5) );
              else
                sprintf( date,"%5.3f", fgp );
              strcat( buff, date );
              if( htot.tha > 0 )
                thp = (double)htot.thm/htot.tha;
              else
                thp = 0;
              if( thp < 1.0 )
                sprintf( date,"   .%03d", (int)(1000*thp+.5) );
              else
                sprintf( date,"  %5.3f", thp );
              strcat( buff, date );
              if( htot.fta > 0 )
                ftp = (double)htot.ftm/htot.fta;
              else
                ftp = 0;
              if( ftp < 1.0 )
                sprintf( date,"   .%03d     ", (int)(1000*ftp+.5) );
              else
                sprintf( date,"  %5.3f     ", ftp );
              strcat( buff, date );
              sprintf( date,"  team rebs %2d", i );
              strcat( buff, date );
              if( j > 0 )
                {
                  sprintf( date,", team TO's %d\n", j );
                  strcat( buff, date );
                }
              else
                strcat( buff,"\n" );
              fprintf(box, buff );
              trebs += i;
              if( strlen( line ) )
                cp = &line[1];
              else
                cp = NULL;
              if( cp && k )
                fprintf(box,"procedure technicals %d, bench technicals: %s\n", k, cp );
              else if( k )
                fprintf(box,"procedure technicals %d\n", k );
              else if( cp )
                fprintf(box,"bench technicals: %s\n", cp );
            }
          else   /* process home player record */
            {
              cp = line;
              while( *cp )
                *cp++ |= 040;
              cp = readplyr( line );
              if( cp )
                {
fprintf(stderr,"** WARNING ** %s", cp );
fprintf(stderr,"** WARNING ** %s\n", line );
                }
              if( rosco & 02 )
                checkplyr( pp->name, hprs );
              prnplyr();
              i = pp->ftm + pp->thm + 2*pp->fgm;
              boxplyr( i );
            }
        }
      fclose( fout );
      if( trebs != (vmisses+hmisses) )
        {
fprintf(stderr,"** WARNING ** total rebounds did NOT equal total missed shots: %d %d\n",
                  trebs, (vmisses+hmisses) );
        }
    }
  else
    {
fprintf(stderr,"** FATAL ** no first line!\n");
      fclose( fin );
      exit();
    }
  fprintf(box,"\n%s   %2d  %2d  %2d  %2d", vis, qv1, qv2, qv3, qv4 );
  for( j=0; j<ots; j++ )
    fprintf(box,"  %2d", ov[j] );
  fprintf(box,"  -  %3d\n", vs );
  fprintf(box,"%s   %2d  %2d  %2d  %2d", hom, qh1, qh2, qh3, qh4 );
  for( j=0; j<ots; j++ )
    fprintf(box,"  %2d", oh[j] );
  fprintf(box,"  -  %3d\n", hs );
  fprintf(box,"\nattendance %d, time %s, officials: %s,%s,%s\n",att,time,ref1,ref2,ref3);
fprintf(stderr,"\n** DONE **  - template %s made.\n", template );
  fclose( fin );
  if( argc > 2 )
    fclose( box );
  exit();
}
