#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts;
use Getopt::Long;
use Pod::Usage;
use Term::ReadKey;

=head1 NAME

    la-sql-attrvalue - Set limited choice to attributes

=head1 SYNOPSIS

    la-rename [options] attribute [value]

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base basename

Query this specific base instead of the default one.

=item -o|--otype otype

Modify object having type 'otype', default is user

=item -r|--remove

Remove the given value (or all if no value are given)

=back

=cut

GetOptions(
    'c|config=s' => \my $config,
    'b|base=s'   => \my $base,
    't|test'     => \my $test,
    'o|otype=s'  => \my $otype,
    'r|remove'   => \my $remove,
    'help'       => sub { pod2usage(0) },
) or pod2usage();

my ($attribute, @values) = @ARGV;

if (!$attribute) { pod2usage(); }

$otype ||= 'user';

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase && $labase->load or die "Cannot load base\n";

if ($remove) {
    $labase->DelAttrValue($otype, $attribute, @values) or die "Cannot remove this value";
} else {
    if (@values) {
        $labase->AddAttrValue($otype, $attribute, @values) or die "Cannot add this value";
    }

    print "Otype: $otype, Attribute: $attribute\n";
    foreach ($labase->ListAttrValue($otype, $attribute)) {
        print "\t$_\n";
    }
}


$labase->commit;
