#! /usr/bin/env perl
# -*- mode: perl; -*-
#
# Create an index of web pages for MPICH
#
# Default values
# Root for the pages
$WWWRoot=`pwd`;
chop $WWWRoot;
# Base address (installation address)
# $URLBase = "http://";
# End of line character (\r\n is DOS-friendly)
$eol = "\r\n";
# Number of colums in table of names
$TableCols = 3;
#
# Process arguments for any changes
foreach $_ (@ARGV) {
    if (/-?(-[^=]*)=(.*)/) {
	$argname = $1;
	$argval  = $2;
    }
    else {
	$argname = $1;
	$argval  = "";
    }
    if ($argname =~ /-wwwroot/) {
	$WWWRoot = $argval;
    }
    elsif ($argname =~ /-urlbase/) {
	$URLBase = $argval;
    }
    elsif ($argname eq "-help") {
	print STDOUT "createhtmlindex [ -wwwroot=directory ] [ -urlbase=base ]\n";
	print STDOUT "Build the www index pages for MPICH.";
	print STDOUT "This must be run in the root of an MPICH tree; it may\n
be run in a VPATH directory after configuring.\n";
	exit 1;
    }
    else {
	print STDERR "Unknown argument $_\n";
	exit 1;
    }
}

# Create the main index
open( OUTFD, ">$WWWRoot/www/index.html" ) ||
    die "Cannot open $WWWRoot/www/index.html\n";

&AddHeader( "Web pages for MPI" );

print OUTFD "<H2>MPI Commands</H2>$eol";
&AddDirectoryContents( "www", "www1" );

print OUTFD "<H2>MPI Routines and Constants</H2>$eol";
if (-f "www/www3/mpi.cit") {
    &createRedirects("www/www3", "mpi.cit");
}
else {
    print STDERR "Could not find mapping file\n";
}
&AddDirectoryContents( "www", "www3" );

#print OUTFD "<H2>MPE Routines</H2>$eol";
#&AddDirectoryContents( "www", "www4" );

&AddTrailer( );

close( OUTFD );

# Create the sectional indices
open( OUTFD, ">$WWWRoot/www/www1/index.htm" ) ||
    die "Cannot open $WWWRoot/www/www1/index.htm\n";

&AddHeader( "Manpages for MPICH" );
&AddDirectoryContents( "www/www1", "." );
&AddTrailer( );
close( OUTFD );

open( OUTFD, ">$WWWRoot/www/www3/index.htm" ) ||
    die "Cannot open $WWWRoot/www/www3/index.htm\n";

&AddHeader( "Web pages for MPI Routines and Constants" );
&AddDirectoryContents( "www/www3", "." );
&AddTrailer( );
close( OUTFD );

# open( OUTFD, ">$WWWRoot/www/www4/index.htm" ) ||
#     die "Cannot open $WWWRoot/www/www4/index.htm\n";

# &AddHeader( "Web pages for MPE Routines" );
# &AddDirectoryContents( "www/www4", "." );
# &AddTrailer( );
# close( OUTFD );


0;
# ---------------------------------------------------------------------------
# Support routines.
# All write to OUTFD and use $eol for end-of-line
# ---------------------------------------------------------------------------
sub AddHeader {
    my $title = $_[0];
    print OUTFD "<HTML>$eol<HEAD>$eol<TITLE>$title</TITLE>$eol";
    print OUTFD "<!-- This file generated by createhtmlindex on $date -->$eol";
    print OUTFD "</HEAD>$eol<BODY BGCOLOR=\"FFFFFF\">$eol";
    print OUTFD "<H1>$title</H1>$eol";
}

sub AddTrailer {
    print OUTFD "</BODY>$eol</HTML>$eol";
}

# For the items (mostly MPI constants) that are within a single web page,
# create a redirect page for them.  This allows us to point to a location
# on the page, rather than just a page which is what a file link would
# accomplish
sub createRedirects {
    $rootdir = $_[0];
    $mapfile = $_[1];
    open( MAPFD, "<$rootdir/$mapfile" ) || die "Cannot open map file $mapfile\n";
    while (<MAPFD>) {
	@fields = split(/\+/);
	$name = $fields[1];
	$url  = $fields[8];
	if ($url =~ /(.*)\/([^\/]*)\.([HTMLhtml]*)#(.*)/) {
	    $rooturl  = $1;
	    $basefile = $2;
	    $ext      = $3;
	    $anchor   = $4;
	    if ($basefile ne $anchor) {
		open(RFD, ">$rootdir/$anchor.htm") || die "Cannot open redirect file $anchor.htm\n";
		print RFD "<html><head><meta http-equiv=\"refresh\" content=\"0; url=$basefile.$ext#$anchor\" /></head></html>\n";
		close RFD;
	    }
	}
	else {
	    print STDERR "Could not decode $url\n";
	}

    }
    close MAPFD;
}

# Take all .htm and .html files and add them to the OUTFD file.
# This works in two steps:
# 1. Read and sort the contents of the directory into the array
# @HTMLFiles
# 2. Use the routine MakeHTMLTable to create a table with a given
# number of columns, adding the links within the columns
@HTMLFiles = ();
# Look in $1/$2 for files, but make links relative to $2
sub AddDirectoryContents {
    my $rootdir = $_[0];
    my $dirname = $_[1];

    # 1 Read the files
    @HTMLFiles = ();
    opendir DIR, "$rootdir/$dirname";
    if ($dirname eq ".") {
	$prefixname = "";
    }
    else {
	$prefixname = "$dirname/";
    }
    while ($filename = readdir DIR) {
	if ($filename =~ /index\.html?/) { next; }
	if ($filename =~ /.*\.html?$/) {
	    $HTMLFiles[$#HTMLFiles+1] = "$prefixname$filename";
	}
    }
    closedir DIR;

    @HTMLFiles = sort( @HTMLFiles );

    # Format the table
    &MakeHTMLTable;
}
# MakeHTMLTable takes an array of items and turns them into a table with
# $TableCols columns.
#
sub MakeHTMLTable {
    my $nvals = $#HTMLFiles;

    my $nrows = int ( ($nvals + $TableCols - 1) / $TableCols );
    print OUTFD "<TABLE>$eol";
    for ($j=0; $j<$nrows; $j++) {
	print OUTFD "<TR>";
	for ($e=0; $e<$TableCols; $e++) {
	    $filename = $HTMLFiles[$j + $e * $nrows];
	    $linkname = $filename;
	    $linkname =~ s/\.html?//;
	    $linkname =~ s/.*\///;
	    if ($filename ne "") {
		$line = "<A HREF=\"$filename\">$linkname</A>";
	    }
	    else {
		$line = "";
	    }
	    print OUTFD "<TD>$line</TD>$eol";
	}
	print OUTFD "</TR>$eol";
    }
    print OUTFD "</TABLE>$eol";
}
