#!/usr/local/bin/perl

# this is Perl, so don't expect any pretty coding. i'm exposing this
# code so that next year's maintainer can exploit my work for his or
# her lazy gain.
#
# Eric Tilton, tilt+@cs.cmu.edu, August/September of 1998

open(SCHEDULE, "source.txt");

# structure of day{}:
#   <day#>:{title,total,item#}
#         :title -- title of the day
#         :total -- total number of items
#         :item#:time -- time of the item
#         :item#:data -- data of the item

$day = 0;

while(<SCHEDULE>) {
    chop;
    if (/^(.+\d) +WEEK/) {
	$title = $1;

	if ($prev_day =~ /Friday/ && $title !~ /Saturday/) {
	    # add an empty saturday if needed
	    $day++;
	    $prev_day =~ /\w+, (\w+) (\d+)/;
	    # this will BREAK if the friday is the end of the month.
	    # happily, this is not my problem.
	    $day{$day . ":title"} = "Saturday, " . $1 . " " . ($2 + 1);
	}

	$day++;
	$total_days = $day;	# bogus compatibility hack

	$day{$day . ":title"} = $title;
	$prev_day = $title;

	$ignore = <SCHEDULE>;
	$count = 0;
	while (($item = <SCHEDULE>) && $item ne "\n") {
	    chop($item);
	    if ($item =~ /^\t\t(.*)/) {
		$day{$day . ":" . ($count - 1) . ":data"} .= " " . $1;
		next;
	    }
	    ($time, $data) = split("\t", $item);
	    $day{$day . ":" . $count . ":time"} = $time;
	    $day{$day . ":" . $count . ":data"} = $data;
	    $count++;
	    $day{$day . ":total"} = $count;
	}
    }
}

# add a trailing saturday if needed
if ($prev_day =~ /Friday/) {
    $day++;
    $prev_day =~ /\w+, (\w+) (\d+)/;
    # this will BREAK if the friday is the end of the month.
    # happily, this is not my problem.
    $day{$day . ":title"} = "Saturday, " . $1 . " " . ($2 + 1);
}

close(<SCHEDULE>);

# links.txt is a file which contains
#
# <string>\t<url>
#
# basically, any time <string> is found in a schedule entry,
# it should be linked to <url>.

open(LINKS, "links.txt");
while (<LINKS>) {
    ($string, $url) = split('\t');
    $links{$string} = $url;
}

close(LINKS);

######################################################################
# FOR DEBUGGING, UNUSED CURRENTLY                                    #
if ($debug) {                                                        #
for ($loop = 1; $loop <= $total_days; $loop++) {                     #
    print "===================\n";                                   #
    print "Day $loop\n";                                             #
    print $day{$loop . ":title"} . "\n";                             #
    print "----\n";                                                  #
    for ($loop2 = 0; $loop2 < $day{$loop . ":total"}; $loop2++) {    #
	print $day{$loop . ":" . $loop2 . ":time"};                  #
	print " -- ";                                                #
	print $day{$loop . ":" . $loop2 . ":data"} . "\n";           #
    }                                                                #
}                                                                    #
}                                                                    #
######################################################################

sub timetonum {
    # returns 9:00 as 0, 9:15 as 1, 9:30 as 2, etc.
    # ???? is -1.

    local($time, $index) = @_;
    local($hour, $minute, $result);

    if ($time =~ /\?/) { return(-1); }

    ($hour, $minute) = split(":", $time);
    if ($hour < 9) { $hour += 12; } # assume 1-8 is PM
    if ($index == 1 && $hour == 9 && $minute == 0) {
	$hour += 12;
    } # 9:00 in second half is probably 9pm

    $result = ($hour * 4 + ($minute / 15)) - 36;
}

sub convert {
    local($time) = @_;
    local(@time);
    local($begin, $end);

    $time =~ /([0-9:]+) - *([0-9:\?]+)/;
    $time[0] = $1;
    $time[1] = $2;

    $begin = &timetonum($time[0], 0);
    $end   = &timetonum($time[1], 1);

    return($begin, ($end == -1 ? 8 : $end - $begin));
}

open(HEADER,"header.txt");
while(<HEADER>) {
    print;
}

sub addlinks {
    # note: there's a slight chance that a URL might contain text
    # that would match the left side, which would cause this to
    # break. I'm going to hope to avoid that for now by just not
    # constructing links.txt that way; if it turns out
    # to be a problem, I'll fix it then.
    local($data) = @_;
    local($key, $scratch);
    
    foreach $key (keys %links) {
	if ($data =~ /$key/i) {
	    $scratch = $key;
	    $scratch =~ s/ /&nbsp;/;
	    $scratch =~ s/\\//g;
	    $data =~ s/$key/<A HREF=\"$links{$key}\">$scratch<\/A>/i;
	}
    }
    
    return $data;
}

#do that funky thing
for ($start = 1; $start < $total_days; $start += 6) {
    local(%grid);
    local(@title);

# generate a 6 day grid

    $earliest = 0;
    $latest   = 0;
    
    for ($loop = $start; $loop < $start + 6; $loop++) {
	$title[$loop - $start] = $day{$loop . ":title"};
	for ($loop2 = 0; $loop2 < $day{$loop . ":total"}; $loop2++) {
	    ($begin, $length) = &convert($day{$loop . ":" . $loop2 . ":time"});

	    # HACKS
	    # shameless PG hack
	    if ($day{$loop . ":" . $loop2 . ":data"} =~ /Pretty Good Race/ &&
		$day{$loop . ":" . $loop2 . ":data"} !~ /Pretty Good Race TG/)
	    {
		$length = 2;
	    }

	    # shameless labor day hack
	    if ($day{$loop . ":" . $loop2 . ":time"} =~ /LABOR DAY/) {
		$begin  = 2;
		$length = 29;
	    }

	    $scratch  = $length . "\t" . $day{$loop . ":" . $loop2 . ":time"};
	    $scratch .= "\t" . $day{$loop . ":" . $loop2 . ":data"};
	    $grid{$loop - $start,$begin} = $scratch;
	    for ($loop3 = $begin + 1; $loop3 < ($begin + $length); $loop3++) {
		$grid{$loop - $start,$loop3} = "XXX";
	    }
	    if (($begin + $length) > $latest) { $latest = $begin + $length; }
	}
    }

    
# output a 5 day grid
    $week = int(($start / 6) + 1);
    if ($week == 1) { print "<H2><A NAME=\"ONE\">Week One</A></H2>\n"; }
    if ($week == 2) { print "<H2><A NAME=\"TWO\">Week Two</A></H2>\n"; }
    if ($week == 3) { print "<H2><A NAME=\"THREE\">Week Three</A></H2>\n"; }

    print "<TABLE BORDER=2 COLS=5 CELLSPACING=1 CELLPADDING=0 WIDTH=100% BGCOLOR=\"#87CEFA\">\n";
    
    print "<tr bgcolor=\"#000066\"> <th width=10%><font color=white>Time</font></th>\n";
    
    local($title_temp);
    local($title_width);

    for ($loop = 0; $loop < 6; $loop++) {
	$title_temp = $title[$loop];

	if ($title_temp =~ /Saturday/) {
	    $title_width = "10%";
	} else {
	    $title_width = "16%";
	}

	print "<th width=" . $title_width . "> <font color=white>";
	$title_temp =~ s/Monday/Mon/;
	$title_temp =~ s/Tuesday/Tue/;
	$title_temp =~ s/Wednesday/Wed/;
	$title_temp =~ s/Thursday/Thu/;
	$title_temp =~ s/Friday/Fri/;
	$title_temp =~ s/Saturday/Sat/;
	$title_temp =~ s/August/Aug/;
	$title_temp =~ s/September/Sep/;
	print $title_temp;
	print "</font></th>\n";
    }
    
    print "</tr>\n";
    
    for ($loop = $earliest; $loop < $latest; $loop++) {
	print "<!-- $loop -->\n";
	print "<tr>";
	unless ($loop % 4) {
	    print "<td width=10% bgcolor=\"#DDDDFF\" valign=top align=left>";
	    $hour = int(($loop / 4) + 9);
	    if ($hour > 12) {
		$meridian = "pm";
		$hour -= 12;
	    } else {
		$meridian = "am";
	    }
	    print "$hour:00 $meridian";
	    print "</td>\n";
	} else {
	    print "<td width=10%>&nbsp;</td>\n";
	}

	local($entry_width);
	for ($loop2 = 0; $loop2 < 6; $loop2++) {
	    if ($loop2 == 5) {
		$entry_width = "10%";
	    } else {
		$entry_width = "16%";
	    }

	    if ($grid{$loop2, $loop} eq "XXX") {
		# print nothing
	    } elsif ($grid{$loop2, $loop}) {
		($length, $time, $data) = split("\t", $grid{$loop2, $loop});

		print "<td ";
		if (   $data =~ /lunch/i   || $data =~/living in pitt/i
		    || $data =~ /party/i   || $data =~ /TG/
		    || $data =~ /brewing/i || $data =~ /pretty good/i
		    || $data =~ /pub crawl/i || $data =~ /pirates game/i
		    || $data =~ /reception/i || $data =~ /scavenger/i ) {
		    print "bgcolor=\"#6495ED\"";
		} elsif ($time =~ /LABOR DAY/) {
		    print "bgcolor=\"#EEEEFF\"";
		}
		print "width=" . $entry_width . " valign=top align=left rowspan=" . $length . ">\n";
		print "<b>" . $time . "</b><br>\n";
		$data = &addlinks($data);
		print $data;
		print "</td>\n";
	    } else {
		print "<td bgcolor=\"#EEEEFF\" width=" . $entry_width . ">&nbsp;</td>";
	    }
	}
	print "</tr>\n";
    }
    print "</table>";
    
}

# footer
print <<"COLOPHON";
<HR>
<H2>Colophon</H2>

<P>This document is generated automatically from the
<A HREF=\"schedule.txt\">text version of the schedule</A>.
<A HREF=\"GENERATE/generate.pl\">Various and
diverse vile Perl and HTML hacks were employed</A>, in order to
(hopefully) achieve a good looking as well as functionally useful
guide. As with any automated process, there may be occasional
glitches\; we\'ll try to fix them as soon as we spot them or have
them pointed out to us. Finally, if you find a bad or sub-optimal link
on these pages, please point them out. (You can contact Eric at <A
HREF=\"mailto:tilt+@cs.cmu.edu\">tilt+@cs.cmu.edu</A>.)

COLOPHON
    ;

print "<HR>\n";

print "<P>Last generated: ";

require "ctime.pl";
print &ctime(time);

print "<BR>\n";

print "<A HREF=\"http://www.cs.cmu.edu/~tilt/\">Eric Tilton &lt;tilt+@cs.cmu.edu&gt;</A></P>\n";

print "</body></html>\n";

