#!/usr/local/bin/perl

$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';
} else {
    $cmd = 'groff -t -T' . $opt_o . ' -ms';
}

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

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

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

&report_init;
while (<>) {
    chop;
    @tmp = split;
    &report($tmp[0]);
}
&report_quit;

close(OUT);
waitpid($pid, 0);

exit(0);

# SUMMARY

sub report_init {
    local($date);

    $date = `date`;
    chop($date);
    print OUT <<EOF;
.\\" title section
.TL
Problem Report Summary - $date
.\\" format section
.ds CF \\\\n(mo / \\\\n(dy / \\\\n(yr
.sp 4
.TS H
box;
L|L|L|L|L|Lw(2i)
L|L|L|L|L|^
L|L|L|L|L|L.
.\\" table heading
Ident\tLast Change\tResponsible\tCategory\tSeverity\tSynopsis
\tArrived\tOriginator\tState\tPriority
=
.TH
EOF
}

sub report {
    local($id) = @_;
    local(%rec);
    local($gnat, $cat, $syn, $conf, $sev, $pri);
    local($resp, $st, $class, $arrive_date, $orig);
    local($last_user, $last_date);
    local($sel_sv);
    local($x, $tmp);

    if (! &gnats'parse_full(*rec, $id)) {
	return(0);
    }
    $sel_sv = select(OUT);
    $gnat = &gnats'file2id($id);
    $cat = $rec{'>Category'};
    $cat = &trim($cat);
    $syn = $rec{'>Synopsis'};
    $syn = &trim($syn);
    $syn =~ s@\n@ @g;
    $conf = $rec{'>Confidential'};
    $conf = &trim($conf);
    $conf = ($conf eq 'no') ? '' : 'CONF';
    $sev = $rec{'>Severity'};
    $sev = &trim($sev);
    $pri = $rec{'>Priority'};
    $pri = &trim($pri);
    $resp = $rec{'>Responsible'};
    $resp = &trim($resp);
    $st = $rec{'>State'};
    $st = &trim($st);
    $class = $rec{'>Class'};
    $class = &trim($class);
    $arrive_date = $rec{'>Arrival-Date'};
    $arrive_date =~ s@ \d+:\d+:\d+@@;
    $arrive_date = &trim($arrive_date);
    $orig = $rec{'>Originator'};
    $orig = &trim($orig);

    $last_date = $arrive_date;
    $last_user = $orig;
    $tmp = $rec{'>Audit-Trail'};
    if (defined($tmp)) {
	local(@tmp, $got_u, $got_d);

	$got_u = $got_d = 0;

	@tmp = reverse split(/\n/, $tmp);
	foreach $x (@tmp) {
	    if ($x =~ m@^(State|Responsible)\-Changed\-When: (.*)$@) {
		$tmp = $2;
		$tmp =~ s@ \d+:\d+:\d+ (\w\wT)?@@;
		$last_date = $tmp;
		$got_d = 1;
	    } elsif ($x =~ m@^(State|Responsible)\-Changed\-By: (.*)$@) {
		$last_user = $2;
		$got_u = 1;
	    }
	    last if ($got_u && $got_d);
	}
    }

#    $tmp = $rec{'>X-Journal'};
#    if (defined($tmp)) {
#	$tmp =~ m@\n%(\a{3} \a{3} \d{2}) \d{2}:\d{2}:\d{2} \a{3} (\d{4})%(\w+)%[0-9a-zA-Z ]+%$@;
#	$last_date = "$1 $2";
#	$last_user = $3;
#    }

    print "\\fB$gnat\\fR\t$last_date\t$resp\t$cat\t$sev\tT{\n$syn\nT}\n$conf\t$arrive_date\t$orig\t$st\t$pri\t\\^\n_\n";
    select($sel_sv);
    1;
}

sub report_quit {
print OUT <<EOF;
.TE
EOF
}

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

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