#!/usr/bin/perl

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

=head1 NAME

    la-query-group - Tools to query base in LATMOS::Accounts system

=head1 SYNOPSIS

    la-query-group [options] [obj_id]

=cut

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

$otype ||= 'user';

=head1 OPTIONS

=over 4

=item -c|--config configfile

Use this configuration file instead of the default one.

=item -b|--base basename

=back

=cut

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

if (my $ouid = shift(@ARGV)) {
    my $obj = $labase->get_object($otype, $ouid) or do {
        die "Object $otype $ouid not found\n";
    };
    $obj->text_dump(*STDOUT,
        {
            empty_attr => $empty_attr,
            only_rw => !$with_ro,
        }
    );
} else {
    foreach (sort $labase->list_objects($otype)) {
        print "$_\n";
    }
}
