#!/usr/athena/bin/perl

$letters_to_start_at = "ACEIKMORT";
$www_rootdir   = "/afs/sipb/project/www/root";
$homepage_file = $www_rootdir . "/Home-byUser.html";
$out_directory = $www_rootdir . "/alpha";

# **************************************************************
# BE CAREFUL!!! THIS NEXT LINE IS WHAT THIS SCRIPT WILL *NUKE*
# IF IT WORKS. DO *NOT* SCREW UP HERE. :-)
$files_to_unlink = $www_rootdir . "/alpha/*";
# **************************************************************

if (@ARGV) {
    print STDERR <<EOU;
Usage: $0 Hm.

Tweedleday.
Allow to specify which letter to update.

EOU
    exit;
}

umask(022);

if (open(HFILE, $homepage_file)) {

    unlink <${files_to_unlink}>;

    # Find the start of the list. If you change the list-type, or
    # add a list before the list of homepages, you need to modify this
    # line/block of code to find the first thing before the list of
    # homepages.
    until ($bar = <HFILE> =~ /<ol>/) {print $bar;};

    # set the line separator to <li>. It's easiest this way. :-)
    $/="<li>";

    # suck the list elements into an array.
    @h_array=<HFILE>;
    close(HFILE);

    # this trims off the trailing HTML from the last element of the array.
    $h_array[$#h_array] =~ /<\/ol>/;
    $h_array[$#h_array] = $` ;

    # Nuke the <li>'s from the list elements.
    grep( s/<li>//, @h_array);
    grep( s#href="people/#href="../people/#, @h_array);

    for (@h_array) {
	# snag the first letter of the linktext
	$_ =~ />(.).*<\/[Aa]>/ ;
	$letter = $1;

	# lowercase it
	$letter =~ tr/A-Z/a-z/;

	# find the index, from 0 to 25, of $letter.
	$n = ord($letter) - ord("a");

	# do special stuff if the first character isn't between A and Z.
	if (($n < 0) || ($n > 25)) {
	    if ($_ =~ /\S/) {
		$out_array[26] .= "<li>" . $_;
	    }
	}
	else { # just slam it into the output array.
	    $out_array[$n] .= "<li>" . $_;
	}

    }

    $m = -1; # m stands for my_index .
    $letters_to_start_at =~ tr/a-z/A-Z/;

    for($i=0;$i<26;$i++) {
	$letter = pack("c", $i + ord("A"));
	if ($letters_to_start_at =~ /$letter/) {
	    ++$m;
	}
	$ra[$m] .= $letter;
    }

    foreach $range (@ra) {
	# for($i=0;$i<26;$i++) {
	if (length($range) < 2) {
	    $range_print = $range;
	}
	else {
	    $range_print = substr($range, 0, 1) . "-" . substr($range, -1, 1);
	}

	$start_p = ord(substr($range,  0, 1)) - ord("A");
	$end_p   = ord(substr($range, -1, 1)) - ord("A");

	$list_print = join('', @out_array[$start_p..$end_p]);
	$linecount = ($list_print =~ s/<li>/<li>/g);
	print $range_print, ": ", $linecount, "\n";

	if ($list_print) { # if there are elements for that letter
	    $ofile = ">" . $out_directory . "/home-" . $range_print . ".html";
	    open(OF, $ofile) || die "I can't write to $ofile: $!\n";

	    $list_of_pages .= "<li><a href=\"home-" . $range_print . ".html\">" . $range_print . "</a>\n";
	    &sub_header($range_print);
	    print OF $list_print;
	    print OF "</ol>\n<a href=\"../home-pages.html\">";
	    print OF "<img alt=\"\" src=\"../images/home.gif\">";
	    print OF "<em>Take me back to the full list of homepages</em></a>\n";
	    &o_footer();
	    close(OF)
	}
    }
    $ofile = ">" . $out_directory . "/home-index.html";
    open(OF, $ofile) || die "I can't write to $ofile: $!\n";

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

    print OF "<ul>\nAlphabetic by username\n";
    print OF $list_of_pages;
    print OF "</ul>\n";
    &o_footer();
    close(OF);
    print "Alphabetized homepage list generated.\n";

}
else {
    print ("Couldn't open homepage file. Oops.\n");

}

chmod 0644, <${files_to_unlink}>;

sub sub_header {
    local($range) = @_;

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

    print OF "\nThe usernames are at <code>mit.edu</code> unless they say otherwise.\n";
    print OF "\n<hr>\n<ol>\n";

}

sub o_header {
    print OF <<EOH;

The usernames are at <code>mit.edu</code> unless they say otherwise.

<hr>
<p>

EOH
    ;
}

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

<hr>
<address>
<a href="../help/before-you-mail.html">stuffmaster\@mit.edu</a>
</address>

</body>
</html>
EOF
    ;
}
