#!/usr/bin/perl -w

my @files;
my ($help, $plain, $aggregate);

for $a (@ARGV) {
    if ($a eq '--help' || $a eq '/?') {
	$help = 1;
    } elsif ($a eq '--plain' || $a eq '-p') {
	$plain = 1;
    } elsif ($a eq '--aggregate' || $a eq '-a') {
	$aggregate = 1;
    } else {
	push @files, $a;
    }
}

$help = 1 if !@files;

if ($help) {
    print "owner [--plain(-p)] [--aggregate(-a)] <files...>\n";
    print "  --plain     don't append \@openoffice.org to addresses\n";
    print "  --aggregate uniquify names\n";
    exit 0;
}

system('cvs --version > /dev/null 2>&1') && die "Can't find cvs tool";

# Synchronous - but so is life.
sub get_owner($$)
{
    my ($path, $file) = @_;
    my $owner = '<unknown>';
    my $status = `cd $path ; cvs -z3 status -v $file`;

    defined $status || die "couldn't execute cvs";

    if ($status =~ m/ADMIN_FILE_OWNER_(\S+)/) {
	$owner = lc($1);
    }

    return $owner;
}

my %owners = ();

for (@files) {
    -f $_ || die "$! : $_";
    m/(.*)\/([^\/]+)/ || die "Odd filename $_";
    my $path = $1;
    my $file = $2;
    my $owner = get_owner ($path, $file);

    if ($aggregate) {
	defined $owners{$owner} && next;
	$owners{$owner} = 1;
    } else {
	print "$path/$file : ";
    }
    print "$owner";
    $plain || print '@openoffice.org';
    print "\n";
}
