#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Config::IniFiles;
use LATMOS::Accounts;
use LATMOS::Accounts::SyncManager;
use LATMOS::Accounts::Log;
use LATMOS::Accounts::Task;

=head1 NAME

    la-sync-manager - Daemon performing synchronisation and various tasks

=head1 SYNOPSIS

    la-sync-manager [options...]

=cut

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

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration instead default one

=item --help

Display this help

=item --reset|-r module

Reset the check point to force the module to be rerun even no change occurs in
database.

If the given module name is C<all>, all module get reset.

=item --sync|-s

Tell to L<la-sync-manager> daemon to immediatly perform a synchronisation.

=back

=cut

my $exit = 0;

if ($reset) {
    my $syncm = LATMOS::Accounts::SyncManager->new($config) or do {
        la_log LA_ERR, "Cannot instanciate Sync Manager";
        exit(1);
    };
    foreach ($reset eq 'all' ? ($syncm->list_modules) : ($reset)) {
        $syncm->reset_module_checkpoint($_) or $exit=1;
    }
}

if ($sync) {
    my $LA = LATMOS::Accounts->new($config, noacl => 1);
    $LA->call_batch_sync or $exit=1;
}

exit($exit);

__END__

=head1 SEE ALSO

L<la-sync-manager.ini>, L<latmos-accounts.ini>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
