1#! /usr/bin/env perl
2##
3## Copyright (C) by Argonne National Laboratory
4##     See COPYRIGHT in top-level directory
5##
6
7# Create an index of web pages for MPICH
8#
9# Default values
10# Root for the pages
11$WWWRoot=`pwd`;
12chop $WWWRoot;
13# Base address (installation address)
14# $URLBase = "http://";
15# End of line character (\r\n is DOS-friendly)
16$eol = "\r\n";
17# Number of colums in table of names
18$TableCols = 3;
19#
20# Process arguments for any changes
21foreach $_ (@ARGV) {
22    if (/-?(-[^=]*)=(.*)/) {
23	$argname = $1;
24	$argval  = $2;
25    }
26    else {
27	$argname = $1;
28	$argval  = "";
29    }
30    if ($argname =~ /-wwwroot/) {
31	$WWWRoot = $argval;
32    }
33    elsif ($argname =~ /-urlbase/) {
34	$URLBase = $argval;
35    }
36    elsif ($argname eq "-help") {
37	print STDOUT "createhtmlindex [ -wwwroot=directory ] [ -urlbase=base ]\n";
38	print STDOUT "Build the www index pages for MPICH.";
39	print STDOUT "This must be run in the root of an MPICH tree; it may\n
40be run in a VPATH directory after configuring.\n";
41	exit 1;
42    }
43    else {
44	print STDERR "Unknown argument $_\n";
45	exit 1;
46    }
47}
48
49# Create the main index
50open( OUTFD, ">$WWWRoot/www/index.html" ) ||
51    die "Cannot open $WWWRoot/www/index.html\n";
52
53&AddHeader( "Web pages for MPI" );
54
55print OUTFD "<H2>MPI Commands</H2>$eol";
56&AddDirectoryContents( "www", "www1" );
57
58print OUTFD "<H2>MPI Routines and Constants</H2>$eol";
59if (-f "www/www3/mpi.cit") {
60    &createRedirects("www/www3", "mpi.cit");
61}
62else {
63    print STDERR "Could not find mapping file\n";
64}
65&AddDirectoryContents( "www", "www3" );
66
67#print OUTFD "<H2>MPE Routines</H2>$eol";
68#&AddDirectoryContents( "www", "www4" );
69
70&AddTrailer( );
71
72close( OUTFD );
73
74# Create the sectional indices
75open( OUTFD, ">$WWWRoot/www/www1/index.htm" ) ||
76    die "Cannot open $WWWRoot/www/www1/index.htm\n";
77
78&AddHeader( "Manpages for MPICH" );
79&AddDirectoryContents( "www/www1", "." );
80&AddTrailer( );
81close( OUTFD );
82
83open( OUTFD, ">$WWWRoot/www/www3/index.htm" ) ||
84    die "Cannot open $WWWRoot/www/www3/index.htm\n";
85
86&AddHeader( "Web pages for MPI Routines and Constants" );
87&AddDirectoryContents( "www/www3", "." );
88&AddTrailer( );
89close( OUTFD );
90
91# open( OUTFD, ">$WWWRoot/www/www4/index.htm" ) ||
92#     die "Cannot open $WWWRoot/www/www4/index.htm\n";
93
94# &AddHeader( "Web pages for MPE Routines" );
95# &AddDirectoryContents( "www/www4", "." );
96# &AddTrailer( );
97# close( OUTFD );
98
99
1000;
101# ---------------------------------------------------------------------------
102# Support routines.
103# All write to OUTFD and use $eol for end-of-line
104# ---------------------------------------------------------------------------
105sub AddHeader {
106    my $title = $_[0];
107    print OUTFD "<HTML>$eol<HEAD>$eol<TITLE>$title</TITLE>$eol";
108    print OUTFD "<!-- This file generated by createhtmlindex on $date -->$eol";
109    print OUTFD "</HEAD>$eol<BODY BGCOLOR=\"FFFFFF\">$eol";
110    print OUTFD "<H1>$title</H1>$eol";
111}
112
113sub AddTrailer {
114    print OUTFD "</BODY>$eol</HTML>$eol";
115}
116
117# For the items (mostly MPI constants) that are within a single web page,
118# create a redirect page for them.  This allows us to point to a location
119# on the page, rather than just a page which is what a file link would
120# accomplish
121sub createRedirects {
122    $rootdir = $_[0];
123    $mapfile = $_[1];
124    open( MAPFD, "<$rootdir/$mapfile" ) || die "Cannot open map file $mapfile\n";
125    while (<MAPFD>) {
126	@fields = split(/\+/);
127	$name = $fields[1];
128	$url  = $fields[8];
129	if ($url =~ /(.*)\/([^\/]*)\.([HTMLhtml]*)#(.*)/) {
130	    $rooturl  = $1;
131	    $basefile = $2;
132	    $ext      = $3;
133	    $anchor   = $4;
134	    if ($basefile ne $anchor) {
135		open(RFD, ">$rootdir/$anchor.htm") || die "Cannot open redirect file $anchor.htm\n";
136		print RFD "<html><head><meta http-equiv=\"refresh\" content=\"0; url=$basefile.$ext#$anchor\" /></head></html>\n";
137		close RFD;
138	    }
139	}
140	else {
141	    print STDERR "Could not decode $url\n";
142	}
143
144    }
145    close MAPFD;
146}
147
148# Take all .htm and .html files and add them to the OUTFD file.
149# This works in two steps:
150# 1. Read and sort the contents of the directory into the array
151# @HTMLFiles
152# 2. Use the routine MakeHTMLTable to create a table with a given
153# number of columns, adding the links within the columns
154@HTMLFiles = ();
155# Look in $1/$2 for files, but make links relative to $2
156sub AddDirectoryContents {
157    my $rootdir = $_[0];
158    my $dirname = $_[1];
159
160    # 1 Read the files
161    @HTMLFiles = ();
162    opendir DIR, "$rootdir/$dirname";
163    if ($dirname eq ".") {
164	$prefixname = "";
165    }
166    else {
167	$prefixname = "$dirname/";
168    }
169    while ($filename = readdir DIR) {
170	if ($filename =~ /index\.html?/) { next; }
171	if ($filename =~ /.*\.html?$/) {
172	    $HTMLFiles[$#HTMLFiles+1] = "$prefixname$filename";
173	}
174    }
175    closedir DIR;
176
177    @HTMLFiles = sort( @HTMLFiles );
178
179    # Format the table
180    &MakeHTMLTable;
181}
182# MakeHTMLTable takes an array of items and turns them into a table with
183# $TableCols columns.
184#
185sub MakeHTMLTable {
186    my $nvals = $#HTMLFiles;
187
188    my $nrows = int ( ($nvals + $TableCols - 1) / $TableCols );
189    print OUTFD "<TABLE>$eol";
190    for ($j=0; $j<$nrows; $j++) {
191	print OUTFD "<TR>";
192	for ($e=0; $e<$TableCols; $e++) {
193	    $filename = $HTMLFiles[$j + $e * $nrows];
194	    $linkname = $filename;
195	    $linkname =~ s/\.html?//;
196	    $linkname =~ s/.*\///;
197	    if ($filename ne "") {
198		$line = "<A HREF=\"$filename\">$linkname</A>";
199	    }
200	    else {
201		$line = "";
202	    }
203	    print OUTFD "<TD>$line</TD>$eol";
204	}
205	print OUTFD "</TR>$eol";
206    }
207    print OUTFD "</TABLE>$eol";
208}
209