#!/usr/bin/perl

use strict;
use warnings;

my $total = 0;
my %maint = ();
my %upload = ();
my %all = ();
my $cur;
my %src_up = ();
my %src_t = ();
my $team = 0;

while ( my $line = <> ) {
    chomp $line;
    if ($line eq '') {
        $cur = undef;
        next;
    }
    if ($line =~ m/^\s/o) {
        die "Missing maintainer before line $.: $line\n"
            unless defined $cur;
        my $src = $line;
        $src =~ s/^\s++//o;
        $src =~ s,\s*+(?:\Q(U)\E\s*+)?$,,o;
        $src_up{$src}{$cur} = 1;
        $src_t{$src} = 0 unless exists $src_t{$src};
        $src_t{$src} = 1 if $team;
    } elsif ($line =~ m/\<([^>]++)>$/o or $line eq '"Adam C. Powell') {
        my $email = lc ($1//'hazelsct@debian.org');
        $cur = $email;
        $team = 0;
        $team = 1 if $cur =~ m/\@lists/;
    } else {
        die "Cannot parse line $.: $line\n";
    }
}

my @list = sort {
        $src_t{$a} <=> $src_t{$b} ||
        scalar keys %{ $src_up{$a} } <=> scalar keys %{ $src_up{$b} } 
    } keys %src_up;

print "# Lists packages by the \"number of maintainers\"\n";
print "#  - sorted by team-maint and then number\n";
print "# <src> <no-maint> [(team-maintained)]\n";

foreach my $src (@list) {
    print "$src " . scalar (keys %{ $src_up{$src} });
    print " (team-maintained)" if $src_t{$src};
    print "\n";
}

