#!/usr/bin/perl

#
# FBB Connection Statistics Management Program
# Author:  Stewart Wilkinson G0LGS
use File::stat;
use Getopt::Long;

# Where is fbb.conf ?
my $FBBCONF="/etc/ax25/fbb.conf";

#
# Nothing should need Changing Below
#
my $help = 0;
my $all = 0;
my $first = -1;
my $last = -1;
my $verbose = 0;
($base = $0) =~ s%.*/%%;

GetOptions(
        "help|?" => \$help,
        "first:i" => \$first,
        "last:i" => \$last,
        "all" => \$all,
        "verbose" => \$verbose
        );

#
# Nothing should need Changing Below
#
my $Vers="0.12";
my $VDate="30/01/2005";
my $VData="Statistics Management for XFBB 7.01+ (c) 2005 by G0LGS V${Vers} ${VDate}.";

if( $help ){
	Usage();
}

# Where are the FBB var files ?
my $VARDIR=`fbbgetconf -f $FBBCONF data`;
my $Err = $?;
if( $Err ){
	exit $Err;
}

chomp($VARDIR);
my $STAT=$VARDIR . "/statis.dat";

if( ! open( STATIS, "< $STAT") ){
	printf STDERR "ERROR: Unable to Open '%s' - Check the configuration\n", $STAT;
	exit 1;
}

# Calc Number of Records
$sb = stat($STAT);
my $Recs = $sb->size / 14;
# my $Recs = (stat($STAT))[7] / 14;

my $Start = $Recs - 10;
my $End = $Recs;

if( $all ){
	$Start = 0;
	$End = $Recs;
}

if( $first > -1 ){
	$Start = 0;
	$End = 10;
	if( $first > 0 ){
		$End = $first;
	}
}

if( $last > -1 ){
	$End = $Recs;
	if( $last > 0 ){
		$Start = $End - $last;
	}
}

# Parameters ?
if( $#ARGV >= 0 ){

	# Parameters
	my $PARAM=$ARGV[0];
	if( $PARAM =~ /^(\d+)$/ ){
        	$Start = $1;
        	$End = $1+1;
	}else{
		Usage();
	}

	if( $#ARGV >= 1 ){
		my $PARAM=$ARGV[1];
		if( $PARAM =~ /^(\d+)$/ ){
	        	$End = $1+1;
		}else{
			Usage();
		}
	}
}

my $RecNo = $Start;

# Move to the appropriate Record
seek(STATIS, $RecNo * 14, 0);;

printf STDERR "%s\n\n", ${VData};

# Process Each Record
while( ! eof(STATIS) && (read( STATIS, $Rec, 14 ) == 14) && ($RecNo < $End) ) {

	# Extract Data from Record.
	my ($call,$port,$ch,$date,$dur) = unpack("A6CCLS", $Rec);

	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($date);

	my $Yr = ('00'..'99')[$year % 100];
	my $Mn = ('01'..'12')[$mon];
	my $Dy = ('00'..'31')[$mday];
	my $Hr = ('00'..'23')[$hour];
	my $Mi = ('00'..'59')[$min];
	my $Sc = ('00'..'59')[$sec];

	my $Date= $Dy."/".$Mn."/".$Yr." ".$Hr.":".$Mi.":".$Sc;

	printf "%6d: %c %02d %-6s %s %d`%02d\"\n", $RecNo, $port+65, $ch, $call, $Date, $dur/60, $dur%60;

	$RecNo++;
}

close(STATIS);

printf "\n%d Records Available\n", $Recs;

exit;

sub Usage {
        printf STDERR "%s\n", ${VData};
        printf STDERR "\nUsage: %s [options]\n", ${base};
        printf STDERR "\noptions are:\n";
        printf STDERR "  --help|--?  This help information.\n";
        printf STDERR "  --first=<n> Show first n Entries (default 10)\n";
        printf STDERR "  --last=<n>  Show last n Entries (default 10)\n";
        printf STDERR "  --all       Show ALL Entries.\n";
        printf STDERR "  <n>         Show Entry n.\n";
        printf STDERR "  <m> <n>     Show Entries m to n.\n";
        printf STDERR "\n\n";
	exit 1;
}
