/*
  NHL.C - Last modified on September 14, 1992, 11:00 EDT
includes March snow storm schedule changes

  Schedule program for the 1992-'93 NHL regular season.

  Any inquiries should be directed to:
  Valerie Hammerl (hammerl@acsu.buffalo.edu)
  Rob Springall (rgs7077@ultb.isc.rit.edu, rgs7077@ritvax.bitnet)

  ----------------------------------------------------------------------

  Original program concept by Len Carr, used with permission.
  1992-'93 schedule information coded by Valerie Hammerl.
  All other features created by Rob Springall.

  Additional credit goes to the members of the USENET community who
  contributed ideas and support for this project.

  This program is not copyrighted or registered in any form.  It does,
  however, remain the intellectual property of its authors.

  ----------------------------------------------------------------------

  To compile (*ix systems):
    cc -o nhl nhl.c

  To use, consult the program help screen:
    nhl -h

*/

#include <stdio.h>
#include <string.h>
#include <time.h>

#define  FIRST_DAY   279   /* Day of 1992 that season begins */
#define  LAST_DAY    106   /* Day of 1993 that season ends   */
#define  NUM_TEAMS   24    /* Number of teams in program     */
#define  CODE_LEN    3     /* Length of codes for -t flag    */
#define  DEF_GM_NUM  3     /* Default number of games        */
#define  MAX_GAMES   84    /* Max. no. games team will play  */

char *nhl_teams[] = {
  "Boston",       "Buffalo",      "Calgary",      "Chicago",
  "Detroit",      "Edmonton",     "Hartford",     "Los Angeles",
  "Minnesota",    "Montreal",     "New Jersey",   "NY Islanders",
  "NY Rangers",   "Ottawa",       "Philadelphia", "Pittsburgh",   
  "Quebec",       "St. Louis",    "San Jose",     "Tampa Bay",
  "Toronto",      "Vancouver",    "Washington",   "Winnipeg"
};

char *team_codes[] = {
  "bos", "buf", "cgy", "chi", "det", "edm", "hfd", "los", "min",
  "mtl", "njd", "nyi", "nyr", "ott", "phl", "pit", "que", "stl", 
  "sjs", "tmp", "tor", "van", "wsh", "wpg"
};

char *team_names[] = {
  "Bruins",      "Sabres",      "Flames",      "Blackhawks",
  "Red Wings",   "Oilers",      "Whalers",     "Kings",
  "Stars",       "Canadiens",   "Devils",      "Islanders",
  "Rangers",     "Senators",    "Flyers",      "Penguins",    
  "Nordiques",   "Blues",       "Sharks",      "Lightning",
  "Maple Leafs", "Canucks",     "Capitals",    "Jets"
};

char *neutral_sites[] = {
"Providence, RI", "Hamilton, Ont.", "Cincinnati, OH", "Milwaukee, WI", 
"Milwaukee, WI", "Cleveland, OH", "Birmingham, AL", "Phoenix, AZ", 
"Saskatoon, Sask.", "Sacramento, CA", "Hamilton, Ont.", "Oklahoma City, OK", 
"Miami, FL", "Saskatoon, Sask.", "Cleveland, OH", "Atlanta, GA", 
"Hamilton, Ont.", "Dallas, TX", "Sacramento, CA", "Halifax, N.S.",
"Hamilton, Ont.", "Saskatoon, Sask.", "Indianapolis, IN", "Saskatoon, Sask."
};

/*************************************************************************
  Schedule matrix...each character string represents one day in the life
  of the NHL for the 1992-1993 season.  There are 24 positions in each
  string...one for each NHL team (arranged alphabetically).  If the team 
  is away there will be an entry in that team's position to indicate where 
  the team will play.  The values used to represent the home team range 
  from "a-x" and correspond to the "nhl_teams" array above.
*************************************************************************/

char *nhl_schedule[] = {
"####x##crg#k##p######fu#", /* 10/6 */
"###t####################", /* 10/7 */
"####hca##n#p####bi#####s", /* 10/8 */
"##########o#w###########", /* 10/9 */
"#g#rsv#####akqwj###ic##h", /* 10/10 */
"#########b#########df###", /* 10/11 */
"######m######a########kv", /* 10/12 */
"#pI###########q###hr####", /* 10/13 */
"#####x####m##g##########", /* 10/14 */
"s#h##d##rp#o####e##u####", /* 10/15 */
"#############w#####b#x##", /* 10/16 */
"hwsu#e##j###l#kg#q######", /* 10/17 */
"########u##m#########d#o", /* 10/18 */
"#################j######", /* 10/19 */
"#####tkc#####Ul######p#e", /* 10/20 */
"###b############r#j###m#", /* 10/21 */
"c###p#n###d#####i###to##", /* 10/22 */
"f######x#m#w######b#####", /* 10/23 */
"####r#li#o##n##kt#u#####", /* 10/24 */
"v#f#d###################", /* 10/25 */
"##############m###r###x#", /* 10/26 */
"#######l#######n###q####", /* 10/27 */
"#ux#####f#g#######ej##v#", /* 10/28 */
"#######a######drm#######", /* 10/29 */
"########v##k#b####t#e#c#", /* 10/30 */
"#n#au##gc#l#j#r#######fq", /* 10/31 */
"###############t##d#####", /* 11/1 */
"#m###################c#j", /* 11/2 */
"###W#######p#f##gt######", /* 11/3 */
"##v######e####m#########", /* 11/4 */
"#s########hi#c##ap##d###", /* 11/5 */
"#####xe######v#####w####", /* 11/6 */
"#h#qji####s#a##u#o#l##g#", /* 11/7 */
"##q####s#######d#######v", /* 11/8 */
"##j################mn###", /* 11/9 */
"#####r#x#######i##v#####", /* 11/10 */
"b#g#t####k######n#####m#", /* 11/11 */
"##a##s#####o####pd###h#i", /* 11/12 */
"######b######t#e######k#", /* 11/13 */
"#ltigh####w#q#j#####as#r", /* 11/14 */
"########d####o##########", /* 11/15 */
"j######v#########u######", /* 11/16 */
"#p#e###s#n##########Q##t", /* 11/17 */
"#K######w########g###f##", /* 11/18 */
"###h##n#tq#ao#######sc#e", /* 11/19 */
"####w##########k########", /* 11/20 */
"###s#vq#b#pcxja####rh###", /* 11/21 */
"#o#########f##########q#", /* 11/22 */
"n##v###########m###e##j#", /* 11/23 */
"###########x#######u####", /* 11/24 */
"w######f#gn#p###bec##i##", /* 11/25 */
"################u####r##", /* 11/26 */
"###f##ae###oib#w##xc####", /* 11/27 */
"g##cr##u##q###l###if#jp#", /* 11/28 */
"#n######################", /* 11/29 */
"qj######m#############e#", /* 11/30 */
"#####srDn######l####k###", /* 12/1 */
"####m##################c", /* 12/2 */
"#####vs#ean####ho###d###", /* 12/3 */
"###########bw####c######", /* 12/4 */
"k##ut#h#qx####ns#f####l#", /* 12/5 */
"o########db#########m###", /* 12/6 */
"#q###c#####t#####v####n#", /* 12/7 */
"##fe#####H#############p", /* 12/8 */
"b###u########g####vM##k#", /* 12/9 */
"#####i#####d#a##hs######", /* 12/10 */
"##u###b#####t#ek#######w", /* 12/11 */
"jgni#t####p#####sh####ol", /* 12/12 */
"#####L###m######v#######", /* 12/13 */
"#ae#####################", /* 12/14 */
"##m#n#####xR##p####hi###", /* 12/15 */
"################j##s#fg#", /* 12/16 */
"#########q##rl#o#######d", /* 12/17 */
"e#####wf##t#######v#####", /* 12/18 */
"#j#oi##c###pgu#######sar", /* 12/19 */
"########d##q##t#####b###", /* 12/20 */
"#####cj#####k###p#x###n#", /* 12/21 */
"#################i#aeh##", /* 12/22 */
"##xn######mj###o##fg##b#", /* 12/23 */
"########################", /* 12/24 */
"########################", /* 12/25 */
"g###u##s####lqw##d#####i", /* 12/26 */
"m#f#d#k#xv#####bn###r###", /* 12/27 */
"########################", /* 12/28 */
"x##e#####fq#w#h##Gv#l###", /* 12/29 */
"##############s#########", /* 12/30 */
"i####x#v#c#rbe##g##dp###", /* 12/31 */
"##########w#############", /* 1/1 */
"#n#wq#a#lh##p#c##u#f#s#k", /* 1/2 */
"########g#####f##b#v###d", /* 1/3 */
"##########m#######J#e###", /* 1/4 */
"p####r###s######l######c", /* 1/5 */
"#g######k####m#####h#u##", /* 1/6 */
"##r##d##p#######a#####o#", /* 1/7 */
"#######x###b#k####u##e##", /* 1/8 */
"##pr#w####a#o###g##ijl##", /* 1/9 */
"##b##o#d#g#####x##n#####", /* 1/10 */
"#################e#u#m##", /* 1/11 */
"#ali###n##########x##k##", /* 1/12 */
"######j##########u#e##mf", /* 1/13 */
"##o####kdq#####a#n####l#", /* 1/14 */
"#v####f###########e#####", /* 1/15 */
"##iu##v####kjpa##tq####h", /* 1/16 */
"#f##o######n########d#t#", /* 1/17 */
"######X###########a#####", /* 1/18 */
"lc#x###ft###e##vn###r###", /* 1/19 */
"##########j#############", /* 1/20 */
"o############i###eg#thd#", /* 1/21 */
"#########k#####fb######c", /* 1/22 */
"#q#grx###ua#hwlc##t##i##", /* 1/23 */
"######o#t############d##", /* 1/24 */
"j#######################", /* 1/25 */
"qo##c###u#l##r####h###p#", /* 1/26 */
"###vf#j###############bm", /* 1/27 */
"##h###n###ip####ot#####a", /* 1/28 */
"###s########b###w#######", /* 1/29 */
"l#shv#####r#ujp####i###g", /* 1/30 */
"#####b########jw########", /* 1/31 */
"########v###l######sr##n", /* 2/1 */
"##w##a#q################", /* 2/2 */
"q#ke#nbjs##u##m##x#v####", /* 2/3 */
"########################", /* 2/4 */
"########################", /* 2/5 */
"########################", /* 2/6 */
"########################", /* 2/7 */
"Pn##########k###########", /* 2/8 */
"r####h###le##o######tqi#", /* 2/9 */
"#x#############m##c#####", /* 2/10 */
"d###h###to###########ur#", /* 2/11 */
"######x####m####c#f##b##", /* 2/12 */
"###pr#c#un##l#k#######h#", /* 2/13 */
"t###d#####o####bf#x#i###", /* 2/14 */
"#################m###h##", /* 2/15 */
"#####l########C#######s#", /* 2/16 */
"jgu####i#####q###k#e####", /* 2/17 */
"#####p#d######v##l#####s", /* 2/18 */
"#ke################u####", /* 2/19 */
"u####g#w####sjilT######v", /* 2/20 */
"##d#ij#########gkw######", /* 2/21 */
"####O##t####Sx######v###", /* 2/22 */
"##s##q###rp###########lN", /* 2/23 */
"####b#######v#g#########", /* 2/24 */
"###t###ra#oq###n####s###", /* 2/25 */
"#########b##c########x##", /* 2/26 */
"#j#e##q#r##ofk####cph#a#", /* 2/27 */
"####k###x##g###wndf#####", /* 2/28 */
"#########a###########B##", /* 3/1 */
"##h#l########s#ox####w##", /* 3/2 */
"#m######utg#############", /* 3/3 */
"##r##########h##d####a#f", /* 3/4 */
"###k##b#######wm####e###", /* 3/5 */
"##t##h###i##q####a###g#u", /* 3/6 */
"####is#####w#dk########b", /* 3/7 */
"######q#################", /* 3/8 */
"p######m##v###l###i#w##t", /* 3/9 */
"#q##f#u####j############", /* 3/10 */
"####c##pVa##d#####r###o#", /* 3/11 */
"##########f########u#x##", /* 3/12 */
"#g######r#c##a##j#######", /* 3/13 */
"###fs##########l#i#x#c##", /* 3/14 */
"m######b############q###", /* 3/15 */
"#r#c##t#o#As##########Eh", /* 3/16 */
"#####m##################", /* 3/17 */
"n####k##eq#h########t#pv", /* 3/18 */
"######w###########m#####", /* 3/19 */
"#t#jau#####v##p#kh######", /* 3/20 */
"##x#i#####o####F##wd####", /* 3/21 */
"#j####a#####n####v######", /* 3/22 */
"###########e####w#pkx###", /* 3/23 */
"b######v#g####m##c######", /* 3/24 */
"#d#######ap#######oni#l#", /* 3/25 */
"##vm###f#########x######", /* 3/26 */
"####t#i###w##jqa##l#f###", /* 3/27 */
"######dx#####b#wm###c###", /* 3/28 */
"#######e##########k#####", /* 3/29 */
"gw###########pl######r#c", /* 3/30 */
"#######uf#b#####j#######", /* 3/31 */
"####d#poc#######n####t#s", /* 4/1 */
"#########w#m############", /* 4/2 */
"#asr####hlu##g#q###o#e#f", /* 4/3 */
"b#s#########w##k#d##on##", /* 4/4 */
"######m#################", /* 4/5 */
"qih##s#####w##x##t######", /* 4/6 */
"#####vn##p##k###########", /* 4/7 */
"###lt###########a#h#x#o#", /* 4/8 */
"###############m#####c##", /* 4/9 */
"je#t##qs##w#plu##i######", /* 4/10 */
"##v#####r##k#a##b##dg##f", /* 4/11 */
"############o#########j#", /* 4/12 */
"##fi##lv#b###q###u#x####", /* 4/13 */
"n##########g###k######m#", /* 4/14 */
"#####x##e#####b###crdh##", /* 4/15 */
"##########l#w#g#########"  /* 4/16 */
} ;

int     days[] = {
  0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
} ;

char    *weekday[] = {
  "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", 
  "Monday"
} ;


/*
 * main(argc, argv)
 * Main body of program.
 */

main(argc, argv)
int  argc;
char  *argv[];

{
  extern void  do_nhl_sched();
  extern void  print_help();

  register int i, j;
  int          month, day;
  struct tm    *time_tm;
  long         current_date;
  char         *ptr;
  int          gm_num;          /* Num. of games from -n flag */
  char         team[CODE_LEN];  /* Team code name */
  int          num_dates;
  int          help_flag;       /* Boolean, has user requested help? */


  /* Get team flag and game number info from cmd line */
  gm_num    = 0;
  team[0]   = '\0';
  help_flag = 0;
  if ( (num_dates = get_flag_info(argc, argv, team, &gm_num, &help_flag)) < 0)
    /* Abort if there is a bad flag */
    exit (0);

  /* Game number is 0, change it to default */
  if (gm_num == 0)
    gm_num = DEF_GM_NUM;

  if (help_flag == 1) {
    /* Print help messages */
    print_help();

  } else if (num_dates == 0) {
    /* No date specified, do today's schedule */
    current_date = time((long *) NULL);
    time_tm      = localtime(&current_date);
    month        = time_tm->tm_mon+1;
    day          = time_tm->tm_mday;

    if (strcmp(team, "") == 0)
      /* No team requested, do league schedule */
      do_nhl_sched(month, day);
    else
      /* Do schedule for team requested */
      do_team_sched(team, gm_num, month, day);

  } else {
    /* Do schedule for dates specified */
    for (i = 1; i < argc; i++) {
      /* Don't interpret arg beginning with "-" for date */
      if ((argv[i][0] != '-') && (arg_to_md(argv[i], &month, &day) >= 0)) {
        if (strcmp(team, "") == 0)
          do_nhl_sched(month, day);
        else
          if (do_team_sched(team, gm_num, month, day) < 0)
            exit(0);
      }
    }
  }

  return 0;
}


/*
 * print_help()
 * Print a helpful instructions to stdout.
 */

void print_help()

{
  int i; /* Loop counter */

  fprintf(stdout, "To use:\n");
  fprintf(stdout, "   For help...                      nhl -h\n");
  fprintf(stdout, "   For daily league schedule...     nhl [mm/dd ...]\n");
  fprintf(stdout, "   For team \"upcoming\" schedule...  nhl -txxx [-nxx] [mm/dd ...]\n\n");
  fprintf(stdout, "   -txxx - Print games for single team, ");
  fprintf(stdout, "where xxx is a team code (see list)\n");
  fprintf(stdout, "   -nxx  - Print next xx games, where xx is 1-81\n");
  fprintf(stdout, "           (only works with -t option, default is 3 without -n)\n");
  fprintf(stdout, "   mm/dd - Specify date for schedule (any number of dates can be used)\n\n");
  fprintf(stdout, "   Only first -t and -n arguments will be processed -- others are ignored.\n");
  fprintf(stdout, "   If no date argument is specified, default is today.\n\n");

  fprintf(stdout, "Team codes:\n");
  for (i = 0; i < NUM_TEAMS; i++) {
    fprintf(stdout, "   %3s - %-12s", team_codes[i], nhl_teams[i]);
    if ( (i % 3) == 2 )
      fprintf(stdout, "\n");
  }

  if ( (i % 3) != 0)
    fprintf(stdout, "\n");
}


/*
 * arg_to_md(month, day)
 * Parses a date argument to month and day.  Does not check if month and
 * day numbers are valid.
 * Returns -1 on error.
 */

int arg_to_md(arg, month, day)
char arg[];
int  *month;
int  *day;

{
  int  j;
  char *ptr;

  j = strlen(arg);
  if ((j < 3) || (j > 5) || (strspn(arg, "0123456789/") != j) ||
      (arg[0] == '/') || (arg[j-1] == '/')) {
    fprintf(stderr,"Invalid date \"%s\"...format is \"mm/dd\".\n", arg);
    return -1;
  }

  *month = atoi(arg);
  ptr    = strchr(arg, '/');
  if (ptr != NULL) {
    ptr++;
    *day = atoi(ptr);
  }

  return 1;
}


/*
 * team_to_no(team)
 * Checks to see if team code (team) is a valid team code.  If it is,
 * function returns team number (starting at Boston == 0).
 * Returns -1 on error.
 */

int team_to_no(team)
char team[];

{
  register int i, team_no;

  /* Compare team against team_codes array */
  team_no = 0;
  while ( (team_no < NUM_TEAMS) &&
          (strncmp(team, team_codes[team_no], CODE_LEN) != 0) )
    team_no++;

  /* If the team is not found */
  if (team_no >= NUM_TEAMS) {
    fprintf(stderr, "Invalid team code \"");
    for (i = 0; i < CODE_LEN; i++)
      fprintf(stderr, "%c", team[i]);
    fprintf(stderr, "\"...use \"nhl -h\" to see code list.\n");
    return -1;
  } else {
    return team_no;
  }
}


/*
 * get_flag_info(argc, argv, team, gm_num, help_flag)
 * Get flags (beginning with "-") from command line.
 * Return number of non-flag arguments (not argv[0]) or -1 on error.
 * CAUTION:  This function is littered with "return" calls. :-P
 */

int get_flag_info(argc, argv, team, gm_num, help_flag)
int  argc;
char *argv[];
char team[];
int  *gm_num;
int  *help_flag;

{
  int i, z;                  /* loop variables */
  int ret_val;               /* Value to return */
  int gm_num_set, team_set;  /* Boolean */

  register int  arg_len;

  ret_val     = 0;
  gm_num_set  = 0;
  team_set    = 0;
  *help_flag  = 0;  /* 0 until help flag is encountered */

  i = 1;
  while ( (i < argc) && (*help_flag == 0) ) {
    if (argv[i][0] == '-') {
      switch (argv[i][1]) {
        case 'n':
           /*Game number flag */
          if (gm_num_set == 0) {
            arg_len = strlen(argv[i]);
            if ( (arg_len > 4) || (arg_len < 3) ||
                 (arg_len-2 != strspn(&argv[i][2], "01234567890")) ) {
              fprintf(stderr, "Invalid flag \"%s\"...game number must be 1 or 2 digits (1-81).\n", argv[i]);
              return -1;
            } else {
              if ( ((*gm_num = atoi(&argv[i][2])) > MAX_GAMES) || 
                   (*gm_num < 1) ) {
                fprintf(stderr, "Invalid flag \"%s\"...game number must be 1-%d.\n", argv[i], MAX_GAMES);
                return -1;
              }
            }
            gm_num_set = 1;
          }
          break;

       case 't':
         /* Team flag */
        if (team_set == 0) {
          if (strlen(argv[i]) == 5) {
            for (z = 0; z < CODE_LEN; z++)
              team[z] = argv[i][z+2];
          /* Check is team is a valid team code */
          if (team_to_no(team) < 0)
            return -1;
          } else {
            fprintf(stderr, "Invalid flag \"%s\"...team code should be 3 characters.\n", argv[i]);
            return -1;
          }
          team_set = 1;
        }
        break;

      case 'h':
        /* Help flag */
        *help_flag = 1;
        break;

      default:
        /* Bad flag */
        fprintf(stderr, "Invalid flag \"%s\"\n", argv[i]);
        return -1;

      }
    } else {
      ret_val++;
    }

    i++;
  }

  return ret_val;
}


/*
 * md_to_nhlday(month, day)
 * Convert the month and day info into nhl_day. Print error and return -1
 * if the date is not during the nhl season.
 * CAUTION:  This function is littered with "return" calls.
 */

int md_to_nhlday(month, day)
int month;
int day;

{
  int nhl_day;  /* Day of the nhl season */

  /* Do some basic date validation... */
  if (month > 12) {
    fprintf(stderr, "\nInvalid date \"%d/%d\"...months are from 1 - 12.\n",month,day);
    return -1;
  } else if (day == 0) {
    fprintf(stderr,"\nInvalid date \"%d/%d\"...the day can not be \"0\"\n",month,day);
    return -1;
  } else if (day > (days[month] - days[month-1])) {
    fprintf(stderr, "\nInvalid date \"%d/%d\"...there are only %d days in the month.\n", month, day, days[month] - days[month-1]);
    return -1;
  }

  /* Figure out what julian date is being requested. */
  nhl_day = days[month-1] + day;
  if ((nhl_day > LAST_DAY) && (nhl_day < FIRST_DAY)) {
    fprintf(stdout, "\nThe NHL regular season runs from 10/6 - 4/15\n");
    return -1;
  }

  /* Convert the julian date to the specific day of the NHL season... */
  if (nhl_day >= FIRST_DAY)
    nhl_day -= FIRST_DAY;
  else
    nhl_day += 86;

  return nhl_day;
}


/*
 * do_nhl_sched(month, day)
 * Print out the entire league schedule for the month and day specified.
 */

void do_nhl_sched(month, day)
int  month;
int  day;

{
  register int i, j, nhl_day;
  int          home;
  int          visitor;
  int          game_count;
  int          day_o_week;


  /* Month/Day to nhl_day code moved to md_to_nhlday */
  if ((nhl_day = md_to_nhlday(month, day)) < 0)
    return;

  day_o_week = nhl_day % 7;

  fprintf(stdout,"\nNHL schedule for %s, %d/%d...\n",
    weekday[day_o_week], month, day);

  for (i = game_count = 0; i < NUM_TEAMS; i++) {
    home = tolower(nhl_schedule[nhl_day][i]) - 'a';    /*XXX*/

    if (home >= 0 && home <= NUM_TEAMS) {
      visitor = i;
      fprintf(stdout, "     %-12s at %s", nhl_teams[visitor], nhl_teams[home]);
      if (isupper(nhl_schedule[nhl_day][i]))           /*YYY*/
        fprintf(stdout, " @ %s",neutral_sites[home]);  /*YYY*/
      fprintf(stdout, "\n");

      game_count++;
    }
  }

  /* If there are no games, see if it is a special day. */
  if (game_count == 0) {
    if (nhl_day == 123) {
      fprintf(stdout, "     All Star game in Montreal\n");
    } else {
      fprintf(stdout, "     No games scheduled");
      if ((nhl_day == 121) || (nhl_day == 122) || (nhl_day == 124))
        fprintf(stdout, "...All Star Break\n");
      else if ((nhl_day == 80) || (nhl_day == 81))
        fprintf(stdout, "...Holiday Break\n");
      else
        fprintf(stdout, "\n");
    }
  }
}


/*
 * inc_date(month, day)
 * Increment day (and month, if necessary) to the next day.
 */

void inc_date(month, day)
int  *month;
int  *day;

{
  if (*day >= days[*month] - days [*month-1]) {
    *day   = 1;
    *month = (*month % 12) + 1;
  } else {
    (*day)++;
  }
}


/*
 * do_team_sched(team, gm_num, month, day)
 * Do schedule for one team.
 * Return -1 if error, 0 otherwise.
 */

int do_team_sched(team, gm_num, month, day)
char  team[];
int  gm_num;
int  month;
int  day;

{
  int  team_no;       /* Number of nhl team (Boston == 0)  */
  int  nhl_day;       /* Day of the nhl season (10/6 == 0) */
  int  home, visitor;
  int  day_o_week;
  int  game_count;    /* Track number of games printed     */
  int  i;             /* Loop counter                      */
  char temp[40];      /* Temp. string for sprintf          */
  char temp2[2]; /*YYY*/

  team_no = team_to_no(team);   /* Validity checked in get_flag_info*/

  /* If it is between 9/26 and 10/5, rig the program to think it is 10/6
     so we can print out beginning of season schedule */
  if ( ( (month == 9) && (day > 25) ) || ( (month == 10) && (day < 6) ) ) {
    month = 10;
    day   = 6;
  }

  /* Calculate nhl_day (Day of season) */
  if ( (nhl_day = md_to_nhlday(month, day)) >= 0 )
    fprintf(stdout, "\nUpcoming %s games...\n", team_names[team_no]);

  game_count = 0;

  while ( (nhl_day >= 0) && (game_count < gm_num) ) {
    day_o_week = nhl_day % 7;

    if (nhl_schedule[nhl_day][team_no] == '#') {
      /* Team is at home (maybe), try to find it in the schedule string */
      sprintf(temp, "%c", (char) ('a' + team_no));
      sprintf(temp2, "%c", (char) ('A' + team_no));       /*YYY*/

                                                          /*v XXX*/
      if ( (visitor = strcspn(nhl_schedule[nhl_day], temp)) < NUM_TEAMS ||
           (visitor = strcspn(nhl_schedule[nhl_day], temp2)) < NUM_TEAMS) {
                                                          /*^ YYY*/
        sprintf(temp, "%s, %d/%d:", weekday[day_o_week], month, day);
        fprintf(stdout,"     %-18s", temp);
        fprintf(stdout, "%s at %s", nhl_teams[visitor], nhl_teams[team_no]);
        if (isupper(nhl_schedule[nhl_day][visitor]))      /*YYY*/
          fprintf(stdout, " @ %s",neutral_sites[team_no]); /*YYY*/
        fprintf(stdout, "\n");

        game_count++;

      } 
    } else {
      /* Team is on the road, print out info */
      home = tolower(nhl_schedule[nhl_day][team_no]) - 'a'; /*XXX*/
      sprintf(temp, "%s, %d/%d:", weekday[day_o_week], month, day);
      fprintf(stdout,"     %-18s", temp);
      printf("%s at %s", nhl_teams[team_no], nhl_teams[home]);
      if (isupper(nhl_schedule[nhl_day][team_no]))          /*YYY*/
        fprintf(stdout, " @ %s",neutral_sites[home]);       /*YYY*/
      fprintf(stdout, "\n");
   
      game_count++;

    }

    inc_date(&month, &day);
    nhl_day = md_to_nhlday(month, day);

  }

  return 0;
}
