#!/usr/bin/perl
# $Id: summary.in,v 1.8 2003/05/03 20:01:20 doogie Exp $

$config_path = '/etc/debbugs';
$lib_path = '/usr/lib/debbugs';

require("$config_path/config");
require("$lib_path/errorlib");
$ENV{'PATH'} = $lib_path.':'.$ENV{'PATH'};

chdir("$gSpoolDir") || die "chdir spool: $!\n";

#open(DEBUG,">&4");

$mode= shift(@ARGV);

open(M,"$gMaintainerFile") || &quit("open $gMaintainerFile: $!");
while (<M>) {
    m/^(\S+)\s+(\S.*\S)\s*$/ || warn "$_ ?";
    ($a,$b)=($1,$2);
    $a =~ y/A-Z/a-z/;
    $maintainer{$a}= $b;
}
close(M);
open(M,"$gMaintainerFileOverride") || &quit("open $gMaintainerFileOverride: $!");
while (<M>) {
    m/^(\S+)\s+(\S.*\S)\s*$/ || warn "$_ ?";
    ($a,$b)=($1,$2);
    $a =~ y/A-Z/a-z/;
    $maintainer{$a}= $b;
}
close(M);
		

defined($startdate= time) || &quit("failed to get time: $!");

opendir(DIR,"db-h") || &quit("opendir db-h: $!\n");
@dirs = sort { $a <=> $b } grep(s,^,db-h/,, grep(m/^\d+$/,readdir(DIR)));
closedir(DIR);
foreach my $dir (@dirs) {
        opendir(DIR,$dir);
        push @list, sort { $a <=> $b } grep(s/\.status$//,grep(m/^\d+\.status$/,readdir(DIR)));
        closedir(DIR);
}

$head= $mode eq 'bymaint'
    ? ' Package     Ref    Subject'
    : ' Ref   Package    Keywords/Subject               Package maintainer';
$amonths=-1;

while (length($f=shift(@list))) {
    if (!($data = lockreadbug($f))) { next; }
    $_= $data->{package}; y/A-Z/a-z/; $_= $` if m/[^-+._a-z0-9]/;
    $data->{maintainer}=
        defined($maintainer{$_}) ? $maintainer{$_} :
        length($_) ? "(unknown -- \`$_')" :
        "(unknown)";
    if ($mode eq 'undone' || $mode eq 'veryold') {
        &unfilelock;
        next if length($data->{done}) || length($data->{forwarded});
        $cmonths= int(($startdate - $data->{date})/2592000); # 3600*24*30 (30 days)
        next if $mode eq 'veryold' && $cmonths < 2;
        if ($cmonths != $amonths) {
            $msg= $cmonths == 0 ? "Submitted in the last month" :
                  $cmonths == 1 ? "Over one month old" :
                  $cmonths == 2 ? "Over two months old - attention is required" :
                                  "OVER $cmonths MONTHS OLD - ATTENTION IS REQUIRED";
            print "\n$msg:\n$head\n";
            $amonths= $cmonths;
        }
        printf("%6d %-10.10s %-30.30s %-.31s\n", $f, $data->{package},
               (length($data->{keywords}) ? $data->{keywords}.'/' : '').$data->{subject},
               $data->{maintainer}) || &quit("output undone: $!");
    } elsif ($mode eq 'bymaint') {
        &unfilelock;
        next if length($data->{done}) || length($data->{forwarded});
        $string{$f}=
            sprintf(" %-10.10s %6d  %-.59s\n", $data->{package}, $f, $data->{subject});
        $data->{maintainer}= "(unknown)" if $data->{maintainer} =~ m/^\(unknown \-\-/;
        $maintainercnt{$data->{maintainer}}++;
        $maintainerlist{$data->{maintainer}}.= " $f";
    } else {
        &quit("badmode $mode");
    }
}

if ($mode eq 'bymaint') {
    print("$head\n") || &quit("output head: $!");
    for $m (sort { $maintainercnt{$a} <=> $maintainercnt{$b} } keys %maintainercnt) {
        printf("\n%s (%d $gBugs):\n",$m,$maintainercnt{$m})
            || &quit("output mainthead: $!");
        for $i (sort { $string{$a} cmp $string{$b} } split(/ /,$maintainerlist{$m})) {
            printf($string{$i}) || &quit("output 1bymaint: $!");
        }
    }
}

close(STDOUT) || &quit("close stdout: $!");
