#!/usr/bin/perl

# this code is so bad it doesn't deserve a licence of any kind.
# so, uh...it's public domain, license free, royalty free
# use it at your own risk code

use Date::Manip;
#use Class::Date qw(:errors date localdate gmdate now);
$sqlPreamble = "INSERT into Game (awayTeamID,homeTeamID,gameDate,gameWeek) values ";
$teams{"Arizona"} = 1;
$teams{"N.Y. Giants"} = 2;
$teams{"Baltimore"} = 3;
$teams{"Pittsburgh"} = 4;
$teams{"Carolina"} = 5;
$teams{"Washington"} = 6;
$teams{"Chicago"} = 7;
$teams{"Minnesota"} = 8;
$teams{"Detroit"} = 9;
$teams{"New Orleans"} = 10;
$teams{"Indianapolis"} = 11;
$teams{"Kansas City"} = 12;
$teams{"Jacksonville"} = 13;
$teams{"Cleveland"} = 14;
$teams{"N.Y. Jets"} = 15;
$teams{"Green Bay"} = 16;
$teams{"Philadelphia"} = 17;
$teams{"Dallas"} = 18;
$teams{"San Diego"} = 19;
$teams{"Oakland"} = 20;
$teams{"San Francisco"} = 21;
$teams{"Atlanta"} = 22;
$teams{"Seattle"} = 23;
$teams{"Miami"} = 24;
$teams{"Tampa Bay"} = 25;
$teams{"New England"} = 26;
$teams{"Tennessee"} = 27;
$teams{"Buffalo"} = 28;
$teams{"Denver"} = 29;
$teams{"St. Louis"} = 30;
$teams{"Cincinnati"} = 31;
$teams{"Houston"} = 32;

open (INPUT, "input.txt");

$gotdate = false;
$date;
$realdate;
while(<INPUT>)
{
	if ($_ =~ /^WEEK/)
	{
		# week detected
		($toss,$week) = split / /;
		print "# Week $week\n";
		chomp $week;
		$gotdate = false;

	}
	elsif ($_ =~ /^Monday|^Sunday|^Saturday|^Friday|^Thursday/)
	{
		$date = ParseDate($_);
		  if (! $date) {
    		die "BAD DATE!";
		  }
		#print &UnixDate($date,"It is now %A.");
		chomp $date;
		#print "Found a day for games! ".$date."\n";
		$gotdate = true;

	}
	elsif ($_ =~ /^$/)
	{
		print "# blank line\n";
	}
	else
	{
		chomp $_;
		#print "** $week $date $_\n";
		($away,$home,$time) = split /---|, /;
		($hour,$minutes) = split (/:| /,$time);
		# INSERT into Game (awayTeamID,homeTeamID,gameDate,gameWeek) values (15,6, '2003-09-04 22:00:00',1);
		print $sqlPreamble . "(". $teams{$away}.",".$teams{$home}.",". &UnixDate($date,"'%Y-%m-%d ").($hour+12).":$minutes:00',$week);\n";
	}
}

close INPUT;