#!/usr/local/bin/perl

# add class

$tkgnatslib = '/usr/local/lib/tkgnats';
#$tkgnatslib = '.';

unshift(@INC, $tkgnatslib);

require("getopts.pl");
require("gnats.pl");

&gnats'filter_init($tkgnatslib, *usage);

if ($opt_o eq 'nroff') {
    $cmd = 'tbl | nroff -ms';
} elsif ($opt_o eq 'otroff') {
    $cmd = 'tbl | troff -t -ms';
} elsif ($opt_o eq 'debug') {
    $cmd = 'cat';
} else {
    $cmd = 'groff -t -ms -T' . $opt_o;
}

$date = `date`;
chop($date);

if (defined($opt_p)) {
    $cmd .= "| lpr -P$opt_p";
} else {
    $cmd .= "> $opt_f";
}

open(OUT, '|' . $cmd) || die "Cannot spawn [$cmd] for writing:  $!\n";

while (<>) {
    next if (m/^\s*$/);
    chop;
    @tmp = split;
    push(@ids, $tmp[0]);
}
&report_init(scalar(@ids));
foreach $tmp (@ids) {
    &report($tmp);
}
&report_quit;

exit(0);

# SUMMARY

sub report_init {
    local($num) = @_;
    local($date, $sep);

    $date = `date`;
    chop($date);
    $sep = (($opt_o eq 'ascii') || ($opt_o eq 'latin1')) ? ' ' : '|';
    print OUT <<EOF;
.\\" title section
.TL
Problem Report Summary - $date
.\\" format section
.ds CF \\\\n(mo / \\\\n(dy / \\\\n(yr
.sp 4
.TS H
EOF
    ($opt_o eq 'ascii') || ($opt_o eq 'latin1') || print OUT "box;\n";
    print OUT <<EOF;
L${sep}Lw(5.5i)
L${sep}^
L S
EOF
    if ($num > 0) {
	--$num;
	for ($x = 0; $x < $num; ++$x) {
	    print OUT "L${sep}L\nL${sep}^\nL S\n";
	}
	print OUT "L${sep}L\nL${sep}^\nL S.\n";
    }
    print OUT <<EOF;
Ident	Synopsis
_
Status
_
Description
=
.TH
EOF
}

sub report {
    local($id) = @_;
    local(%rec);
    local($gnat, $syn, $conf, $st, $desc);

    if (! &gnats'parse_full(*rec, $id)) {
	return(0);
    }
    $gnat = &gnats'file2id($id);
    $syn = &trim($rec{'>Synopsis'});
    $st = &trim($rec{'>State'});
    $conf = $rec{'>Confidential'};
    $conf = &trim($conf);
    $conf = ($conf eq 'no') ? '' : ' CONF';
    $desc = $rec{'>Description'};
    while (chop($desc) == 10) { ; }
    print OUT <<EOF;
.sp .5
\\fB#$gnat$conf\\fR\tT{
$syn
T}
_
$st
_
T{
$desc
T}
=
EOF
    1;
}

sub report_quit {
    print OUT ".TE\n";
}

sub trim {
    local($x) = @_;
    local($s) = $*;

    $* = 1;
    $x =~ s@^\s+@@;
    $x =~ s@\s+$@@;
    $* = $s;
    $x;
}
