#!/usr/bin/perl

use lib "."; use lib "uxmon"; use lib "platforms"; 		#inslib
$debug = 0;
@files = sort { length($b) <=> length($a ) } (@ARGV);

while( $file = shift ) {
    my @text = ( "<PRE>\n" );
    my @contents = ();
    my $name = 0;

    print STDERR "converting $file\n";
    open( IN, "<$file" );
    open( OUT, ">$file.h" );
    select OUT;
    while( <IN> ) {
	chomp;
	s/\@/\@\@/g;
	s/\</&lt/g;
	s/\>/&gt/g;
	if( /^[\s\t]*([\-=\.]{2,})[\s\t\r\n]*$/ ) {
	    my $level = (substr($1,0,1) eq ".")?3:2;
	    my $last = $text[$#text];

	    if( $last =~ /^[\s\t]+/ ) {
		$last = $';
		$level++;
	    }
	    $text[$#text] = "</PRE><H$level><A NAME=$name>\n  ".$last."\n</A></H$level><PRE>\n";
	    push( @contents, { "level" => $level, "text" => $last, "name" => $name } );
	    $name++;
	}
	else {
	    push( @text, $_ );
	}
    }
    my $content;
    my $level=2;
    print <<END
<HTML>
<HEAD>
</HEAD>
<BODY>
END
if( $debug );
    print "<UL>";
    foreach $content (@contents) {
	while( $content->{"level"} > $level ) {
	    print "\n\n<UL>\n";
	    $level++;
	}
	while( $content->{"level"} < $level ) {
	    print "</UL>\n\n";
	    $level--;
	}
	print "<LI>\n";
	print "<A HREF=\"#".$content->{"name"}."\">\n";
	print $content->{"text"};
	print "\n</A></LI>\n\n";
    }
    print "</UL><HR>";
    foreach $content( @text ) {
	foreach $file (@files) {
	    $href = "<A HREF=\"$file.html\">$file</A>";
	    $content =~ s/([^\/\"])$file/$1$href/g;
	}
	$content =~ s#(http://[^ ]+)#<A HREF="$1">$1</A>#g;
	print $content."\n" ;
    }
    print <<END
</BODY>
</HTML>
END
if( $debug );
    close IN;
    close OUT;
}



