#! /usr/bin/perl -w

# vim:syntax=perl

#-----------------------------------------------------------
# Build DLF from a SQL table
#-----------------------------------------------------------

use strict;

use DBI;
use Time::Local;

use lib '/usr/local/share/perl5';

use Lire::Program qw/:msg $PROG/;
use Lire::Config;

#---------------------------------------------------------
# Main
#---------------------------------------------------------

lr_info("Starting sql2dlf ");

lr_err("Usage: $PROG <superservice> <dlf-file> <date_begin> <date_end>")
 unless @ARGV == 4;

my ($superservice, $dlf_file, $date_begin, $date_end) = @ARGV;

my $dbh = Lire::Config::connect_dbi();

$date_begin =~ /(\d{4})(\d{2})(\d{2})(\d{2}):(\d{2})$/ 
  or lr_err( "starting time is not in YYYYMMDDHH:MM format: $date_begin" );
my $tsmp_begin = timelocal(0, $5, $4, $3, $2-1, $1-1900);

$date_end=~ /(\d{4})(\d{2})(\d{2})(\d{2}):(\d{2})$/ 
  or lr_err( "ending time is not in YYYYMMDDHH:MM format: $date_end" );
my $tsmp_end=timelocal(0, $5, $4, $3, $2-1, $1-1900 );

my $query = "SELECT * FROM $superservice WHERE time >= $tsmp_begin AND time < $tsmp_end";
lr_debug("SQL request: $query");

my $sth = $dbh->prepare( $query )
  or lr_err( "prepare failed: ", $dbh->errstr );

$sth->execute()
  or lr_err( "prepare failed: ", $dbh->errstr );

open( DLF,">$dlf_file")
  or lr_err("Can't open DLF file $dlf_file: $!");
while (my $cols = $sth->fetchrow_arrayref ) {
    # Skip Id column 
    print DLF join( " ", map { defined $_ ? $_ : 'LIRE_NOTAVAIL' } @$cols[1..$#$cols]), "\n";
}
lr_err( "fetchrow_arrayref failed: ", $sth->errstr )
  if $sth->err;
close DLF;

__END__

=pod

=head1 NAME

lr_sql2dlf - generate a temporary DLF from a SQL database

=head1 SYNOPSIS

B<lr_sql2dlf> <superservice> <dlf-file> <datetime-start> <datetime-end>

=head1 DESCRIPTION

This script will create a DLF file B<dlf-file> by extracting DLF
records stored in a SQL database. The DLF file will contain the
records between <datetime-start> and <datetime-end>. (Including the
starting timestamp and excluding the ending one).

The timestamp should be specified using YYYY-MM-DD HH:MM format.

=head1 SEE ALSO

lr_dlf2sql(1), lr_sql2report(1)

=head1 AUTHORS

Arnaud Gaillard and Francis J. Lacoste

=head1 COPYRIGHT

Copyright (C) 2002 Arnaud Gaillard <wireless@orange.ch>

Stichting LogReport Foundation <logreport@logreport>

This program is part of Lire.

Lire 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:
