#!/usr/bin/perl
# $Id: summary.in,v 1.11 2004/04/17 17:31:04 cjwatson 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") || die "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") || die "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) || die "failed to get time: $!";

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

$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}) || die "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 {
        die "badmode $mode";
    }
}

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

close(STDOUT) || die "close stdout: $!";
