#! /usr/bin/perl -w

# vim:syntax=perl

use strict;

use lib '/usr/share/perl5';

use Locale::TextDomain qw/lire/;

use Lire::Config;
use Lire::Error qw/ an_error_occured missing_argument_usage /;
use Lire::PluginManager;
use Lire::DlfStore;

Lire::Config->init();
Lire::PluginManager->register_default_plugins();

my ( $period, $store_path, $time ) = @ARGV;

my $usage = __( "Usage: lr_cron <period> <store>\n" );

die ( missing_argument_usage( 'period' ),
      $usage )
  unless defined $period;

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

die ( missing_argument_usage( 'store' ),
      $usage )
  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, $time ) if $job->period() eq $period;
    }
    foreach my $job ( @{ $store->report_jobs() } ) {
        $job->run( $store, $period, $time );
    }
    $store->clean_streams( $time )
      if $period eq 'daily';
    $store->close();
};
die an_error_occured( $@ ) 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".
I<store> should be the full path name of the DLF Store.

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

To run jobs as scheduled by lire(1), you will need to setup cron jobs. For each
schedule you use, you should configure a cron job to be executed according to
that period.

Here is a crontab(5) entry that will make sure that all possible
schedules are supported in the /home/flacoste/myStore DLF store:

 05  *  *  *  * lr_run lr_cron hourly  /home/flacoste/myStore
 15 01  *  *  * lr_run lr_cron daily   /home/flacoste/myStore
 15 02  *  * 02 lr_run lr_cron weekly  /home/flacoste/myStore
 15 03 01  *  * lr_run lr_cron monthly /home/flacoste/myStore
 15 04 01  01 * lr_run lr_cron yearly  /home/flacoste/myStore

.

=head1 SEE ALSO

lire(1)

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=head1 VERSION

$Id: lr_cron.in,v 1.37 2006/07/23 13:16:32 vanbaal 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.

=cut

Local Variables:
mode: cperl
End:
