#!/usr/bin/perl

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

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

#
# Nothing should need Changing Below
#
my $help = 0;
my $test = 0;
my $verbose = 0;
my $SUM = 0;
($base = $0) =~ s%.*/%%;

GetOptions(
        "help|?" => \$help,
        "verbose" => \$verbose,
        "test" => \$test,
        "summary" => \$SUM
        );

if( $verbose || $test ){
	$SUM++;
}

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();
}

# Wrong number of Parameters ?
if( $#ARGV != 0 ){
	Usage();
}

my $days=0;
# The Parameters
my $PARAM=$ARGV[0];
if( $PARAM =~ /^(\d+)$/ ){
	$days = $1;
}else{
	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";
my $NEW=$VARDIR . "/statis.new";
my $OLD=$VARDIR . "/statis.old";

my $today = int ( time() / (60 * 60 * 24)  ) * 60 * 60 * 24;
my $first = $today - (60 * 60 * 24 * $days);

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

if( ! $test ) {
	if( ! open( NEW, "> $NEW") ){
		printf STDERR "ERROR: Unable to Create '%s' - Check the configuration\n", $NEW;
		exit 1;
	}
}

my $Recs=0;
my $Keep=0;

if( $SUM != 0 ){
	printf STDOUT "%s\n", ${VData};

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

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

        $Date= $Dy."/".$Mn."/".$Yr;

	if( ! $test ){
		printf STDOUT "Removing records prior to: %s\n", $Date;
	}else{
		printf STDOUT "Checking for records prior to: %s\n", $Date;
	}
}

# Read & Check Each Record
while( ! eof(STATIS) && (read( STATIS, $Rec, 14 ) == 14) ) {

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

	# Keep ?
	if( $date >= $first ){
		$Keep++;
		if( ! $test && syswrite( NEW, $Rec, 14 ) != 14 ) {
			die;
		}
	}

	$Recs++;
	if( $verbose ){
		printf "Processing: %6d\r", $Recs;
	}
}

close(STATIS);
if( ! $test ) {
	close(NEW);
}

# Replace Files
if( ! $test ){
	rename( $STAT, $OLD );
	rename( $NEW, $STAT );
}

if( $SUM != 0 ){
	if( ! $test ){
		printf STDOUT "\r*** %6d of %6d Records Deleted  ***", $Recs - $Keep, $Recs;
		printf STDOUT "\n*** %6d of %6d Records Retained ***\n\n", $Keep, $Recs;
	}else{
		printf STDOUT "\r*** %6d of %6d Records would be Deleted  ***", $Recs - $Keep, $Recs;
		printf STDOUT "\n*** %6d of %6d Records would be Retained ***\n\n", $Keep, $Recs;
	}
}

exit;

sub Usage {
	printf STDERR "%s\n", ${VData};
        printf STDERR "\nUsage: %s <no_days_to_keep> [options]\n", ${base};
        printf STDERR "\noptions are:\n";
        printf STDERR "  --help|--?  This help information.\n";
        printf STDERR "  --summary   Display a summary.\n";
        printf STDERR "  --test      Test mode (don\'t write changes to disk).\n";
        printf STDERR "\n\n";
        exit 1;
}
