#!/usr/local/bin/perl
#
#  roff-ms - generate a summary of a roff -ms file using semantic information
#
#  Interesting -ms Macros
#  -----------------------
#  .AB			begin abstract
#  .AE			end abstract
#  .AI			author's institution
#  .AU			author's name
#  .B t			bold tag
#  .B1			begin boxed text
#  .B2			end boxed text
#  .BT			bottom title
#  .BX x		box word x
#  .CT			chapter title
#  .I t			italics tag
#  .IP x i		indented paragraph with hanging tag
#  .IX a b ...		index words a b ...
#  .LP			Paragraph
#  .PP			Paragraph
#  .PT			Page Title
#  .SH  		section header
#  .TL  		Paper title
#
#  Interesting -ms Conventions
#  ----------------------------
#
if ($#ARGV > 0) {
	print STDERR "Usage: roff-ms filename\n";
	exit(1);
}
$tmpfile = "/tmp/roff-ms.$$";
$file = shift(@ARGV);
$still_input_line = 0;
open(INPUT, "< $file") || die "Cannot read $file";
open(OUT, "| deroff > $tmpfile") || die "Cannot write $tmpfile";
while (($still_input_line == 1) || ($_ = <INPUT>)) {
	$still_input_line = 0;
	do print_line() 	if (/^\.(BT|SH|CT|PT)/o);
	do print_paragraph() 	if (/^\.(AI|AU|TL)/o);
	do print_abstract()	if (/^\.AB/o);
	do print_boxtext()	if (/^\.B1/o);
}
close(INPUT);
close(OUT);
open(RESULT, "< $tmpfile") || die "Cannot read $tmpfile";
print <RESULT>;
close(RESULT);
unlink($tmpfile);
exit(0);

sub print_line {
	while (<INPUT>) {
		next if (/^\.(ce|br|sp)/o);
		print OUT;
		last;
	}
	$still_input_line = 0;
}

sub print_paragraph {
	$npara = 0;
	while (<INPUT>) {
		last if (/^\.SH/o);
		last if (/^\.(PP|LP)/o && $npara++ > 1);
		if (/^\.(TP|IP)\s+["]*(.*)["]*\n$/o) {
			print OUT "$2\n";
		} elsif (/^\.(B|I)\s+(.*)\n$/o) {
			print OUT "$2\n";
		} elsif (/^\.(BI|BR|IB|IR|RB|RI)\s+(.*)\s+(.*)\n$/o) {
			print OUT "$2$3\n";
		} else {
			print OUT;
		}
	}
	$still_input_line = 1;
}

sub print_abstract {
	while (<INPUT>) {
		last if (/^\.AE/o);
		if (/^\.IP\s+["]*(.*)["]*\n$/o) {
			print OUT "$1\n";
		} elsif (/^\.(B|I|BX)\s+(.*)\n$/o) {
			print OUT "$2\n";
		} else {
			print OUT;
		}
	}
}

sub print_boxtext {
	while (<INPUT>) {
		last if (/^\.B2/o);
		if (/^\.IP\s+["]*(.*)["]*\n$/o) {
			print OUT "$1\n";
		} elsif (/^\.(B|I|BX)\s+(.*)\n$/o) {
			print OUT "$2\n";
		} else {
			print OUT;
		}
	}
}
