#!/usr/athena/bin/perl

##
# Database file to html file converter
# Wilfredo Sanchez Jr.
##
# Copyright 1995 Wilfredo Sanchez Jr. | tritan@mit.edu
# Written for the MIT SIPB webmasters | stuffmaster@mit.edu
# Oh, joy.
##

$version_id   = "1.1";
$version_date = "13 Mar 95";

unshift(@INC, '/afs/sipb.mit.edu/project/www/src/db');

require 'dbparse.pl';

# Deal with the command line

$outpath = shift(@ARGV);
$dbfile  = shift(@ARGV);

$outpath = '/afs/sipb.mit.edu/project/www/root' unless $outpath;
$dbfile  = $DB'default_home                     unless $dbfile;

# Read in the file
&DB'ParseHome($dbfile);

# Spew out some html
&spewByName($outpath);
&spewByUser($outpath);
&spewMicro($outpath);

&spewByUserSorted($outpath);
&spewByNameSorted($outpath);

sub spewByName {
    # Well, actually, it's by index
    # &spewByName($outpath);
    #   $outpath = directory to place Home-byName.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath) = @_;

    local($outfilename) = ($outpath . "/Home-byName.html");

    # Open output file
    unless (open(OUTFILE, ">$outfilename")) {
	warn "$0: WARNING: Unable to open output file $outfilename.\n";
	return -1;
    }

    # Start it off
    &printTop(OUTFILE, "Comprehensive Listing by Name", "name");

    # List by name
    foreach $index (sort keys %DB'name)
    {
	# Output should look like:
	# <li> <a name="tritan" href="/people/tritan/tritan.html">tritan</a>, Wilfredo S&aaccute;nchez Jr.
	local($name) = $DB'name{$index};
	local($user) = $DB'user{$name};

	unless ($index && $name && $user) {
	    print "spewByName: skipping $index due to empty field\n";
	    next;
        }

	print OUTFILE "  <li> <a name=\"$user\" href=\"$DB'url{$user}\">$user</a>, $name\n\n";
    }

    &printBottom(OUTFILE);

    close(OUTFILE);
}

sub spewByUser {
    # &spewByUser($outpath);
    #   $outpath = directory to place Home-byUser.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath) = @_;

     local($outfilename) = ($outpath . "/Home-byUser.html");
#     local($outfilename) = ($outpath . "/home-pages.html");

    # Open output file
    unless (open(OUTFILE, ">$outfilename")) {
	warn "$0: WARNING: Unable to open output file $outfilename.\n";
	return -1;
    }

    # Start it off
    &printTop(OUTFILE, "Comprehensive Listing by User Name", "email address");

    # List by name
    foreach $user (sort keys %DB'index)
    {
	# Output should look like:
	# <li> <a name="tritan" href="/people/tritan/tritan.html">tritan</a>, Wilfredo S&aaccute;nchez Jr.

	local($index) = $DB'index{$user};
	local($name)  = $DB'name{$index};

	unless ($index && $name && $user) {
	    print "spewByUser: skipping $user due to empty field\n";
	    next;
        }

	print OUTFILE "  <li> <a name=\"$user\" href=\"$DB'url{$user}\">$user</a>, $name\n\n";
    }

    &printBottom(OUTFILE);

    close(OUTFILE);
}

sub spewMicro {
    # &spewByUser($outpath);
    #   $outpath = directory to place Home-byUser.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath) = @_;

     local($outfilename) = ($outpath . "/Home-Micro.html");

    # Open output file
    unless (open(OUTFILE, ">$outfilename")) {
	warn "$0: WARNING: Unable to open output file $outfilename.\n";
	return -1;
    }

    # Start it off
    &printTop(OUTFILE, "Comprehensive Listing by User Name", "email address");

    print OUTFILE "<font size=-10>\n";

    # List by name
    foreach $user (sort keys %DB'index)
    {
	# Output should look like:
	# <li> <a name="tritan" href="/people/tritan/tritan.html">tritan</a>, Wilfredo S&aaccute;nchez Jr.

	local($index) = $DB'index{$user};
	local($name)  = $DB'name{$index};

	unless ($index && $name && $user) {
	    print "spewByUser: skipping $user due to empty field\n";
	    next;
        }

	print OUTFILE "<a href=\"$DB'url{$user}\">$user</a>, $name; ";
    }
    print OUTFILE "</font>\n";

    &printBottom(OUTFILE);

    close(OUTFILE);
}

sub spewByUserSorted {
    # &spewByUserSorted($outpath);
    #   $outpath = directory to place Home-byUser.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath) = @_;

    &spewGenericSorted($outpath, "printUserLine", *DB'index,
                       "User Name", " email address", "useralpha");
}

sub printUserLine {
    local($o, $user)  = @_;
    local($index) = $DB'index{$user};
    local($name)  = $DB'name{$index};

    # Output should look like:
    # <li> <a name="tritan" href="/people/tritan/tritan.html">tritan</a>, Wilfredo S&aaccute;nchez Jr.
	    
    if ($index && $name && $user) {
       print $o "  <li> <a name=\"$user\" href=\"$DB'url{$user}\">$user</a>, $name\n\n";
    } else {
       print "spewByUser: skipping $user due to empty field\n";
    }
}

sub spewByNameSorted {
    # &spewByNameSorted($outpath);
    #   $outpath = directory to place Home-byUser.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath) = @_;

    &spewGenericSorted($outpath, "printNameLine", *DB'name,
                       "Name", "name", "namealpha");
}

sub printNameLine {
    local($o, $index)  = @_;
    local($name) = $DB'name{$index};
    local($user) = $DB'user{$name};

    # Output should look like:
    # <li> <a name="tritan" href="/people/tritan/tritan.html">tritan</a>, Wilfredo S&aaccute;nchez Jr.

    if ($index && $name && $user) {
       print $o "  <li> <a name=\"$user\" href=\"$DB'url{$user}\">$user</a>, $name\n\n";
    } else {
       print "spewByUser: skipping $user due to empty field\n";
    }
}

sub spewGenericSorted {
    # &spewByUserSorted($outpath);
    #   $outpath = directory to place Home-byUser.html
    # NOTE: this function expects DB'ParseHome to have been run
    local($outpath, $printfunc, *tosort, $sortversiontext, $sortbytext,
	  $dirname) = @_;
    local($letter, %todo, $save, $upletter, $navigate, $outfilename);

    local($basefilename) = ($outpath . "/" . $dirname . "/");

    # This code is a little gross. But in order for it to be efficient when
    # handling 1300+ homepages, it's good to avoid grepping/sorting over the
    # full list of keys 26 times; this code does it only once. :-)

    for $letter ("a" .. "z") {
	$todo{$letter} = "";
    }

    for $key (sort keys %tosort) {
        if ($key =~ /^([a-zA-Z])/) {
            $letter = $1;
            $letter =~ tr/A-Z/a-z/;
            $todo{$letter} .= ("\cw" . $key);
        } else {
            # this throws in nonalphanumerics with "a". It's silly and
            # arbitrary but seemed like a reasonable decision; it's where
            # most indices put such things.
            $todo{"a"} .= ("\cw" . $key);
        }
    }

    for $letter (keys %todo) {
	$todo{$letter} =~ s/^\cw//;
        if ($todo{$letter} eq "") {
           delete $todo{$letter};
        }
    }

    $navigate = "";
    for $letter (sort keys %todo) {
	$upletter = $letter;
	$upletter =~ tr/a-z/A-Z/;
	$navigate .= "<a href=\"${letter}.html\">$upletter</a>, ";
    }
    chop $navigate;
    chop $navigate;
    
    $save = $|;
    $| = 1;
    for $letter (sort keys %todo) {

        print $letter, "..";
	local($outfilename) = ($basefilename . $letter . ".html");
	# Open output file
	unless (open(OUTFILE, ">$outfilename")) {
	    warn "$0: WARNING: Unable to open output file $outfilename.\n";
	    return -1;
	}

	# Start it off
	&printTop(OUTFILE,
		  ("Comprehensive Listing by $sortversiontext ($letter)"),
		  $sortbytext);


	# List by name
	foreach $user (split(/\cw/, $todo{$letter})) {
	    &$printfunc(OUTFILE, $user);

        }

        &printBottom(OUTFILE);

        close(OUTFILE);
    }

    print "\n";
    $! = $save;

    # Make index file

    $outfilename = ($basefilename . "index.html");
    # Open output file
    unless (open(OUTFILE, ">$outfilename")) {
	warn "$0: WARNING: Unable to open output file $outfilename.\n";
	return -1;
    }

    # Start it off
    &printTop(OUTFILE,
	      ("Comprehensive Listing by $sortversiontext"),
	      $sortbytext);
    
    print OUTFILE "<ul>\n";
    for $letter (sort keys %todo) {
	$upletter = $letter;
	$upletter =~ tr/a-z/A-Z/;
	print OUTFILE "<li> <a href=\"${letter}.html\">$upletter</a>\n";
    }
    print OUTFILE "</ul>\n";

    &printBottom(OUTFILE);

    close(OUTFILE);
}

#sub spewByDate {
    # Possibly useful for maintenance
#}

sub printTop {
    # Print the top of the document
    # &printTop($outfile, $version, $by);
    #   $outfile = output file handle
    #   $version = kind of listing
    #   $by      = what we'll be listing by
    local($outfile, $version, $by) = @_;

    print $outfile "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n";
    print $outfile "<html>\n";
    print $outfile "<head>\n";
    print $outfile "<title>MIT Community Home Pages ($version)</title>\n";
    print $outfile "<link rev=made href=\"mailto:stuffmaster\@mit.edu\">\n";
    print $outfile "</head>\n";
    print $outfile "\n";
    print $outfile "<body>\n";
    print $outfile "\n";
    print $outfile "<p><img align=\"left\" src=\"images/home.gif\" alt=\"\"\n";
    print $outfile "width=50 height=50 hspace=10 vspace=10> </p>\n";
    print $outfile "<h1>MIT Community Home Pages</h1>\n";

    print $outfile "<h2>$version</h2>\n";
    print $outfile "\n";
    print $outfile "<hr>\n";
    print $outfile "<p>To link your own home page into this document, please\n";
    print $outfile "read the <a href=\"doc/how-to-hp.html\">instructions</a>\n";
    print $outfile "on how to add your home page to our server.</p>\n";
    print $outfile "\n";
    print $outfile "<p>This listing is alphabetical by $by. Electronic mail addresses\n";
    print $outfile "are at <tt>mit.edu</tt> unless otherwise specified.</p>\n";
    print $outfile "\n";
    print $outfile "<hr>\n";
    print $outfile "\n";
    print $outfile "<form action=\"/home\">\n";
    print $outfile "Give me the home page for: <input name=\"isindex\">\n";
    print $outfile "</form>\n";
    print $outfile "\n";
    print $outfile "<hr>\n";
    print $outfile "\n";
    print $outfile "<ol>\n";
    print $outfile "\n";
}

sub printBottom {
    # Print the bottom of the document
    # &printBottom($$outfile);
    #   $$outfile = output file handle
    local($outfile) = @_;

    print $outfile "</ol>\n";
    print $outfile "\n";
    print $outfile "<hr>\n";
    print $outfile "\n";

    print $outfile "<a href=\"index.html\"><img src=\"images/main.gif\"\n";
    print $outfile "alt=\"[stuff.mit.edu home page]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";
    print $outfile "<a href=\"home-pages.html\"><img src=\"images/home.gif\"\n";
    print $outfile "alt=\"[personal home pages]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";
    print $outfile "<a href=\"activities/\"><img src=\"images/groups.gif\"\n";
    print $outfile "alt=\"[activities and clubs]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";
    print $outfile "<a href=\"services.html\"><img src=\"images/services.gif\"\n";
    print $outfile "alt=\"[gateways and services]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";
    print $outfile "<a href=\"content.html\"><img src=\"images/docs.gif\"\n";
    print $outfile "alt=\"[local content]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";
    print $outfile "<a href=\"help/\"><img src=\"images/help.gif\"\n";
    print $outfile "alt=\"[help resources]\"\n";
    print $outfile "width=50 height=50 border=0></a>\n";

    print $outfile "\n";
    print $outfile "<hr>\n";
    print $outfile "Last updated: ", `date`;
    print $outfile "\n";
    print $outfile "<address>\n";
    print $outfile "MIT Student Information Processing Board |\n";
    print $outfile "<a href=\"help/before-you-mail.html\">stuffmaster\@mit.edu</a>\n";
    print $outfile "</address>\n";
    print $outfile "\n";
    print $outfile "</hr>\n";
    print $outfile "\n";
    print $outfile "</body>\n";
    print $outfile "</html>\n";
}

# This is really really gross. But it keeps perl -w from complaining about
# the fact that these functions are never explicitly called.
if (0) {
    &printUserLine();
    &printNameLine();
}
