#!/usr/local/bin/perl5

# 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
# Seth Copen Goldstein, seth@cs.cmu.edu, August of 1999
# Spiros Papadimitriou, spapadim+@cs.cmu.edu, pilot-link version
#

$year=1999;

$webDir = "/afs/cs/user/seth/public_html/IC99";

$headerFile = "header.datebook";
$footerFile = "footer.datebook";
$outFile = "ic99.datebook";

close(STDOUT);
open(STDOUT, ">$webDir/$outFile");


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);

######################################################################
# FOR DEBUGGING, UNUSED CURRENTLY                                    #
#$debug=1;
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 timeFix {
    local($time, $index) = @_;
    local($hour,$minute);

    if ($time =~ /\?/) {
        return "23:59";
    }

    ($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

    return "$hour:$minute";
}

sub getTimes {
    local($date, $time) = @_;
    local(@time);
    local(@date);
    local($timeStr);

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

    $date =~ /([A-Za-z]+), *([A-Za-z]+) *([0-9]+)/;
    $date[0] = $2;
    $date[1] = $3;

    if (($time[0] eq "") || ($time[1] eq "")) {
        $timeStr = "$date[0] $date[1], $year\t$date[0] $date[1], $year";
    } else {
        $timeStr  = "$date[0] $date[1], $year $time[0] GMT-500";
        $timeStr .= "\t$date[0] $date[1], $year $time[1] GMT-500";
    }

    return $timeStr;
}

open(HEADER,$headerFile);
while(<HEADER>) {
    print;
}

for ($d = 1; $d <= $total_days; $d++) {
    local($dayTitle, $eventTime, $eventData);
    $dayTitle =  $day{$d . ":title"};
    for ($ev = 0; $ev < $day{$d . ":total"}; $ev++) {
        local($timesStr, $eventStr);

        $eventTime = $day{$d . ":" . $ev . ":time"};
        $eventData = $day{$d . ":" . $ev . ":data"};

        if ($eventData =~ /break/i || $eventTime =~ /LABOR DAY/) {
            next;
        }
        $timesStr = &getTimes($dayTitle, $eventTime);
        $eventStr = "$timesStr\t\t$eventData\n";

        print $eventStr;
    }
}

open(FOOTER,$footerFile);
while(<FOOTER>) {
    print;
}

