#!/usr/local/bin/perl
#
#  roff-man - generate a summary of a roff -man file using semantic information
#
#  Interesting -man Macros
#  -----------------------
#  .B t			bold tag
#  .BI t		bold and italics tag
#  .BR t		bold and roman tag
#  .I t			italics tag
#  .IB t		bold and italics tag
#  .IR t		italics and roman tag
#  .IP x i		indented paragraph with hanging tag
#  .RB t		bold and roman tag
#  .RI t		italics and roman tag
#  .PP			Paragraph
#  .SH t		subheading
#  .TH n c x v m	page named n, chapter c, commentary x; foot left v,
#			head center m
#  .TP x 		indented paragraph with hanging tag
#
#  Interesting -man Conventions
#  ----------------------------
#  .SH NAME		Name and brief description
#  .SH SYNOPSIS		Usage
#  .SH DESCRIPTION	Long description
#  .SH AUTHOR		Author(s)
#  .SH FILES		Related files
#  .SH SEE ALSO		Related commands and documents
#
if ($#ARGV > 0) {
	print STDERR "Usage: roff-man filename\n";
	exit(1);
}
$tmpfile = "/tmp/roff-man.$$";
$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 (/^\.SH ["]*NAME["]*/o);
	do man_th() 		if (/^\.TH/o);
	if ((/^\.SH ["]*SYNOPSIS["]*/o) || (/^\.SH ["]*DESCRIPTION["]*/o) ||
	    (/^\.SH ["]*AUTHOR[S]["]*/o) || (/^\.SH ["]*FILES["]*/o) ||
	    (/^\.SH ["]*SEE ALSO["]*/o)) {
		do print_paragraph();
	}
}
close(INPUT);
close(OUT);
open(RESULT, "< $tmpfile") || die "Cannot read $tmpfile";
while (<RESULT>) {
	s/\([1-8][a-zA-Z]*\)//g;
	print;
}
close(RESULT);
unlink($tmpfile);
exit(0);


sub man_th {
	@line = split(/[\s"]/, $_);
	shift(@line);
	foreach $word (@line) {
		print "$word\n" if ($word ne "");
	}
	$still_input_line = 0;
}

sub print_line {
	$_ = <INPUT>;
	print OUT;
	$still_input_line = 0;
}

sub print_paragraph {
	$npara = 0;
	while (<INPUT>) {
		next if (/^\.so/o);
		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;
}
