/********************************************************************
* getemp.c  -  program to generate TEMPLATE names for NBASTATS[tm]  *
*                                                                   *
*                                                                   *
*   to build:                                                       *
*               cc -o getemp getemp.c                               *
*                                                                   *
*   to use:                                                         *
*                                                                   *
*     reporter:            getemp  tea  [outfile]                   *
*     adminismonger:       getemp  dgt  [outfile]                   *
*                                                                   *
*      where  tea is the 3-letter NBASTATS id of the team           *
*             dgt is up to 3 digits indicating which days           *
*                    of the week you need TEMPLATE names for,       *
*                    with Sunday=0, Monday=1,...,Saturday=6         *
*                                                                   *
*             outfile is the optional name of a file to receive     *
*                     the TEMPLATE names.                           *
*                                                                   *
********************************************************************/
#ifdef RCSheader
 $Id: getemp.c,v 1.3 1992/09/25 13:20:13 nates Exp nates $
 $Log: getemp.c,v $
 * Revision 1.3  1992/09/25  13:20:13  nates
 * new version for 1992-1993.  added documentation for adminismongers
 *
 * Revision 1.2  1992/04/15  14:16:15  nates
 * moved some things around to better places.
 * added weekly numeric option for dumping out
 * template names by day of week instead of by team code.
 *
 * Revision 1.1  1992/04/10  22:27:08  nates
 * Initial revision
 *
}
#endif
#include        <stdio.h>
#include        <string.h>
#include        <ctype.h>
#include        <time.h>

char    *nbatm[] = {
   "atl",   "bos",  "cha",  "chi",
   "cle",   "dal",  "den",  "det",
   "gsw",   "hou",  "ind",  "lac",
   "lal",   "mia",  "mil",  "min",
   "njn",   "nyk",  "orl",  "phi",
   "pho",   "por",  "sac",  "san",
   "sea",   "uta",  "was",  "gm ",
   "A* "
};
int g[30];

char *month[] = {
  "-", "jan","feb","mar","apr","may","jun",
       "jul","aug","sep","oct","nov","dec"
};

/********************************************************************
* Schedule matrix...each character string represents one day in the *
* life of the NBA for the 1991-1992 season.  There are 27 different *
* teams in the NBA represented by the characters "a-z", "{", "|",   *
* and "} and correspond to the "nba_teams" array above.             *
*                                                                   *
* In general the matrix can be viewed as visitor/home/time triplet  *
* where the first entry is the visiting team, the second is the     *
* home team, and the third is the tip-off time.  This 3 character   *
* set is modified slightly in order to store any TV coverage and    *
* "alternate home site" information.                                *
*                                                                   *
* If there is a digit between the visitor and the home team then we *
* look in the "alternate_site" array for the actual location that   *
* the game is being played.                                         *
*                                                                   *
* If there is a digit after the tip-off time then the "tv_coverage" *
* array is checked to see whether NBC or TNT are broadcasting the   *
* game.                                                             *
********************************************************************/
typedef struct sched
{
  int mon;
  int day;
  int nday;
  char *tms;
} SCHED;
SCHED sch[] = {
    11, 6 ,  1,  "raTpbT{cUdeU0ohUy3jZ0mlZnsTqtTxwZizW",
    11, 7 ,  2,  "adVzfVxgWhkTqnTboWipUtrTluXj3yYs{T",
    11, 8 ,  3,  "ceTwmZgvY",
    11, 9 ,  4,  "kdV",
    11, 10,  5,  "{eTzgWmiZbnTfpUqrTcsU0uvZlwZoxV",
    11, 11,  6,  "bcThdVofVajVtkTwlZ",
    11, 12,  7,  "nhTeiZpqT{sTaxVmyYuzW",
    11, 13,  8,  "tbTckTlmZ0doUevZr{U0",
    11, 14,  9,  "ncTgdVafVkhTviZupUsqTbrTjxVwyYlzW",
    11, 15, 10,  "imZgoV{qSewW",
    11, 16, 11,  "yuXrvZ",
    11, 17, 12,  "oaU0fcTwjVgkThlZinTdpUzqTexVryYb{T",
    11, 18, 13,  "zbTstTwuX",
    11, 19, 14,  "qaThgWejVrlZisTfxVd2yY",
    11, 20, 15,  "icTzkTdmZ0{pUntTvwU0",
    11, 21, 16,  "baTefVpgWxjVulZcnTkoWtqTsrVhyYz{T",
    11, 22, 17,  "gmZiqSduWhvY",
    11, 23, 18,  "a1bTywZ",
    11, 24, 19,  "tcToeTdiZljVnkT{rTxvU0qyY",
    11, 25, 20,  "{bTlfVwgWchTqmZknTeoVrpUjsTatTvuXxzW",
    11, 27, 22,  "cbTaeTyfVskTglZjnTotTiuXmvZqzWp0{T",
    11, 28, 23,  "naTkcTlgWuiZzjV{oWhpUdrGesTbtTqwZyxV",
    11, 29, 24,  "rhSfmZwvY",
    12, 1 , 26,  "kaTbeTjgWvrTcuXmwZsyYfzWx{T",
    12, 2 , 27,  "dbU0ciZvkTnoVaqTxtT",
    12, 3 , 28,  "reTmjVslZfyYpzWo{T",
    12, 4 , 29,  "nbTvdU0igWakTxqThtTmuXcwZ",
    12, 5 , 30,  "xaTbdVveTjfVthTsiZclZqnTorGpyYgzWk{T",
    12, 6 , 31,  "pmZuoV",
    12, 8 , 33,  "daTleTnfVkiZpjVuqTyrTbsU0ovZ{wZzxV",
    12, 9 , 34,  "gbTucTedVshTvmZytT",
    12, 10, 35,  "pfVoiZlqTcrTkwZnxV{zW",
    12, 11, 36,  "gaTybTjdVehU0{mZusTltTkvZ",
    12, 12, 37,  "taTgcTqdTyeTxfVunTjpUhrTiwZozW",
    12, 13, 38,  "klWomZ{vU",
    12, 14, 39,  "grT",
    12, 15, 40,  "haTcdVjeTliZwnTbpU0rqTtsT{uXmxVoyY",
    12, 16, 41,  "zcTmfVvgWahTbkTilZetT",
    12, 17, 42,  "xjVqoVwsTd{T",
    12, 18, 43,  "rbTweTkhUplZumZanTztTfxU0v2yY",
    12, 19, 44,  "saTtdVzeTmgWpiZfjVqkThoWnrTlyYc{T",
    12, 20, 45,  "wqSivY",
    12, 21, 46,  "ndVktT",
    12, 22, 47,  "caTjhTflZwoVbqTzsTiuU0yvZgxV",
    12, 23, 48,  "jbThcT{dVkeTugWfiZymZznTwpU",
    12, 25, 50,  "rdW2xlL2",
    12, 26, 51,  "qeHifVgjVdkTxmZsnTroWapUyuXtvYbzLh0{T",
    12, 27, 52,  "tlWguWbwW",
    12, 28, 53,  "heTmnTcqTosTixVpzWa{T",
    12, 29, 54,  "eaTdcTijVkrTfwZb2yY",
    12, 30, 55,  "xgW{hTrkTblZdnTqpUmsTjuXfvZtzW",
    1, 2  , 58,  "oaUqcTkdVmeTjiZfpUlrGhsTzvYtwZgyYn{T",
    1, 3  , 59,  "lbSmoVjvYuxU0",
    1, 5  , 61,  "{aTbcTmdU0vfVujVlkThnTpoVerTqsTgwZiyYxzW",
    1, 6  , 62,  "kbTdeTlhTmpUqtT",
    1, 7  , 63,  "ufVniZvjVaoVzxV",
    1, 8  , 64,  "qbTlcTodVjgWahUwmZypUrsU0t{T",
    1, 9  , 65,  "peTzfVygWihT{qTksTdtTnwZvxV",
    1, 10 , 66,  "zjVnmZloVbrU0",
    1, 11 , 67,  "cfVxhT",
    1, 12 , 68,  "iaTbeTtkTjlZdsTgvZrwZuyYnzWo{T",
    1, 13 , 69,  "{bTrgWfhTxpUeqTitT",
    1, 14 , 70,  "haTcjVmlZxoVnvZuwZyzW",
    1, 15 , 71,  "sbTidVrfVekU0vmZgpUtqTnuXlyY",
    1, 16 , 72,  "sdVaeTfgWrjVikTboWhtTcxVwzWq0{T",
    1, 17 , 73,  "yvU0",
    1, 18 , 74,  "bdLwiOjmNlpUkqTurGstIzyYe{G",
    1, 19 , 75,  "acThfV{kTvoVjwZgxV",
    1, 20 , 76,  "abTueTziZymZtnTvpUcrT",
    1, 21 , 77,  "gfVhjVokTdqT",
    1, 22 , 78,  "qaTcdVyiZelZnsTrtTwvZhxVmzU0u{T",
    1, 23 , 79,  "uaTegWjkTtoWfsTpwZ",
    1, 24 , 80,  "kcTylWfnQjqSdxG2vzL2m{G",
    1, 25 , 81,  "waTpgWuhU0",
    1, 26 , 82,  "dfVjoVmqTtrTasTivZezUn{T",
    1, 27 , 83,  "wcTbhTgiZzlZupVktTxyY",
    1, 28 , 84,  "seTqgWdjVmkTonTarT",
    1, 29 , 85,  "mcTwfVphUliZbsT{tTxuU0",
    1, 30 , 86,  "taTdgWqjVhkTenTopUfuXwxVvyYlzWc{T",
    1, 31 , 87,  "mbF2",
    2, 1  , 88,  "ycTqfVioVswZdzU0",
    2, 2  , 89,  "yaTieTkjVulZsmZ{rTpvZqxV",
    2, 3  , 90,  "jfVanTeoVctTpuXdwZgzX",
    2, 4  , 91,  "dlZzmZyqTirTkxV",
    2, 5  , 92,  "tbTocTheTkfVwgWpjVrnU0muXsvZi{T",
    2, 6  , 93,  "eaTfgWwlZpxVy{T",
    2, 7  , 94,  "ibG2yhSqoJnrQsuWdvL2",
    2, 8  , 95,  "{jVfmZrtTszW",
    2, 9  , 96,  "o1bTecTnhTaiZzwZlxVgyY",
    2, 10 , 97,  "{fVdkTgmZnpUhqU0esTjtTluX",
    2, 11 , 98,  "scTuiZjrTawZ{xVzyY",
    2, 12 , 99,  "rdU0oeTbfVtgZ0qhUpkTvlZcnT",
    2, 13 , 100,  "edVtfV{gWkoWjpUiwZbxVuyYazW",
    2, 14 , 101,  "hcI3{iZamZnqOrsI3lvU0",
    2, 15 , 102,  "wdLkeQxlZgnTcoVzpU",
    2, 16 , 103,  "shTxiZtjToqTfrTbuU0avZ{yY",
    2, 17 , 104,  "rcTzdVfeTwkThnTgsT",
    2, 18 , 105,  "yiZbjV{lZzoVwpUauXmvZtxV",
    2, 19 , 106,  "|}Y0",
    2, 20 , 107,  "|}S0",
    2, 21 , 108,  "}|Q2",
    2, 22 , 109,  "|}[",
    2, 23 , 110,  "{aTqcTodVneTygWthTbkTwlZprTvsTuxVjzW",
    2, 24 , 111,  "qbTgfVziZvnU0roVypUatTmwZk{T",
    2, 25 , 112,  "xcTujVdsT",
    2, 26 , 113,  "baTrhU0liZckTemZxnTfpUvqTotTzuXywZs{T",
    2, 27 , 114,  "adVohTgjViyYwzW",
    2, 28 , 115,  "vbScgWfkJlmL3pnMrqG3xsG3euL3",
    3, 1  , 116,  "bhU0tiZczW",
    3, 2  , 117,  "mgWxkTjlZfoVdqU0arTpsTuvZeyY",
    3, 3  , 118,  "xbTfdVzhVmiZ{nTtuXjwZ",
    3, 4  , 119,  "vgWakTepUsqTzrTcyYl{T",
    3, 5  , 120,  "hbTxdU0jfVtmZlnTsoWwuXcvZ",
    3, 6  , 121,  "zaTufVwjVxpUgqTtyY",
    3, 7  , 122,  "ebG2viL2cmZknMhoJlsI",
    3, 8  , 123,  "laTgeTykTtpUsrU0",
    3, 9  , 124,  "{cTydTmhTnjVaoVuwZfxVpzW",
    3, 10 , 125,  "vfVqlZmrTksTbtTiuXg{T",
    3, 11 , 126,  "ceTghTqiZvjVdnTyoTpwZ",
    3, 12 , 127,  "saTobTcdVplZmtTfuXr{T",
    3, 13 , 128,  "zjVynTkrVgtTquXfwZvxVe0{T",
    3, 14 , 129,  "maScbU0dhE2lpLysT",
    3, 15 , 130,  "ldVzfVngWqvZmxU0",
    3, 16 , 131,  "acTteTbiU0sjVgpUorThwZnyY",
    3, 17 , 132,  "sfVokTxmZaqT{tTvuXhzW",
    3, 18 , 133,  "pcTreTbgWjiZnlZwyY",
    3, 19 , 134,  "paT{dVckTbmZoqTftThuXjvZiwZsxVyzW",
    3, 20 , 135,  "sgWzlZenTtoWd{T",
    3, 21 , 136,  "yjVhmU0cpLfqSxrE2kuWbvU",
    3, 22 , 137,  "zgWhiZq{T",
    3, 23 , 138,  "faTjcTpdTxeU0nsTruXlwZv2yYkzW",
    3, 24 , 139,  "nbTolZumZxpUcqTdtTf{T",
    3, 25 , 140,  "jaTigWvwZkyYrzW",
    3, 26 , 141,  "fbTsdVchUrmZqnTipUetTouXlvZzxVj{T",
    3, 27 , 142,  "naTkgWqsTowZlyY",
    3, 28 , 143,  "{1bJvcE3xfUehE3riU0kmZjpLutE3",
    3, 29 , 144,  "vaTwbT{oVgzW",
    3, 30 , 145,  "ncTudU0piZmlZtqTwrThsTyxV",
    3, 31 , 146,  "tbTyfVagWvhT{kTpmZxzW",
    4, 1  , 147,  "ojVnqTerTcsTw{T",
    4, 2  , 148,  "ubU0ecTqdVifVogWwhUskTalZrnTvtTpxV",
    4, 3  , 149,  "ijVayYv{T",
    4, 4  , 150,  "dbH2qeHpgMukLflWzmZsnMwtFrxU0",
    4, 6  , 152,  "raTneT{hTljVdoVkqTtsTmuZzvZgwZixVfyY",
    4, 7  , 153,  "scTkpUt{T",
    4, 8  , 154,  "miZnoVhqTbrU0fvZuwZlxVjzW",
    4, 9  , 155,  "daThbT{eTvmZspUctTguZwyY",
    4, 10 , 156,  "acTfiZxjVdkVglZtnTsoWeqT",
    4, 11 , 157,  "rbG2ymL3zuL3pvUh{G",
    4, 12 , 158,  "tcTdhTgiZulZonTfzW",
    4, 13 , 159,  "eaTkbTmjV{rTosTlvZxwZpyY",
    4, 14 , 160,  "rcTndVmfVjgWahTxiZqkVpuZ",
    4, 15 , 161,  "eoVbqTstTvzU0n{T",
    4, 16 , 162,  "hcTodVseTgfVrkVilZjmZvpUyuZwxV",
    4, 17 , 163,  "caTwfVbnL2hrVjyYuzWq{T",
    4, 18 , 164,  "deP2lgWimK2koJbsFptGxvU",
    4, 19 , 165,  "juZxyY",
    4, 20 , 166,  "kaTtdVheTlfVrnTmpU{sTivZzwZ",
    4, 21 , 167,  "sbTocTfgWwiZekVqrTntTmzWa{T",
    4, 22 , 168,  "hdVpjVylZuvU0gxV",
    4, 23 , 169,  "oaTebTdcTpfVkhU{nTsqTrtTvwZmyYizW",
    4, 24 , 170,  "fjVnkTwmZcoWasTxuL2",
    4, 25 , 171,  "teL3ugWqhL3yiL3vlL3zpLdrG2jxVb{G",
    0,  0 ,   0,  "END"                        /* end of list */
};

main( argc, argv )
     int argc;
     char *argv[];
{
  FILE *out;
  char team[4];
  char *vis, *hom, *cp, z;
  int vg, hg, yr;
  int weekly, wd;
  int j, k, m, found, new;

  if( argc < 2 )
    {
      printf("Usage:     getemp tea [outfile]\n");
      printf("    where\n");
      printf("            tea = 3-letter code for the team desired\n");
      printf("        OR  tea = string of 3 digits, from 0=sunday to 6=saturday,\n");
      printf("                  for adminismongers who are responsible for\n");
      printf("                  gathering templates on certain days of the week.\n");
      printf("\n");
      printf("        outfile = filename to receive list of template names\n");
      exit();
    }
  strncpy( team,argv[1],3 );
  team[3] = '\0';
  if( isdigit( *team ) )
    weekly = 1;
  else
    weekly = 0;     
  if( argc > 2 )
    out = fopen( argv[2],"w" );
  else
    out = stdout;
  j = 0;
  while( sch[j].mon > 0 )
    {
      found = 0;
      cp = sch[j].tms;
      while( *cp )
        {
          if( *cp > 0140 )
            {
              k = *cp - 0141;
              g[k]++;
            }
          cp++;
        }
      cp = sch[j].tms;
      m = sch[j].mon;
      yr = (m>6?92:93);
      if( weekly )
        {
          wd = (sch[j].nday + 4)%7;
          z = '0' + wd;
          if( strchr( team, z ) )
            found = 1;
          else
	    {
	      j++;
	      continue;
	    }
        }
      while( *cp )
        {
          if( *cp > 0140 )
            {
              k = *cp - 0141;
              if( !weekly && (strncmp( team, nbatm[k], 3 ) == 0) )
                found = 1;
              if( found )
                {
                  if( new )
                    {
                      vis = nbatm[ k ];
                      vg = g[k];
                      k = *(cp+1) - 0141;
		      if( k < 0 )
			k = *(cp+2) - 0141;
                      hom = nbatm[ k ];
                      hg = g[k];
                    }
                  else if( !weekly )
                    {
                      hom = nbatm[ k ];
                      hg = g[k];
                      k = *(cp-1) - 0141;
		      if( k < 0 )
			k = *(cp-2) - 0141;
                      vis = nbatm[ k ];
                      vg = g[k];
                    }
                }
              new = 0;
            }
          else if( *cp > 0100 )
            {
              new = 1;
              if( found )
                {
                  fprintf( out, "%3.3s%02d%3.3s%02d.%02d%3.3s%2d\n",
                       vis, vg, hom, hg, sch[j].day, month[m], yr );
                  found = weekly;
                }
            }
          cp++;
        }
      j++;
    }
  exit();
}

