# -*-perl-*-
#
# grdoc_richdoc
#
# Makes the rich frame frame targets and sub-descriptors from a given infile
# Note this does not have to parse through the file since it simply 
# assembles the already created File and Routine documentation.
# (although does some stuff to those).

# $Id: grdoc_richdoc,v 1.15 1996/04/21 17:31:49 pwalker Exp $

sub
    grdoc_richdoc
{
    local ($infile) = @_;
    $outfile = $infile;
    $outfile =~ s:$opt_i:$opt_o/:;
    $outfile =~ s:^(.*)/([^/]+)$:\2:;
    $thesd = $1;
    $urlfile = $outfile;
    $urlfile =~ s:\.:_:g;
    # Create the frameset doc.
    open (OUT, ">$thesd/${urlfile}_Rich_Doc.html") 
	|| die "$thesd/${urlfile}_Rich_Doc.html: $!\n";
    print "      Created $thesd/${urlfile}_Rich_Doc.html\n" if $opt_V;
    $thehtmld = $thesd;
    $thehtmld =~ s:$opt_o:$opt_t/PROTECTED:;
    $srcurl = "$thehtmld/${urlfile}_RD_src.html";
    print OUT <<EOM;
<html>
<head><title>$outfile Rich Documentation</title></head>
<!-- $outfile Rich Documentation: created by grdoc -->
<!-- For more on grdoc, see http://jean-luc.ncsa.uiuc.edu/Codes/grdoc/ -->
<!-- grdoc Copyright University of Illinois Trustees -->
<!-- grdoc by Paul Walker with Joan Masso -->
<frameset ROWS="60%, 40%">
<frame src="${urlfile}_RD.html" name="rd">
<frame src="$srcurl" name="src">
<noframes>
<h1 align=center>This document uses Frames</h1>
This document requires the use of frames, and thus must be viewed with a frames compliant
browser, such as Netscape 2.0 or higher.  The fact that you are seeing this indicates that you
are NOT using a frames compliant browser, so you cannot see this document.
<hr>
This document uses frames<br>
<a href="mailto:$author">$author</a>
</noframes>
</frameset>
</html>
EOM
    close OUT;
    
    # Create the actual frame index
    open(OUT, ">$thesd/${urlfile}_RD.html") 
	|| die "$thesd/${urlfile}_RD.html: $!\n";
    $dadots = &make_dotdot($infile);
    print OUT <<EOM;
<body bgcolor="#ffffff">
<!-- $outfile Rich Documentation: created by grdoc -->
<!-- For more on grdoc, see http://jean-luc.ncsa.uiuc.edu/Codes/grdoc/ -->
<!-- grdoc Copyright University of Illinois Trustees -->
<!-- grdoc by Paul Walker with Joan Masso -->
<h3 align=center>$outfile<br>Documentation Index</h3>
EOM

    &navigation($dadots);
    print OUT "<hr>\n";

# OK, now go through this file inserting the html from the other locations
# into this document separated by horizontal rules.  We will filter out all 
# rules from the read in files before printing, and then add our own.

# First, grab the file descriptor
    print OUT "<h3>\@file: ";
    &localnav($outfile, "brief");
    print OUT "</h3>";

    open (FD, "< $thesd/${urlfile}_File_Doc.html")
	|| warn "FILE: $thesd/${urlfile}_File_Doc.html File in richdoc: $!\n";
    while (<FD>) {
	last if /<!-- BEGIN -->/;
    }
    $printing = 1;
    while (<FD>) {
	last if /<!-- END -->/;
	$printing = 0 if /<!-- ROUTINES -->/;
	$printing = 1 if /<!-- END ROUTINES -->/;
	s/<hr>//gi;
	# Cut out a tonne of troublesome crapola with the new navigation
	# paradigm.
	print OUT if $printing;
    }
    print OUT "<hr>";
    close FD;

    # Now do each of the routines.
    foreach $ROUTINE (sort cialph keys %routine_home) {
	next if (!($routine_home{$ROUTINE}  =~ m/$outfile/));
	open (RD, "< $thesd/${ROUTINE}_Routine_Doc.html")
	    || warn "ROUTINE: $thesd/${ROUTINE}_Routine_Doc.html in richdoc: $!\n";
	while (<RD>) {
	    last if /<!-- BEGIN -->/;
	}
	print OUT "<h3>\@routine: ";
	&localnav($ROUTINE, "brief", "$srcurl");
	print OUT "</h3>";
	while (<RD>) {
	    last if /<!-- END -->/;
	    s/<hr>//gi;
	    # Once again, lots of troubling stuff is gone
	    print OUT;
	}
	print OUT "<hr>";
    }
# Footer
    &navigation($dadots);
    print OUT <<EOM;
<hr>
$opt_c Rich Documentation Index<br>
$html_foot_date<br>
$organization / \n<a href=\"mailto:$author\">$author</a>
</body>
EOM
    close OUT;

    # Finally, convert the src to the RDsrc.
    # Protection
    $thesd =~ s:$opt_o:$opt_o/PROTECTED:;
    open(IN, "< $thesd/${urlfile}_src.html") || die "IN in rd\n";
    open(OUT,"> $thesd/${urlfile}_RD_src.html") || die "OUT in rd\n";
    # OK copy up to the first <hr>
    while (<IN>) {
	print OUT;
	s/h2/h3/g;
	last if /<hr>/;
    }

    # Now go through munging any file hrefs to point to the RD.
    $printing = 1;
    while (<IN>) {
	if (/<!-- GRDOC[^-]+-->/) {
	    s/<!-- GRDOC\s*([^-]+)-->.*$/$src_comment_start &gt;&gt; GRDoc Region for \1Supressed &lt;&lt  $src_comment_end/;
	    print OUT;
	    $printing = 0;
	}
	if (/<!-- END GRDOC -->/) {
	    s/^.*<!-- END GRDOC -->/<!-- END GRDOC -->/;
	    $printing = 1;
	}
	if (/<a href/) {
	    # OK, so first thing we want to do is find
	    # out what we have now
	    @refs = split("</a>");
	    $newline = "";
	    foreach $REF (@refs) {
		if ($REF =~ m/<a href=\"([^\"]+)\"/) {
		    $urlnow = $1;
		    $urlafter = $urlnow;
		    $urlafter =~ s:\#.*$::;
		    $urlafter =~ s:_src:_Rich_Doc:;
		    $REF =~ s:$urlnow:$urlafter:;
		    $newline = "$newline$REF</a>";
		} else {
		    $newline = "$newline$REF";
		}
	    }
	    $_ = $newline;
	}

	print OUT if $printing;
	last if /<hr>/;
    }

    # and finish the copying
    while (<IN>) {
	print OUT;
    }
    close IN;
    close OUT;
}

1;	
