#!/usr/bin/perl

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

=head1 NAME

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

=head1 SYNOPSIS

    la-query [options] [obj_id]

=item [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id.
        If none is given, all obj_ids will be printed.

For the default object_type (user), obj_id = login.

Example : la-query lambda

=cut

GetOptions(
    'c|config=s' => \my $config,
    'b|base=s'   => \my $base,
    'o|object=s' => \my $otype,
    'e|empty'    => \my $empty_attr,
    'fmt=s'      => \my $fmt,
    'filefmt=s'  => \my $filefmt,
    'ro'         => \my $with_ro,
    '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

Query this specific base instead of the default one.

=item -o|object object_type

Query will be performed on this object. Default is the 'User' object.

=item -e|--empty

(Only if obj_id is specified) Include also unset attributes.

=item --ro

(Only if obj_id is specified) Include also read-only attributes as comments.

=item --fmt format

Specify a format of output.

If NOT present and "obj_id" is specified, all non-empty writable attributes will be printed (modified by -e and -ro).
If NOT present and "obj_id" is NOT specified, only the list of "obj_id" will be printed.

The "format" string may query tags in form:

    %{ATTRIBUTE:printflike}

Where ATTRIBUTE is the name of an attribute and the optional
C<:printflike> is a printf like string to print the attribute content (without
the C<%>).

Examples for users:

    --fmt "Name %{sn}, First Name %{givenName}\n"

    --fmt "Name %{sn:20s}, First Name %{givenName}\n"

=item --filefmt format-file

"format-file" is a text file with the same strings as "--fmt" option, WITHOUT quotes (").

It can be on more than one line, but all lines will be concatenated as one.
Don't forget the "\n" if you really want seperate lines.

Example of format-file:

Name: %{sn}, First Name: %{givenName}\n
     Department : %{department}\n

Note : --fmt and --filefmt can be used together, and will be concatenated (the "fmt" string first, then what is in the "format-file").

=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 ($filefmt){
        open my $hfmt, $filefmt or die "Cannot open $filefmt\n";
        $fmt ||= ''; # avoid undef warning
        while (<$hfmt>) {
                chomp($fmt .= $_);
        }
        close $hfmt;
}

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