#!/usr/bin/perl -w

use strict;
use warnings;
#use File::Find;
#use RPM::Header;
use Getopt::Long;
use Pod::Usage;
#use File::Basename;
use ALTLinux::ACL;

my $verbose=0;
my $help=0;
my $noaccess;
my $user;

my $result = GetOptions (
    'quiet'=> sub {$verbose=0},
    "verbose+"  => \$verbose,
    "access"  => sub {$noaccess=0},
    "n|noaccess|no-access"  => sub {$noaccess=1},
    "help"  => \$help,
    "user=s"  => \$user,
);

if ($help) {
    pod2usage();
}

$user||= ALTLinux::ACL::email2aclname(`rpm --eval '%{packager}'`);
print "user=$user\n" if $verbose;

my $acl = ALTLinux::ACL->new();
while (<>) {
    my @line=split(/\s+/,$_);
    my $name=$line[0];
    next unless $name; # empty line ?
    print "name=$name acl-expanded: ",join(',',$acl->acl_expanded($name)),"\n" if $verbose;
    print "$name\n" if $noaccess xor $acl->is_authorized($name,$user);
}

=head1	NAME

altlinux-acl-filter - filter list of names according to altlinux acl.

=head1	SYNOPSIS

B<altlinux-acl-filter>
[B<-n|--no-access>]
[B<-h|--help>]
[B<-q|--quiet>]
[B<-v|--verbose>]
[B<-u|--user> I<name>]

=head1	DESCRIPTION

B<altlinux-acl-filter> 

=head1	OPTIONS

=over

=item	B<-n, --no-access> 

Revert filter; print names that user has no access to.

=item	B<-h, --help>

Display this help and exit.

=item	B<-u, --user> I<name>

ACL user.

=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 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

