#!/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, August of 1999
#

require "links.pl";

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

$numRows = 48;

$headerFile = "header.html";
$footerFile = "footer.html";
$outFile = "sched.html";
if ($ARGV[0]) {
    $headerFile = "header-$ARGV[0].html";
    $footerFile = "footer-$ARGV[0].html";
    $outFile = "sched-$ARGV[0].html";
}

# 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



sub readsched {
    local($sourceFile) = @_;

    open(SCHEDULE, $sourceFile);

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

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) {
        # assume 1-8 is PM
        $hour += 12;
    }
    if ($index == 1 && $hour == 9 && $minute == 0) {
        # 9:00 in second half is probably 9pm
        $hour += 12;
    }

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

    if (($end != -1) && ($end < $begin)) {
        # probably guessed wrong about am/pm
        $end=$end+12*4;
    }

    if ($end == -1) {
        # special case of time-unbounded parties
        $end=$begin+8;
    }
    if ($end > $numRows) {
        # special case of LONG events
        $end = $numRows;
    }

    return($begin, $end - $begin);
}

sub printsched {
    local ($webDir, $outFile, $headerFile, $footerFile) = @_;

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

    &readlinks();

    ## Header

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

    ## Do that funky thing

    print("<table width=100% border=0 cellspacing=0 cellpadding=0>\n");

    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 = $numRows-12;
                }

                $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);
        $last_week = int(($total_days / 6) + 1);
        if ($week == 1) {
            print "<tr><td bgcolor=#eeeeee><font face=\"Arial, Helvetica\">";
            print "<b><a name=\"ONE\">Week One</a></b>";
            if ($last_week > 1) {
                print " | <a href=\"#TWO\">Week Two</a>";
            }
            if ($last_week > 2) {
                print " | <a href=\"#THREE\">Week Three</a>";
            }
            print " | <a href=\"#top\">Top</a></font></td></tr>\n<tr height=6><td></td></tr>\n";
        }
        if ($week == 2) {
            print "<tr><td bgcolor=#eeeeee><font face=\"Arial, Helvetica\">";
            print "<a href=\"#ONE\">Week One</a>";
            print " | <b><a name=\"TWO\">Week Two</a></b>";
            if ($last_week > 2) {
                print " | <a href=\"#THREE\">Week Three</a>";
            }
            print " | <a href=\"#top\">Top</a></font></td></tr>\n<tr height=6><td></td></tr>\n";
        }
        if ($week == 3) {
            print "<tr><td bgcolor=#eeeeee><font face=\"Arial, Helvetica\">";
            print "<a href=\"#ONE\">Week One</a>";
            print " | <a href=\"#TWO\">Week Two</a>";
            print " | <b><a name=\"THREE\">Week Three</a></b>";
            print " | <a href=\"#top\">Top</a></font></td></tr>\n<tr height=6><td></td></tr>\n";
        }

        print "<tr><td><TABLE BORDER=0 COLS=5 CELLSPACING=2 CELLPADDING=1 WIDTH=100%>\n";
        
        print "<tr bgcolor=\"#752023\"> <th width=10%><font face=\"Arial,Helvetica\" size=-1 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 face=\"Arial,Helvetica\" size=-1 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=\"#E2D0D0\" valign=top align=left>";
                $hour = int(($loop / 4) + 9);
                if ($hour >= 12) {
                    $meridian = "pm";
                    if ($hour > 12) {
                        $hour -= 12;
                    }
                } else {
                    $meridian = "am";
                }
                print "$hour:00 $meridian";
                print "</td>\n";
            } else {
                print "<td width=10% bgcolor=\"#E2D0D0\">&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 
                        || $data =~ /potluck/i || $data =~ /happy hour/i )
                    {
                        print "bgcolor=\"#CC7676\" ";
                    } elsif ( $data =~ /break/i ) {
                        print "bgcolor=\"#C4A39D\" ";
                    } elsif ($time =~ /LABOR DAY/) {
                        print "bgcolor=\"#DDC3C3\" ";
                    } else {
                        print "bgcolor=\"#E2BEBE\" ";
                    }
                    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=\"#E5D8D7\" width=" . $entry_width . ">&nbsp;</td>";
                }
            }
            print "</tr>\n";
        }
        print "</table></td></tr>\n<tr height=12><td></td></tr>";
        
    }

    print "<tr><td bgcolor=#eeeeee><font face=\"Arial, Helvetica\">";
    print "<a href=\"#ONE\">Week One</a>";
    if ($last_week > 1) {
        print " | <a href=\"#TWO\">Week Two</a>";
    }
    if ($last_week > 2) {
        print " | <a href=\"#THREE\">Week Three</a>";
    }
    print " | <a href=\"#top\">Top</a></font></td></tr>\n<tr height=6><td></td></tr>\n";


## Footer

    print "<tr height=16><td></td></tr>\n<tr><td><P>Last generated: ";

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

    print "<BR>\n";

    print "<A HREF=\"http://www.cs.cmu.edu/~seth/\">Seth Copen Goldstein &lt;seth+\@cs.cmu.edu&gt;</A></P></td></tr>\n";

    print "</table>\n";

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


&readsched("source.txt");
&printsched($webDir, $outFile, $headerFile, $footerFile);

######################################################################
# 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";           #
    }                                                                #
}                                                                    #
}                                                                    #
######################################################################

