#!/usr/local/bin/perl

# Reformat table-of-contents file for NROFF output

print STDOUT "<!-- NROFF\n";
print STDOUT ".nf\n";
print STDOUT "NROFF -->\n";
while (<STDIN>) {
	&process_line($_);
}
print STDOUT "<!-- NROFF\n";
print STDOUT ".fi\n";
print STDOUT "NROFF -->\n";
exit;

sub process_line {
    local($line) = @_;

    if ($line =~ /^ *\* /) {
	# adjust indentation depth at start of line
	$line =~ s|^                \* |       |;
	$line =~ s|^          \* |   |;
	$line =~ s|^    \* ||;

	# Expand first ! into space and N dots, until second ! falls off end
        $line =~ s|!| !|;
	while ($line =~ /!$/) {
	    $line =~ s|!|.!|;
	    $line =~ s|(.....................................................................)!|$1|;
	}
	$line =~ s|!| |;

        print STDOUT $line;
    }
}
