#!/usr/bin/perl

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

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

$otype ||= 'user';

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

foreach ($labase->search_objects($otype, company => 'LATMOS')) {
    print "$_\n";
    my $obj = $labase->get_object($otype, $_);
    my $mail = $obj->get_c_field('givenName') . '.' .
        $obj->get_c_field('sn') . '@latmos.ipsl.fr';
    print "$mail\n";
    $obj->set_c_fields(mail => $mail);
}
$labase->commit;
