#!/usr/bin/perl -w

use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use File::Basename;
use ALTLinux::ACL;

my $verbose=0;
my $help=0;
my $column=1;
my $nvr_mode;

my $result = GetOptions (
    @ALTLinux::ACL::LONGOPT,
    "help"  => \$help,
    'quiet'=> sub {$verbose=0},
    "verbose+"  => \$verbose,
    "column=i"  => \$column,
    "nvr"  => \$nvr_mode,
);

if ($help) {
    pod2usage();
}

my $acl = ALTLinux::ACL->new();
# in perl, column counting is from 0.
$column--;
while (<>) {
    next if /^\s*$/;
    my @line=split(/\s+/,$_);
    my $name=basename($line[$column]);
    next unless $name; # empty line ?
    $name=~s/-[^-]+-[^-]+$// if $nvr_mode;
    my @acl_list=$acl->acl($name);
    warn "$name: the package has no acl\n" unless @acl_list;
    print STDERR "name=$name acl: ",join(',',@acl_list),"\n" if $verbose;
    print $acl_list[0]."\t$_";
}

=head1	NAME

altlinux-acl-filter-list-prepend-leader - prepend altlinux acl leader to the list of names.

=head1	SYNOPSIS

B<altlinux-acl-filter-list-prepend-leader>
[B<-h|--help>]
[B<-q|--quiet>]
[B<-v|--verbose>]
[B<--nvr>]
[B<-c|--column> I<N>]

=head1	DESCRIPTION

B<altlinux-acl-filter-list-prepend-leader>
is a program that reads STDIN and files given as its arguments
and prepends leader's name according to altlinux acl to each line.
The lines are treated as a text table separated by space class symbols.
The forst column or the column set by --column option is used as input
that is expected to be a src.rpm name.

=head1	OPTIONS

=over

=item	B<-h, --help>

Display this help and exit.

=item	B<-c, --column> I<N>

The column containing source rpm name (counting from 1).

=item	B<--nvr>

NVR mode. In this mode, the input is not a srpm name, but
is expected to look like name-version-release.something,
like foo-1.0-alt1, foo-1.0-alt1.log, foo-1.0-alt1.noarch,
foo-1.0-alt1.noarch.patch.

=item	B<-v, --verbose>, B<-q, --quiet>

Verbosity level. Multiple -v increase the verbosity level, -q sets it to 0.

=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	COPYING

Copyright (c) 2009-2023 Igor Vlasenko, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License, or (at your option) any later version.

=cut
