#! /usr/bin/perl -w

use strict;
use lib '/usr/local/share/perl5';
use Lire::DlfSchema;
use Lire::Program qw(:msg :dlf);
use Lire::Time qw(clf2cal);

my $lines      = 0;
my $dlflines   = 0;
my $errorlines = 0;
my $schema     = Lire::DlfSchema::load_schema( "print" );
my $dlf_maker  = 
  $schema->make_hashref2asciidlf_func( qw/time duration user job-id printer 
					  num_copies num_pages billing/);

init_dlf_converter( "print" );
my %dlf = ();
my $dlfoutput;
my $prev_dlf = {};
while (<>) {
    chomp;
    $lines++;

    # initialize fields
    my %dlf = ();

    # do the parsing here
    my $failed = 0;
    my $time;
    my $pagenum;
    lr_debug($_);
    ($dlf{printer},
     $dlf{user},
     $dlf{"job-id"},
     $time,
     $pagenum,
     $dlf{num_copies},
     $dlf{billing}) =
        ($_ =~ /^(.*)\s(.*)\s(.*)\s(\[.*\])\s(.*)\s(.*)\s(.*)$/)
        or do {
	    lr_warn("Cannot convert $_");
	    $errorlines++;
	    next;
	}; 
    lr_debug("User: " . $dlf{user});
    my $endtime = clf2cal( $time );

    if (!$failed) {
        if ( ! defined $prev_dlf->{"job-id"} ||
	     $prev_dlf->{"job-id"} ne $dlf{"job-id"} )
	{
            lr_debug("New job");
            # new job; output the DLF line
            $dlf{num_pages} = 1;
            $dlf{time} = $endtime;
            $dlf{duration} = 0;
            if ( defined $prev_dlf->{"job-id"} ) {
                lr_debug("Output the previous DLF line");
                $dlfoutput = $dlf_maker->( $prev_dlf );
                print join( " ", @$dlfoutput), "\n";
                $dlflines++;
            };
        } else {
            # same job; update info
            $dlf{num_pages} = $prev_dlf->{num_pages} + 1;
            $dlf{time} = $prev_dlf->{time};
            $dlf{duration} = $endtime - $dlf{time};
        }
        $prev_dlf = \%dlf;
    }
}
if ( defined $prev_dlf->{'job-id'} ) {
    # now output the 'running' job
    $dlfoutput = $dlf_maker->( $prev_dlf );
    print join( " ", @$dlfoutput), "\n";
    $dlflines++;
}

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


__END__

=pod

=head1 NAME

cups_pagelog2dlf - convert a CUPS page_log log file to the Print DLF format

=head1 SYNOPSIS

B<cups_pagelog2dlf> I<file>

=head1 DESCRIPTION

This script expects a CUPS page_log log file on stdin, and converts it to a
Lire print DLF file.

=head1 SEE ALSO

The CUPS website at http://www.cups.org/ .  See the Lire User Manual for a
description of the CUPS log files. lprng_account2dlf(1).

=head1 AUTHORS

Egon Willighagen <egonw@logreport.org>

=head1 VERSION

$Id: cups_pagelog2dlf.in,v 1.6 2002/08/15 15:21:38 vanbaal 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:
