#!/usr/local/bin/perl -w

@sets = ("crisGet", "crisPut", "sisGet", "sisPut", "utilities");
@languages = ("c", "idl");

foreach $set (@sets) {
    undef(%val);

    $orderFile = "routineDBs/$set/order.txt";
    if (-e $orderFile) { open(ORDER, $orderFile) || die $!; }
    else { next; }

    $/ = "\n\n";	# read paragraph mode

    @sections = ();
    @routineIDs = ();
    while (<ORDER>) {
	@lines = split(/\n/);
	$section = $lines[0];
	push(@sections, $section);
	$i = 1;
	$description = "";
	while ($lines[$i] =~ /^\t/) {
	    ($trimmed) = ($lines[$i] =~ /^\t(.*)/);
	    $description .= $trimmed . " ";
	    $i++;
	}
	chop($description);
	$descriptions{$section} = $description;

	$routineIDs{$section} = "";
	for (; $i < @lines; $i++) {
	    $routineID = $lines[$i];
	    $routineIDs{$section} .= $routineID . "|";
	    foreach $lang (@languages) {
		$file = "routineDBs/$set/$routineID-$lang.db";
		if (-e $file) { open(DB, $file) || die $!; }
		else { next; }

		while (<DB>) {
		    @dbLines = split(/\n/, $_);
		    $key = shift(@dbLines);
		    unless ($key =~ /^_\w+/) {
			die "Invalid key-value pair in $routineID-$lang.db\n";
		    }
		    $val{"$routineID-$lang-$key"} = join("\n", @dbLines);
		}
		@stats = stat(DB);
		close(DB);

		@dateFields = localtime($stats[9]);
		$mday = $dateFields[3];
		$month = $dateFields[4] + 1;
		$year = $dateFields[5] + 1900;
		$lastModifiedDate{"$routineID-$lang"} = "$month/$mday/$year";
	    }
	}
	chop($routineIDs{$section});
    }
    close(ORDER);

    foreach $lang (@languages) {
	open(HTML, ">$set-$lang.html") || die $!;

	print HTML <<"STOP_PRINT";

<HTML>
<HEAD>
<TITLE>ACEware Level1 Library: $lang $set</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff" TEXT="#000000">
<H1 ALIGN=CENTER>ACEware Level1 Library: $lang $set</H1>
<HR SIZE=5 NOSHADE>
<H3 ALIGN=CENTER><A NAME="contents">Contents</A></H3>
<CENTER>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>

STOP_PRINT

	for ($i = 0; $i < @sections; $i++) {
	    $section = $sections[$i];
	    print HTML <<"STOP_PRINT";

  <TR><TD ALIGN=CENTER COLSPAN=2><B>$section</B></TD></TR>
STOP_PRINT

	    @routineIDs = split(/\|/, $routineIDs{$section});
	    while ($routineID = shift(@routineIDs)) {
		$name = $val{"$routineID-$lang-_NAME_"};
		unless ($name) { next; }
		$date = $lastModifiedDate{"$routineID-$lang"};
		print HTML <<"STOP_PRINT";

  <TR><TD><A HREF="#$name">$name</A></TD>
    <TD><IMG SRC="/icons/blank.gif" HEIGHT=1 WIDTH=30>$date</TD></TR>
STOP_PRINT

	    }
	    print HTML <<"STOP_PRINT";

  <TR><TD COLSPAN=2></TD></TR>
STOP_PRINT

	}
	print HTML <<"STOP_PRINT";

</TABLE>
</CENTER>
<HR SIZE=5 NOSHADE>
STOP_PRINT

	for ($i = 0; $i < @sections; $i++) {
	    $section = $sections[$i];
	    print HTML <<"STOP_PRINT";

<H3 ALIGN=CENTER>$section</H3>
<BLOCKQUOTE>
$descriptions{$section}
</BLOCKQUOTE>
<HR WIDTH="80%" NOSHADE>
STOP_PRINT

	    @routineIDs = split(/\|/, $routineIDs{$section});
	    while ($routineID = shift(@routineIDs)) {
		unless ($val{"$routineID-$lang-_NAME_"}) { next; }
		$prototype = $val{"$routineID-$lang-_PROTOTYPE_"};
		$name = $val{"$routineID-$lang-_NAME_"};
		$prototype =~ s/$name/<FONT COLOR=\"#ff0000\">$name<\/FONT>/;

		$description = $val{"$routineID-$lang-_DESCRIPTION_"};
		$description =~ s/^_$/<P>/mg;

		$shortCodeSample = "";
		if (defined($val{"$routineID-$lang-_SHORT-CODE-SAMPLE_"})) {
		    $shortCodeSample =
				$val{"$routineID-$lang-_SHORT-CODE-SAMPLE_"};
		    $shortCodeSample =~ s/^_\s*$//mg;
		    $shortCodeSample =~ s/^(\t*)/$1    /mg;
		    $shortCodeSample =~
			s/$name/<FONT COLOR=\"#ff0000\">$name<\/FONT>/mg;
		    $shortCodeSample =
				"\nUsage:<BR>\n<PRE>\n" . $shortCodeSample;
		    $shortCodeSample .= "\n</PRE>\n<P>\n\n";
		}

		$input = $val{"$routineID-$lang-_INPUT_"};
		$input =~ s/^\* /\n  <LI> /mg;
		$output = $val{"$routineID-$lang-_OUTPUT_"};
		$output =~ s/^\* /\n  <LI> /mg;

		print HTML <<"STOP_PRINT";

<A NAME="$name"><B>$val{"$routineID-$lang-_SHORT-DESCRIPTION_"}</B></A>
<PRE>
$prototype
</PRE>
<P>
$description
<P>
$shortCodeSample
<B>Input:</B>
<UL>
$input
</UL>
<B>Output:</B>
<UL>
$output
</UL>

<CENTER>
<TABLE WIDTH="80%" BORDER=0>
  <TR><TD ALIGN="LEFT">
<FONT COLOR="#ff0000" SIZE="-1">
Last Modified: $lastModifiedDate{"$routineID-$lang"}
</FONT>
  </TD>
  <TD ALIGN="RIGHT">
<FONT SIZE="-1"><A HREF="#contents">Return to Contents</A></FONT>
  </TD></TR>
</TABLE>
</CENTER>
STOP_PRINT

		if (@routineIDs > 0) { print HTML "<HR WIDTH=\"80%\">\n"; }
	    }

	    if (@sections > 0) { print HTML "<HR NOSHADE>\n"; }
	}

	print HTML <<"STOP_PRINT";

<P ALIGN=CENTER>
<A HREF="/ACE/forum-posts/bruce/aceWareDocs/level1/getData.html">
Return to level1 Data Retrieval Libraries Documentation</A>
<P ALIGN=CENTER>
<A HREF="/ACE/forum-posts/bruce/aceWareDocs/level1/putData.html">
Return to level1 Data Archival Libraries Documentation</A>
<P ALIGN=CENTER>
<A HREF="/ACE/forum-posts/bruce/aceWareDocs/level1/contents.html">
Return to level1 Documentation Table of Contents</A>


</BODY>
</HTML>
STOP_PRINT

	close(HTML);
    }
}
