#! /usr/bin/perl -w

# vim:syntax=perl

use strict;

use lib '/usr/share/perl5';

use Locale::TextDomain qw/lire/;

use Lire::Config;
use Lire::DlfConverterManager;
use Lire::DlfStore;

Lire::Config->init();
Lire::DlfConverterManager->instance()->register_default_converters();

my ( $period, $store_path, $time ) = @ARGV;
die ( __( "Missing 'period' argument.\n" ),
      __("Usage: lr_cron <period> <store>\n") )
  unless defined $period;

die ( __x( "Invalid period '{period}'. Should be one of hourly, daily, weekly, monthly or yearly.\n", 'period' => $period ),
      __("Usage: lr_cron <period> <store>\n") )
  unless $period =~ /^(hourly|daily|weekly|monthly|yearly)$/;

die ( __( "Missing 'store' argument.\n" ),
      __("Usage: lr_cron <period> <store>\n") )
  unless defined $store_path;

# Since lr_cron is run once the period is over, we need to
# offset it.
$time ||= time();
if ( $period eq 'hourly' ) {
    $time -= 3600;
} else {
    $time -= 86400;
}
eval {
    my $store = Lire::DlfStore->open( $store_path, 1 );
    foreach my $job ( @{ $store->import_jobs() } ) {
        $job->run( $store ) if $job->period() eq $period;
    }
    foreach my $job ( @{ $store->report_jobs() } ) {
        $job->run( $store, $period, $time );
    }
    $store->close();
};
die __x( 'An error occured: {error}', 'error' => $@ ) if $@;

exit(0);

__END__

=pod

=head1 NAME

lr_cron - run the perodical jobs configured in a DlfStore.

=head1 SYNOPSIS

B<lr_cron> I<period> I<store>

=head1 DESCRIPTION

B<lr_cron> is designed to be called in a crontab(5) entry.
I<period> should be "hourly", "daily", "weekly", "monthly" or "yearly".

lire(1) should be used to configure periodical jobs in the store.

=head1 SEE ALSO

lire(1)

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=head1 VERSION

$Id: lr_cron.in,v 1.28 2004/04/07 18:21:55 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2004 Stichting LogReport Foundation
LogReport@LogReport.org

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=cut

Local Variables:
mode: cperl
End:
