#! /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(date2cal getMonthName);

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 client_host printer
						 job-id size duration time/);

sub parse_time {
    my ( $year, $month, $day, $time, $msec) =
      $_[0] =~ /^(\d\d\d\d)-(\d\d)-(\d\d)-(\d\d:\d\d:\d\d).(\d\d\d)$/
	or die "bad time field: $_[0]\n";

    return date2cal( $year, getMonthName( $month -1 ), $day, $time ) . "." .$msec;
}

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

    # do the parsing here
    eval {
	# Keys are
	#  H = client host
	#  k = job-id
	#  b = size
	#  t = time
	#  n = user
	#  P = printer
	my %rec = /'-(.)(.*?)'/g
	  or die "failed to parse LPR-NG accounting record\n";

	my $time  = parse_time( $rec{t} );
	if ( /^jobstart/ ) {
	    my $jobid = $rec{k};
	    $cache{$jobid} = { time => $time, 'job-id' => $jobid };
	} elsif ( /^jobend/ ) {
	    my $jobid = $rec{k};
	    lr_warn ( "no jobstart with same job-id found: $jobid\n" )
	      unless exists $cache{$jobid};
	    my $job = $cache{$jobid} || { time => $time, 'job-id' => $jobid };

	    $job->{user}	= $rec{n};
	    $job->{printer}	= $rec{P};
	    $job->{client_host} = $rec{H};
	    $job->{size}	= $rec{b};
	    $job->{duration}	= $time - $job->{time};

	    my $dlf = $dlf_maker->( $job );
	    print join( " ", @$dlf), "\n";
	    $dlflines++;

	    delete $cache{$jobid};
	} else {
	    die "unknown record type\n";
	}
    };
    if ( $@ ) {
	lr_warn( $@ );
	lr_notice( qq{cannot convert line $. "$_" to print dlf, skipping} );
	$errorlines++;
    }
}

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

__END__

=pod

=head1 NAME

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

=head1 SYNOPSIS

B<lprng_account2dlf> I<file>

=head1 DESCRIPTION

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

=head1 SEE ALSO

The LPRng website at http://www.lprng.com/ .  See the Lire User Manual for a
description of the LPRng log files.  cups_pagelog2dlf(1).

=head1 AUTHORS

Egon Willighagen <egonw@logreport.org>

=head1 VERSION

$Id: lprng_account2dlf.in,v 1.6 2002/08/15 15:21:38 vanbaal Exp $

=head1 COPYRIGHT

Copyright (C) 2000, 2001, 2002 Stichting LogReport Foundation LogReport@LogReport.org

This file 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:
