#!/usr/local/bin/perl
#
#  roff-generic - generate a summary of a roff file using semantic information
#
#  print centered text, the first 3 paragraphs, and the first line of 
#  every paragraph
#
if ($#ARGV > 0) {
	print STDERR "Usage: roff-generic filename\n";
	exit(1);
}
$tmpfile = "/tmp/roff-generic.$$";
$file = shift(@ARGV);
$np = 0;	# number of paragraphs printed
open(INPUT, "< $file") || die "Cannot read $file";
open(OUT, "| deroff > $tmpfile") || die "Cannot write $tmpfile";
while (<INPUT>) {
	if (/^\.ce/o) {
		do print_paragraph();
	} elsif (/^\.(lp|pp|LP|PP)/o) {
		if ($np < 3) {
			do print_paragraph();
			$np++;
		} else {
			do print_intro();
		}
	}
}
close(INPUT);
close(OUT);
open(RESULT, "< $tmpfile") || die "Cannot read $tmpfile";
print <RESULT>;
close(RESULT);
unlink($tmpfile);
exit(0);

sub print_paragraph {
	while (<INPUT>) {
		last if (/^\.(ce|lp|pp|LP|PP|sp|br|bp|vp)/o);
		next if (/^\.so/o);
		print OUT;
	}
}

sub print_intro {
	$_ = <INPUT>;
	print OUT if (!/^\.so/o);
}
