#! /usr/bin/perl -w
use strict;
use lib '/usr/share/perl5';
use Lire::Program qw(:msg :dlf);
use Lire::Time qw(date2cal getMonthName);
use Lire::DlfSchema;

my $lines      = 0;
my $dlflines   = 0;
my $errorlines = 0;

init_dlf_converter( "print" );

my $schema	= Lire::DlfSchema::load_schema( "print" );
my $dlf_maker =
	 $schema->make_hashref2asciidlf_func( qw/user printer job-id size 
						 duration time/);

my %dlf = ();
my $dlfoutput;
while (<>) {
    chomp;
    $lines++;

    # do the parsing here
    lr_debug($_);
    my @fields = split(' ', $_);
    my ($status) = $fields[0];
    ($dlf{user}) = ($fields[2] =~ /^'-n(.*)'$/);
    ($dlf{printer}) = ($fields[3] =~ /^'-P(.*)'$/);
    ($dlf{"job-id"}) = ($fields[4] =~ /^'-k(.*)'$/);
    ($dlf{size}) = ($fields[5] =~ /^'-b(.*)'$/);
    my ($datetimefield) = ($fields[6] =~ /^'-t(.*)'$/);
    lr_debug("$datetimefield");
    my @datefields = split /-/, $datetimefield;
    lr_debug("@datefields");
    lr_debug("Time: " . $datefields[3]);
    my @timefields = split /\./, $datefields[3];
    lr_debug("@timefields");

    if ($status eq "jobend") {
        # output the DLF line
        my $endtime = date2cal( $datefields[0], getMonthName($datefields[1]),
                                $datefields[2], $timefields[0], "+0000" );
        $dlf{duration} = $endtime - $dlf{time};
        $dlfoutput = $dlf_maker->( \%dlf );
        print join( " ", @$dlfoutput), "\n";
        $dlflines++;

        # reinitialize fields
        %dlf = ();
    } elsif ($status eq "jobstart") {
        $dlf{time} = date2cal( $datefields[0], getMonthName($datefields[1]),
                               $datefields[2], $timefields[0] );
        $lines--; # do not count these lines
    }
}

end_dlf_converter( $lines, $dlflines, $errorlines );

__END__

=pod

=head1 NAME

lprng_account2dlf - convert a Lpr-NG account log file to the Print DLF format

=head1 SYNOPSIS

B<lprng_account2dlf> I<file>

=head1 DESCRIPTION

=head1 SEE ALSO

=head1 AUTHORS

Egon Willighagen <egonw@logreport.org>

=head1 VERSION

$Id: lprng_account2dlf.in,v 1.3 2001/11/10 22:23:08 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2000-2001 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:
