#!/usr/bin/perl

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

=head1 NAME

    la-sql-recompute - Update computed attribute

=head1 SYNOPSIS

    la-sql-upgrade [options]

=cut

GetOptions(
    'c|config=s'     => \my $config,
    'b|base=s'       => \my $base,
    'no-commit'      => \my $nocommit,
    'v|verbose'      => \my $verbose,
    'h|help'         => sub { pod2usage(1); },
) 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.

=back

=cut

$| = 1; # autoflush

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase->wexported(1);

foreach my $user ($labase->list_objects('user')) {
    print "Updating user $user\n";
    my $ouser = $labase->get_object('user', $user) or next;
    $ouser->computeEmploymentDate();
}

$labase->PopulateDynData;

if ($nocommit) {
    $labase->rollback;
} else {
    $labase->commit;
}
print "Process terminated successfully\n";
