#!/usr/bin/perl -w
#
#  Use:
#          Tools/parse_logfile <inputfile(s)>
#  <inputfile> should be a file in syslog format.
#  The start of a connection must match the pattern: "chat\[\d+\]: ATDP"
#  The end must match the pattern: "pppd\[\d+\]: Exit"
#  Edit this regular expressions in this script if needed.
#
# File = Tools/parse_logfile,
# part of tcharge, version 1.3, 11MAY98
# Copyright (C) 1997,98 Volker B"orchers (boercher@physik.uni-bremen.de)
# See file `tcharge.c' for legal affairs.

## The year of the dates in the logfile (current if $year is undefined):
#$year = 98;
## the file the computed charges should be printed into:
$output_logfile = "batch.log";
## the file where all start- and stop-lines should be collected in:
$matched = "matched";

## Regular expressions for the start-/end- messages:
$StartRe = 'chat\[\d+\]: ATDP';
$EndRe = 'pppd\[\d+\]: Exit';

## Path and name of programs:
#############################
# translation program: date/time -> time_t
$date2second = "date2second";	# hope it's in the path
$tcharge = "tcharge";	# hope it's in the path
die "$tcharge not in \$PATH (or it's an old tcharge-version) - exit.\n"
    if((system "$tcharge -start 1 -end 2 >&/dev/null && exit 0") != 0);
die "date2second not in \$PATH - exit.\n"
    if((system "$date2second 0:0:0 1 1 98 >&/dev/null && exit 0") != 0);

$started = 0;
%Months = ( "Jan"=>1, "Feb"=>2, "Mar"=>3, "Apr"=>4,  "May"=>5,  "Jun"=>6,
	    "Jul"=>7, "Aug"=>8, "Sep"=>9, "Oct"=>10, "Nov"=>11, "Dec"=>12 );

sub eval_logline {
    my $t;
    my $l = $_[0];
    ($month,$day,$hour,$min,$sec) = ($l =~ /^(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)/);
    $month = $Months{$month}; # date2second needs a number
    if(! $month) {
	warn "Error: Not a legal month abbreviation in this line:\n",
	$ARGV, ":", $., ": ", $l, "\n";
	return 0;
    }
    $month .= " $year" if defined $year;
    open(DATE2SECOND, "$date2second $hour:$min:$sec $day $month|") ||
	die "cannot open pipe to `$date2second'";
    chomp($t = <DATE2SECOND>);
    close DATE2SECOND;
    return $t;
}
open(MATCH, "> $matched");

## comment out the next 3 lines if you want to use this script wihtin a pipe!
die "Use:\n\t$0 <inputfile(s)>\n<inputfile> should be a file in syslog format.\n",
    "The start of a connection must match the pattern: \"$StartRe\"\n",
    "The end must match the pattern: \"$EndRe\"\n",
    "Edit this regular expressions in this script if needed.\n"
    if($#ARGV < 0);

TOP:
while(<>) {
    if(/$StartRe/o) {
	warn "Last connection not properly ended when starting\n",
	" a new connection in \n", $ARGV, ":", $., ": ", $_, "\n"
	    if($started);
	$started = 1;
	print MATCH;
	$start_time_t = eval_logline($_);
    }
    elsif(/$EndRe/o) {
	if(! $started) {
	    warn "Now ending a connection that have not been properly started\n",
	    " when ending connection (ignoring it) in \n",
	    $ARGV, ":", $., ": ", $_, "\n";
	    next TOP;
	}
	$started = 0;
	print MATCH;
	$end_time_t = eval_logline($_) || next TOP;
	## invoke tcharge only if the call lasted at least 1 second:
	if($start_time_t < $end_time_t) {
	    print MATCH
		"$tcharge -start $start_time_t -end $end_time_t -file $output_logfile\n";
	    system "$tcharge -start $start_time_t -end $end_time_t -file $output_logfile"
		|| die "cannot execute \`$tcharge\'!";
	}
    }
}


print <<EOF;
-------------------------------------------------------------------
Finished!
+ Hopefully you will find the charges in the file \`$output_logfile\'.
+ All lines matching the start- and stop-line regular expressions:
   \$StartRe = \"$StartRe\";
   \$EndRe = \"$EndRe\";
  should be collected in \`$matched\'.
-------------------------------------------------------------------
EOF
