#!/usr/bin/perl
# Copyright (C) Dirk Husemann, Computer Science Department IV, 
# 	Friedrich-Alexander-Universitt Erlangen-Nrnberg, Germany, 1993
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#      This product includes software developed by the Dirk Husemann,
#      Friedrich-Alexander-Universitt Erlangen-Nrnberg, Germany
# 4. The name of the University may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE UNIVERSITY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 

# get path right
$faxhome = "/usr/contrib/fax/bin";
$execpath = "/bin:/sbin:/usr/bin:/usr/sbin:$faxhome";
$ENV{'PATH'} = $execpath;

$accountingdata = "/var/spool/fax/etc/xferlog";

$SR = "<<";			# start record character
$IS = "||";			# item seperator
$ER = ">>";			# item close (needed for re-extracting)

$dt = "[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}";
$tm = "[0-9]{1,2}:[0-9]{1,2}";
$numb = "[0-9]+";
$unqt = "[^\\t]+";
$qt = "\\[[^]]*\\]";
$nr = "[^|>]*";

%record = ();

format FAXACCOUNTING =
  @>>>>>>>>>>>>>>  @>>>>>>>>>>>>>, @##p/@>>>>min (@<<<<</@>>>>) @<<<<<<<<<<<<<<<
  $dest,          $date,          $pages, $trans, $protocol, $sigrate, $reason
.
 
sub getitem {
    local ($entry, $tag) = @_;
    local ($value);
    
    if ($entry =~ m*$tag=($nr)*) {
	$value = $1;
	$value =~ s/\[([^]]*)\]/$1/;

	return $value;
    }

    return "";
}
    

open(ACCOUNTING, "<$accountingdata") || 
    die "Ooops, can't get at accounting data ...\n";
while($acctgline = <ACCOUNTING>) {
    chop $acctgline;
    $acctgline =~ s/\"([^\"]*)\"/\[$1\]/g;
    # Look for SEND lines
    ($acctgline =~ m|^($dt\s+$tm)\s+SEND\s+($unqt)\s+($unqt)\s+($qt)\s+$qt\s+($numb)\s+($unqt\s+$unqt)\s+($numb)\s+($tm)\s+($qt)|) && 
        ($record{$3} .= $SR."DATE=$1".$IS."TTY=$2".$IS."FAX=$4"
	       .$IS."SPEED=$5".$IS."FORMAT=$6"
	       .$IS."PAGES=$7".$IS."TRANS=$8".$IS."ERR=$9".$ER) &&
		   next;;

    # Look for RECV lines
    ($acctgline =~ m|^($dt\s+$tm)\s+RECV\s+($unqt)\s+$unqt\s+($qt)\s+($qt)\s+($numb)\s+($unqt\s+$unqt)\s+($numb)\s+($tm)\s+($qt)|) && 
        ($record{$3} .= $SR."DATE=$1".$IS."TTY=$2".$IS."FAX=$4"
	       .$IS."SPEED=$5".$IS."FORMAT=$6"
	       .$IS."PAGES=$7".$IS."TRANS=$8".$IS."ERR=$9".$ER) &&
		   next;
}

$ttpages = 0;
$~ = FAXACCOUNTING;
foreach $tsisender ( sort(keys(%record))) {
    $tpages = 0;
    printf STDOUT "$tsisender\n";
    @entryitems = split(/></, $record{$tsisender});
    foreach $entry (@entryitems) {
	$dest = &getitem($entry, "FAX");
	$date = &getitem($entry, "DATE");
	$pages = &getitem($entry, "PAGES");
	$transtime = &getitem($entry, "TRANS");
	($transtime =~ /([0-9]+):([0-9]+)/) && 
	    ($tminutes = $1, $tseconds = $2);
	$trans = sprintf("%02d:%02d", $tminutes, $tseconds);
	$protocol = &getitem($entry, "FORMAT");
	$sigrate = &getitem($entry, "SPEED");
	$reason = &getitem($entry, "ERR");

	write;
	$tpages += $pages;
    }
    printf STDOUT "   = $tpages pages.\n\n";
    $ttpages += $tpages;
}
printf STDOUT "Total $ttpages pages\n";

exit;
