#!/usr/bin/perl
########################################################
# TodayGames for FHL
# v 2.0
#
# (c) 2002,2003 Patrick Pellegrino, All rights reserved.
##
# Email: _john_9@yahoo.com
# Website: Soon
##
# A big thanx to Eric Vaillancourt from MagieTec
# www.magietec.com for the HTML creation.
##
# Thank Zubial for your support
########################################################
# Since version 1.0 
#
# New feature :
#
# - Added multiple day of simming
#   Thanks Martin Richer (rangers@quixnet.net) for 
#   sharing your work. 
#
# Bug corrected :
#
# - Bug that all goalies incorrect when a team do not
#   have one for the game   
#
# - Output the games left to right
# 
########################################################

# -- Configuration section
# Sets the name for your league's Today file
$TODAY_FILE = "EHLS5Schedule.html";
# Sets the name for your league's Standing file
$STANDING_FILE = "EHLS5Standings.html";
# Sets the number of days you're simming
$DAYS_SIM = 4;
###



##
# -- Pics configuration
# If needed change the team name to how it appear in the standing.
# If you have a NON NHL based league, you need to change anything.
# Keep the NONHL as is.
##
%pics = (
"QUEBEC" => "QUE.gif",
"ATLANTA" => "ATL.gif",
"COLORADO" => "COL.gif",
"BOSTON" => "BOS.gif",
"BUFFALO" => "BUF.gif",
"NORTH STAR" => "MNS.gif",
"CALGARY" => "CGY.gif",
"HARTFORD" => "HRT.gif",
"CHICAGO" => "CHI.gif",
"DALLAS" => "DAL.gif",
"DETROIT" => "DET.gif",
"EDMONTON" => "EDM.gif",
"FLORIDA" => "FLO.gif",
"LA KINGS" => "LA.gif",
"MINNESOTA" => "MIN.gif",
"MONTREAL" => "MTL.gif",
"WINNIPEG" => "WIN.gif",
"NEW JERSEY" => "NJ.gif",
"ISLANDERS" => "NYI.gif",
"RANGERS" => "NYR.gif",
"OTTAWA" => "OTT.gif",
"PHILLY" => "PHI.gif",
"PHOENIX" => "PHX.gif",
"PITTSBURGH" => "PIT.gif",
"SAN JOSE" => "SJ.gif",
"ST LOUIS" => "STL.gif",
"TAMPA BAY" => "TB.gif",
"TORONTO" => "TOR.gif",
"VANCOUVER" => "VAN.gif",
"WASHINGTON" => "WSH.gif",
"NONHL" => "Space.gif");
##
# -- Abbreviation configuration
# If needed change the team name to how it appear in the standing.
# Change the abbreviation to how you entered it in FHL.
##
%abrev = (
"ANAHEIM" => "Ana",
"ATLANTA" => "Atl",
"COLORADO" => "Col",
"BOSTON" => "Bos",
"BUFFALO" => "Buf",
"COLUMBUS" => "Cbj",
"CALGARY" => "Cal",
"CAROLINE" => "Car",
"CHICAGO" => "Chi",
"DALLAS" => "Dal",
"DETROIT" => "Det",
"EDMONTON" => "Edm",
"FLORIDE" => "Flo",
"LA KINGS" => "LA ",
"MINNESOTA" => "Min",
"MONTREAL" => "Mon",
"NASHVILLE" => "Nas",
"NEW JERSEY" => "New",
"ISLANDERS" => "Isl",
"RANGERS" => "Ran",
"OTTAWA" => "Ott",
"PHILLY" => "Phi",
"PHOENIX" => "Pho",
"PITTSBURGH" => "Pit",
"SAN JOSE" => "San",
"ST LOUIS" => "Stl",
"TAMPA BAY" => "Tam",
"TORONTO" => "Tor",
"VANCOUVER" => "Van",
"WASHINGTON" => "Was");
###

# -- Program section - Do not edit		  
# -- Retrieve the teams record
&GetTeamInfo();
# -- Grab all game data
&GrabGame();
# -- Generate the next game section
&NextGame();
# -- Create the new file
&FileOut();
####

#####
# GrabGame
#
# Retrieve all the game info needed and create the HTML
#
#####
sub GrabGame {
	
	open (TODAYS, "< $TODAY_FILE") or die($!);
	
	$last_day = 0 ;
		
	# Read all line of Today games	
	while (<TODAYS>) {
			
		$line = $_;
			
		#Day 1 <BR>
		if ($line =~ /^.*Day\s*(\d*)\s*\<BR\>.*/)
		{
			$day_number = $1;
			if ($last_day < $day_number)
			{
				$last_day = $day_number ;
			}
		}

		
		#<A HREF=LHSSF2000679.html> 679   LA Kings 7  Anaheim 2 </A><BR>
		if (m/^<A HREF.(\w*.\w+)>.(\d+).*/){
			push (@{$GamesPlayed{$day_number}}, "$1,$2") ;
			
			if ($max_day < $day_number)
			{
				$max_day = $day_number ;
			}
		}
		# 960   Pittsburgh at Philly<BR>
		elsif (m/^.(\d+)\s*(\w*.*) at \s*(\w*.*)<BR>/){
			push (@{$NextGames{$day_number}} , "$1,$2,$3");
		
		}
		#<P>Games of vendredi, mars 30 2001</P>
		elsif (m/^<P>(\w*.*)<\/P>/){
			$GameDay = $1;
		}	
	}
	
	close TODAYS;
		
	# For all games of the day we find and grab all the data
	for ($day_number = ($max_day - ($DAYS_SIM -1)) ; $day_number <= $max_day ; $day_number++) {
		
		$z = scalar(@{$GamesPlayed{$day_number}} );
		
		for ($y = 0 ; $y < $z; $y++) {
		
			($gm,$gm_nb) = split(/,/,$GamesPlayed{$day_number}[$y]);
		
			$goal_a = 0;
			$goal_h = 0;
			$g_away = "";
			$g_home = "";
		
			open (BOXSCORE,"< $gm")or die($!);
	
			while (<BOXSCORE>) {
		
				  $line = $_;

			  	  # Team section
			  	  # <FONT SIZE -1><H3>Rangers at Vancouver</H3>
			  	  if (m/^<FONT SIZE -1><H3>\s*(\w*.*) at \s*(\w*.*)<\/H3>/)
			  	  {
				   	 $AWAY = uc($1);
				  	 $AW_ABREV = uc($abrev{$AWAY});
				 	 $HOME = uc($2);
				 	 $HM_ABREV = uc($abrev{$HOME});
			  	  }
			
				  # Goal section
				  #1. ISLANDERS, WARRINER 7 (OHLUND, LINDSAY), 02:52<BR>
				  if (m/^\d+.\s*(\w*.*)\,.\s*(\w*.*)\s+(\d+).\(.*\)\,.*/){
			   	  	 if ($1 eq $AWAY) {
			   	  	 	if (defined($sc_away{$2})){
				  	 	   $sc_away{$2} .= ",$3";
				  		}
				  		else {$sc_away{$2} .= "$3";}
						$goal_a++;
				  	 }
			   		 else {
			   		 	if (defined($sc_home{$2})){ $sc_home{$2} .= ",$3";}
						else {$sc_home{$2} .= "$3";}
						$goal_h++;
			   		}
		          }
			
				  # Goalers section
				  # Tomas Vokoun (Ran), 14 saves out of 19 shots,  W, 5-4-1<BR>
				  if ( $line =~ m/(\S+ \S+) \((.+)\)\, (\d+) saves out of (\d+) shots(\S+.+)<BR>/ ){
			   	  	 if (uc($2) eq $AW_ABREV) {$g_away = "$1 $3 saves$5";}
			   		 elsif (uc($2) eq $HM_ABREV){$g_home = "$1 $3 saves$5";}
				  }
			}
			close BOXSCORE;
		
			# Use the space.gif if we don't find the team logo (NON NHL based league)
			if (defined($pics{$AWAY})) {$AW_PIC = $pics{$AWAY};}
			else {$AW_PIC = $pics{"NONHL"};}

			if (defined($pics{$HOME})) {$HM_PIC = $pics{$HOME};}
			  else {$HM_PIC = $pics{"NONHL"};}
		
			  # HTML header for the game and for the AWAY Team
			  $output .= qq|<table border=\"0\" width=\"350\" cellpadding=\"0\" cellspacing=\"0\">
        	  <tr height=\"28\"> 
          	  <td background=\"pics\/B_up.gif\"><img src=\"pics\/space.gif\" border=\"0\" width=\"20\" height=\"1\" alt=\"\"><font size=\"2\" color=\"\#ffffff\"><b>Game $gm_nb<\/b><\/font><\/td>
        	  <\/tr>
        	  <tr> 
          	  <td background=\"pics\/B_Middle.gif\"> <div align=\"center\"> 
              <table border=\"0\" width=\"320\" cellpadding=\"0\" cellspacing=\"0\">
                <tr> 
                  <td width=\"300\"><img src=\"pics\/Logos\/$AW_PIC\" border=\"0\" width=\"25\" height=\"25\" alt=\"\"> 
                    <font face=\"Times New Roman\"><b>$AWAY<\/b><\/font><\/td>
                  <td width=\"20\"><font face=\"Arial\"><b>$goal_a<\/b><\/font><\/td>
                <\/tr>
              <\/table>
              <\/div><\/td>
        	  <\/tr>
        	  <tr> 
          	  <td background=\"pics\/B_Middle.gif\"> 
              <img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\"><font size=\"2\">|;
		
			  # Build the HTML for the scorer section - AWAY Team
			  ############
			  $AW_SCORER = "";
			  # For each scorer in this game AWAY Team
			  foreach $la (keys %sc_away){
			
			  	$n_len = length "$AW_SCORER";
			  	$c_len = length "$la($sc_away{$la}";
			  	# Verify if we got any space for the next scorer
			  	if (($n_len + $c_len) < 40 ) {$AW_SCORER .= "$la($sc_away{$la}) ";}
			  	# Else put it on a next line
			  	#else {$output .= $AW_SCORER; $AW_SCORER = ""; $AW_SCORER .= "<BR><img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">$la($sc_away{$la}) ";}
			  	else {$output .= $AW_SCORER; $output .= "<BR><img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">"; $AW_SCORER = ""; $AW_SCORER .= "$la($sc_away{$la}) ";}
			 }
			 
			 # If no goal is scored
			 if ($AW_SCORER eq "") {
		   	 $AW_SCORER .= "NO SCORING";
			 }
			 
			 # Apply the HTML scorer section to output
			 $output .= $AW_SCORER;
			 #############
		
			 # HTML header for the HOME Team
			 $output .= qq|<br>
             <img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">$g_away<\/font>
	 		 <\/td>
        	 <\/tr>
        	 <tr> 
          	 <td background=\"pics\/B_Middle.gif\"> <div align=\"center\"> 
              <table border=\"0\" width=\"320\" cellpadding=\"0\" cellspacing=\"0\">
                <tr> 
                  <td width=\"300\"><img src=\"pics\/Logos\/$HM_PIC\" border=\"0\" width=\"25\" height=\"25\" alt=\"\"> 
                    <font face=\"Times New Roman\"><b>$HOME<\/b><\/font><\/td>
                  <td width=\"20\"><font face=\"Arial\"><b>$goal_h<\/b><\/font><\/td>
                <\/tr>
             <\/table>
             <\/div><\/td>
        	 <\/tr>
        	 <tr> 
          	 <td background=\"pics\/B_Middle.gif\"> 
             <img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\"><font size=\"2\">|;

			 # Build the HTML for the scorer section - HOME Team
			 #############
			 $HM_SCORER = "";
			 # For each scorer in this game HOME Team
			 foreach $la (keys %sc_home){
			
			 	$n_len = length "$HM_SCORER";
				$c_len = length "$la($sc_home{$la}";
				# Verify if we got any space for the next scorer
				if (($n_len + $c_len) < 40 ) {$HM_SCORER .= "$la($sc_home{$la}) ";}
				# Else put it on a next line
				#else {$output.= $HM_SCORER; $HM_SCORER = ""; $HM_SCORER .= "<BR><img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">$la($sc_home{$la}) ";}
				else {$output.= $HM_SCORER; $output .= "<BR><img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">"; $HM_SCORER = ""; $HM_SCORER .= "$la($sc_home{$la}) ";}
			 }
			 # If no goal is scored
			 if ($HM_SCORER eq "") {
		  	 	$HM_SCORER .= "NO SCORING";
			 }
			 
			 # Apply the HTML scorer section to output
			 $output .= $HM_SCORER;
			 #############
		
			 # HTML for the boxscore link
			 $output .= qq|<br>
             <img src=\"pics\/space.gif\" border=\"0\" width=\"50\" height=\"1\" alt=\"\">$g_home<\/font>
	 		 <\/td>
        	 <\/tr>
        	 <tr height=\"25\"> 
          	 <td background=\"pics\/B_down.gif\"> <div align=\"center\"> 
              <table width=\"320\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
                <tr> 
                  <td width=\"290\">&nbsp;&nbsp;<\/td>
                  <td width=\"30\" valign=\"top\"><A HREF=\"$gm\"><font size=\"2\" color=\"\#000000\"><b>Boxscore<\/b><\/font><\/A><\/td>
                <\/tr>
              <\/table>
             <\/div><\/td>
        	 <\/tr>
      		 <\/table><br>
	  		 |;
		
			 # Verify in which column we put the game
			 if (($y % 2) == 0 ) {
		   	 	$col1 .= $output;
			 }
			 else {
		   	 	$col2 .= $output;
			 }
		
			 # Delete all data of the game for the next game
			 foreach $ldel (keys %sc_away){
			 	delete($sc_away {$ldel});
			 }
			 foreach $ldel (keys %sc_home){
			 	delete($sc_home {$ldel});
			 }
			 
			 $goal_a = 0;
			 $goal_h = 0;
			 $output = "";
	}
}

#####
# NextGame
#
# Create the next game section. 
#
#####
sub NextGame {
	
	# Next game HTML header
	$suf .= qq|<br><br><br><table border=\"0\" width=\"500\" cellpadding=\"0\" cellspacing=\"0\">
  <tr> 
    <td background=\"pics\/NG_up.gif\" height=\"30\"><img src=\"pics\/space.gif\" border=\"0\" width=\"20\" height=\"1\" alt=\"\"><font face=\"Times New Roman\" color=\"\#ffffff\" size=2><b>NEXT 
      GAMES<\/b><\/font><\/td>
  <\/tr>
  <tr> 
    <td background=\"pics\/NG_Middle.gif\"> 
      <div align=\"center\"> 
        <table width=\"480\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">|;
	
	# How many games for the next day
	
	# Until we have game
	if (($max_day + $DAYS_SIM) > $last_day) {$max_day = $last_day - $DAYS_SIM;}
	for ($gameday = $day_number ; $gameday <= ($max_day + $DAYS_SIM) ; $gameday++) {
		
		$suf .= qq|<tr><td colspan="6"><b>Day $gameday</b></td></tr>|;
		
		# How many games for the next day
		$s = scalar(@{$NextGames{$gameday}});
		
		for ($a = 0; $a < $s; $a++) {
			($ngm,$aw,$hm) = split(/,/,$NextGames{$gameday}[$a]);
		
			# Use the space.gif if we don't find the team logo (NON NHL based league)
			if (defined($pics{uc($aw)})) {$AW_PIC = $pics{uc($aw)};}
			else {$AW_PIC = $pics{"NONHL"};}

			if (defined($pics{uc($hm)})) {$HM_PIC = $pics{uc($hm)};}
			else {$HM_PIC = $pics{"NONHL"};}
		
			# Build the HTML for the next game section
			$suf .= qq|<tr> 
            <td width=\"40\" height=\"25\">$ngm<\/td>
            <td width=\"125\"><img src=\"pics\/Logos\/$AW_PIC\" border=\"0\" width=\"25\" height=\"25\" alt=\"\"> 
              <A HREF=\"$team_standing->{$aw}->{'URL'}\"><font color=\"\#000000\">$aw<\/font><\/A><\/td>
            <td width=\"80\" height=\"25\">$team_standing->{$aw}->{'Record'}<\/td>
            <td width=\"40\" height=\"25\">@<\/td>
            <td width=\"110\" height=\"25\"><img src=\"pics\/Logos\/$HM_PIC\" border=\"0\" width=\"25\" height=\"25\" alt=\"\"> 
              <A HREF=\"$team_standing->{$hm}->{'URL'}\"><font color=\"\#000000\">$hm<\/font><\/A><\/td>
            <td width=\"80\" height=\"25\">$team_standing->{$hm}->{'Record'}<\/td>
          	<\/tr>|;		
		
		}
    }
	# Last HTML needed
    $suf .= qq|<\/table>
      <\/div><\/td>
  <\/tr>
  <tr> 
    <td background=\"pics\/NG_down.gif\" height=\"60\">&nbsp;<\/td>
  <\/tr>
<\/table>|;

}
}
#####
# FileOut
#
# Use the template file to create the TodayGames.html file
#
#####
sub FileOut {
	open (TEMPLATE,"< Template.html")or die($!);
	open (NEW_TODAY,"> TodayGames.html")or die($!);
	while (<TEMPLATE>){
	  my $line = $_;
	  $line =~ s/DATE/$GameDay/;
  	  $line =~ s/COL1/$col1/;
  	  $line =~ s/COL2/$col2/;
	  $line =~ s/NEXTGAME/$suf/;
	  print NEW_TODAY $line;
	}
	close TEMPLATE;
	close NEW_TODAY;
}

#####
# GetTeamInfo
#
# Retrieve the team URL and the team record for the next game section
# 
#####
sub GetTeamInfo {

	open (STANDING, "< $STANDING_FILE")or die($!);

	while (<STANDING>){
	
		  $line = $_;
	
		  # <A HREF=LHSSF2001Rosters.html#Toronto   >Toronto</A>        4   3  0  1    7  11   4  .875   1  0  1   1  0  0   0  0  1   3-0-1  W1
		  if ( $line =~ m/<A HREF.(\w*.\w*.*)>(\w*.*)<\/A>.\s*(\d*)\s*(\d*)\s*(\d*)\s*(\d*).*?([W|L|T])(\d*)/ ){
		  	 $team_standing->{$2}->{'URL'} = $1;
		  	 $team_standing->{$2}->{'Record'} = "$4-$5-$6";
			 
			 # Not used		  	
			 #$team_standing->{$2}->{'Streak_k'} = $7;
		  	 #$team_standing->{$2}->{'Streak_n'} = $8;
	      }
	}
}