#! /usr/bin/perl
# 
# INCOMPLETE: Sketch of a program to extract the collective algorithms
# from the source files
#
$is_html = 1;
$html_page = 1;

if ($html_page) {
    print "<html><head>
<title>Algorithms for Collective Routines</title>
</head><body bgcolor=\"FFFFFF\">\n";
}

while (<>) {
    if (/Algorithm:\s*([\w_\d]*)/) {
	$name = $1;
	# Remove spaces from name
	$name =~ s/^\s*//;
	$name =~ s/\s*$//;
	if ($name eq "") { next; }
	&PrintHeading( "Algorithm for $name" );
	while (<>) {
	    if (/End [Aa]lgorithm:\s*([\w_\d]*)/) {
		$endname = $1;
		if ($name ne $endname) {
		    print STDERR "Algorithm started with $name but ended with $endname\n";
		}
		last;
	    }
	    # This should eventually handle the formatting modifications
	    &PrintText( $_ );
	}
    }
}

if ($html_page) {
    print "</body></html>\n";
}
$at_heading = 0;
sub PrintHeading { 
    my $line = $_[0];
    
    if ($is_html) {
	print "<h2>$line</h2>\n";
    }
    else {
	print "$line:\n";
    }
    $at_heading = 1;
}
sub PrintText {
    my $line = $_[0];
    if ($is_html) {
	if ($line =~ /^\s*$/) {
	    if (!$at_heading) {
		print "<br>\n";
	    }
	}
	else {
	    # Fixup any special characters
	    $line =~ s/</\*AMP\*lt;/g;
	    $line =~ s/>/\*AMP\*gt;/g;
	    $line =~ s/&/\*AMP\*amp;/g;
	    $line =~ s/\*AMP\*/&/g;
	    # Fixup any formatting commands
	    $line =~ s/^\s*.vb/<PRE>/;
	    $line =~ s/^\s*.ve/<\/PRE>/;
	    print $line;
	}
    }
    else {
	if ($line =~ /^\s*.vb/ || $line =~ /^\s*.ve/) {
	    next;
	}
	print "$line";
    }
    $at_heading = 0;
}
