#! /usr/local/bin/perl

# 6.837
# First Created by JP Mellor for Fall 1995
# Modified by KB (kaybee@graphics.lcs.mit.edu) for Fall 1996
# Modified again by Justin Legakis for Fall 1996
# KB fixed count bug 
# JL reorganized output
# Modified by JL (jslevene@graphics.lcs.mit.edu) for Fall 1997

chdir "/u2/graphics/WWW/classes/6.837/F97/data/";

if (!-e "directory.html" || -M "." < -M "directory.html") {

if (-e "directory.html") {
 system 'rm directory.html';
}

opendir(THISDIR, ".");
@allfiles = grep(!/^\.\.?|(.*)\.html.?|(.*)~/, readdir(THISDIR));
closedir(THISDIR);

$count = 0;
foreach $file (@allfiles) {
    open(FILE, "@allfiles[$count]") || die "can't open file @allfiles[$count]";
    @lines = <FILE>;
    (@fullname[$count]) = @lines[0] =~ /^full name: (.*)$/;
    (@name[$count]) = @lines[1] =~ /^prefered name: (.*)$/;
    (@email[$count]) = @lines[4] =~ /^email: (.*)$/;
    (@url[$count]) = @lines[5] =~ /^url: (.*)$/;
    (@picture[$count]) = @lines[26] =~ /^picture: (.*)$/;
    (@incpic[$count]) = @lines[27] =~ /^incpic: (.*)$/;
    $count++;
}
$count--;

sub fullname { @fullname[$a] gt @fullname[$b] }
@sort = sort(fullname (0..$count));

$count++;

open(DIRECTORY, ">./directory.html") || die "Can't open directory.html: $!\n";

print DIRECTORY <<EOF;
Content-type: text/html

<title>6.837--Computer Graphics Class Directory</title>

<center><img src=http://graphics.lcs.mit.edu/6.837/f96/gifs/6.837_new.gif></center><p>

<center><h1>Class Directory</h1></center><p>

<hr>

There are currently $count people in this directory.<p>

Format: Last Name, First Name (Preferred name) email, photo<p>

Click on the full name for the individual's home page, on the email address
to send email, and on the photo link for a picture.<p>

<hr>
EOF

open(DIRECTORY, ">>./directory.html") || die "Can't open directory.html: $!\n";

foreach $index (@sort) {
 if (@url[$index]) {
     print DIRECTORY "\n<a href=\"@url[$index]\">\n";
     print DIRECTORY "@fullname[$index]</a>\n";
 }
 else {
     print DIRECTORY "@fullname[$index]\n";
 }
 if (@name[$index]) {
     print DIRECTORY "(@name[$index])\n";
 }
 if (@email[$index]) {
     print DIRECTORY "<a href=\"mailto:@email[$index]\">@email[$index]</a>";
 }
 if ((@picture[$index]) && (@incpic[$index] eq "yes")) {
     print DIRECTORY ", <a href=\"@picture[$index]\">\n";
     print DIRECTORY "photo</a>\n";
 }
 print DIRECTORY "<p>";
}

print DIRECTORY <<EOF;

<hr>

This page is generated automatically on-demand.  If you experience difficulty
please contact 6837-staff\@graphics.lcs.mit.edu.

EOF
}

open(DIRECTORY, "<./directory.html") || die "Can't open directory.html: $!\n";

while (<DIRECTORY>) {
 print;
}

close(DIRECTORY);
