#!/usr/bin/perl

# 6.837
# Created by Seth Teller and JP Mellor for Fall 1995
# Modified by KB (kaybee@graphics.lcs.mit.edu) for Fall 1996
# Modified by JL (jslevene@graphics.lcs.mit.edu) for Fall 1997


# Make an associative array to look up month characters to value use it
# in pack.  Fix test in sort?  Expand names.  Generate HTML.


require "6837-cgilib.pl";

%longmonth = (
 'jan', 'January',
 'feb', 'February',
 'mar', 'March',
 'apr', 'April',
 'may', 'May',
 'jun', 'June',
 'jul', 'July',
 'aug', 'August',
 'sep', 'September',
 'oct', 'October',
 'nov', 'November',
 'dec', 'December');

%valmonth = (
 'jan', '01',
 'feb', '02',
 'mar', '03',
 'apr', '04',
 'may', '05',
 'jun', '06',
 'jul', '07',
 'aug', '08',
 'sep', '09',
 'oct', '10',
 'nov', '11',
 'dec', '12');

$locpath="/u2/graphics/WWW/classes/6.837/F97/lectures";
$lecpath="/u2/graphics/WWW/classes/6.837/F97/lectures";

# Print the header
print &PrintHeader;
print &HtmlTop ("6.837 Lectures");
print "<hr>";


if (!-e "$locpath/lectures.html" || -M "$locpath" < -M "$locpath/lectures.html")
{

    
    if (-e "$locpath/lectures.html") {
	system 'rm $locpath/lectures.html';
    }
    
    
    opendir(THISDIR, "$lecpath");
    @allfiles = grep(/^.*97.*\.ps$/, readdir(THISDIR));
    closedir(THISDIR);
    

    
    $count = 0;
    
    foreach $file (@allfiles) {

	($titledate, @ext[$count]) = split(/\./, $file);
	(@lecnum[$count], $date, @title[$count]) = split(/_/, $titledate);
	
	@temp = unpack("a2a3a2", $date);
	@date[$count] = pack("a2a2a2", @temp[2], $valmonth{@temp[1]}, @temp[0]); 
	$tmpval = @date[$count];


	$dcount = 0;
	while (@dateindex[$tmpval . $dcount] ne "")
	{

	    $dcount = $dcount + 1;
	}
	

	@dateindex[$tmpval . $dcount] = $count;

	@year[$count] = @temp[2];
	@month[$count] = $longmonth{@temp[1]};
	@day[$count] = @temp[0];
#	@title[$count] =~ s/_/ /g;
	$count++;
    }
    $count--;
    

    @sorteddate = sort (@date);
    
    if (!open(LECTURES, "> $locpath/lectures.html"))
    {
	print "Can't open lectures.html: $!\n";
	exit;
    }

    print LECTURES "<center><img src=http://graphics.lcs.mit.edu/classes/6.837/F97/gifs/6.837_new.gif></center><p><h1>Lectures</h1><p>";

    if (!open(LECTURES, ">> $locpath/lectures.html"))
    {
	print "Can't open lectures.html: $!\n";
	exit;
    }

    if ($count == -1) {
	print LECTURES "There are no lectures available now. Try again later.\n";
    } else {
	
	$lastdate = "wrong answer";
	foreach $ind (@sorteddate) {



	    if ($ind eq $lastdate)
	    {

		$datecount = $datecount + 1;
	    }
	    else
	    {
		$datecount = 0;
	    }


	    $index = @dateindex[$ind . $datecount];
		
	    print LECTURES "\n<li><a href=\"http://graphics.lcs.mit.edu/classes/6.837/F97/lectures/@allfiles[$index]\">\n";
	    print LECTURES "@lecnum[$index] (@day[$index] @month[$index] @year[$index]):  @title[$index]</a>\n";
	    print LECTURES "</li><p>\n";
	    
	    $lastdate = $ind;
	    
	}
    }


    print LECTURES "<hr>This page is generated automatically on-demand.<p>\n";
    print LECTURES "If you experience difficulty please contact 6.837-staff\@mit.edu<p>\n";


}

if (!open(LECTURES, "< $locpath/lectures.html"))
{
    print "Can't open lectures.html: $!\n";
    exit;
}


while (<LECTURES>) {
 print;
}

close(LECTURES);
chmod 0664, "$locpath/lectures.html"
