#!/afs/athena/contrib/perl/p -w

##
# Database file to html file converter
# Wilfredo Sanchez Jr.
##
# Copyright 1995 Wilfredo Sanchez Jr. | tritan@mit.edu
# Written for the MIT SIPB webmasters | webmaster@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);

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) {
	    warn "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");

    # 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) {
	    warn "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 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 "<html>\n";
    print $outfile "<!-- home2html.pl v.$version_id $version_date\n";
    print $outfile "     Wilfredo Sanchez Jr. | tritan\@mit.edu\n";
    print $outfile "     Document copyright 1993 MIT SIPB | webmaster\@mit.edu\n";
    print $outfile " -->\n";
    print $outfile "<head>\n";
    print $outfile "<link rev=made href=\"mailto:webmaster\@mit.edu\">\n";
    print $outfile "<link rev=owns href=\"mailto:webmaster\@mit.edu\">\n";
    print $outfile "<title>MIT Community Home Pages ($version)</title>\n";
    print $outfile "</head>\n";
    print $outfile "\n";
    print $outfile "<body>\n";
    print $outfile "\n";
    print $outfile "<h1><img alt=\"[fireplace]\" src=\"/gif/MIThome_cropped.GIF\" width=123 height=124>\n";
    print $outfile "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=\"whats-new.html\">\n";
    print $outfile "<img alt=\"What's new?\" src=\"/gif/Icon_NEW.GIF\" width=72 height=72></a>\n";
    print $outfile "\n";
    print $outfile "<a href=\"services.html\">\n";
    print $outfile "<img alt=\"SIPB services\" src=\"/gif/Icon_GATE.GIF\"></a>\n";
    print $outfile "\n";
    print $outfile "<a href=\"server-pages.html\">\n";
    print $outfile "<img alt=\"Other MIT Servers\" src=\"/gif/Icon_SERV.GIF\"></a>\n";
    print $outfile "\n";
    print $outfile "<a href=\"mit-activities.html\">\n";
    print $outfile "<img alt=\"Activities\" src=\"/gif/Icon_ORG.GIF\"></a>\n";
    print $outfile "\n";
    print $outfile "<a href=\"documents.html\">\n";
    print $outfile "<img alt=\"Documents\" src=\"/gif/Icon_DOCS.GIF\"></a>\n";
    print $outfile "\n";
    print $outfile "<a href=\"fun-stuff.html\">\n";
    print $outfile "<img alt=\"Fun Stuff\" src=\"/gif/Icon_FUN.GIF\"></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=\"/webmasters.html\">webmaster\@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";
}
