#!/usr/bin/perl

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

=head1 NAME

    la-edit - Tools to edit forms (accreq)

=head1 SYNOPSIS

    la-edit [options] obj_id

=cut

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

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

Use this file to get attributes (- use stdin)
If this option is not present, you'll have a file opened in your prefered editor to edit.

=back

=cut

if (!$ARGV[0]) { warn "You must specify 'obj_id', aborting\n"; pod2usage(); }

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";
$labase->wexported(1);

sub input_from_handle {
    my ($obj, $fh) = @_;
    my $yaml = join('', <$fh>);
    my $res = $obj->set_c_fields(form => $yaml);
    if($res) {
        print "Changes applied\n";
        $labase->commit;
        $LA->call_batch_sync;
    }
    return
    $res ? 1 : 0;
}

if (my $ouid = shift(@ARGV)) {
    my $obj = $labase->get_object('accreq', $ouid) or do {
        die "Object accreq $ouid not found\n";
    };

    if ($inputfile) {
        my $handle;
        if ($inputfile eq '-') {
            $handle = *STDIN;
        } else {
            open($handle, '<', $inputfile) or die
                "Cannot open input file $@\n";
        }
        my $res = input_from_handle($obj, $handle);
        close($handle);
        exit(!$res);
    } else {
        exit ! LATMOS::Accounts::Utils::dump_read_temp_file(
            sub {
                my ($fh) = @_;
                print $fh $obj->get_attributes('form');
            },
            sub {
                my ($fh) = @_;
                return input_from_handle($obj, $fh);
            }
        );
    }
} else {
    pod2usage();
}

=head1 FORM FORMAT

=head2 EXAMPLE

    ---
    attrs:
      - formtype: textarea
        label: Please explain why your name must changed
        name: reason
      - sn
      - givenName
    description: Change my name
    notifyMail: sysadmin@nomail

=head2 SYNTAX

The form must be describe in YAML format (L<TODO: Wikipedia link>).

=head2 OPTIONS

=over 4

=item description

A short description about this form

=item notifyMail

If set a mail is sent to this address when someone submit the form

=item attrs

The list of attributes the form must contains. See below.

=back

=head2 ATTRIBUTES DESCRIPTION

Each attributes is either:

=over 4

=item a name

Just the name of an object's attribute the is related (C<sn>, C<givenName> for user objects)

=item an option list

A list of options describing how the attribute must appear:

=over 4

=item name

The internal short name of attribute

=item label

The text displayed to user

=item formtype

The type of field to diaply (text, textarea, date, checkbox or list)

=back

=back

=cut
