#!/usr/local/bin/perl
#
#
# Statistics for ftntoss/ftnroute/ftnpack log files
#

$NEWSGROUPS = "junk";
$SUBJECT    = "EchoMail statistics report";

##############################################################################
#
#
# Perl functions to read FIDOGATE config file,
# included by <INCLUDE config.pl> when running subst.pl
#

my %CONFIG;

# specials for DosDrive and Zone
my %CONFIG_dosdrive;
my %CONFIG_zone;



my %CONFIG_default =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"pinbound", "/var/spool/fido/bt/pin",
	"lock_dbc", "dbc",
	"spooldir", "/var/spool/fido/gate",
	"seq_pack", "%V/seq/pack",
	"toss_route", "%S/toss/route",
	"logdir", "/var/log/fido/gate",
	"areas", "%C/areas",
	"logfile", "%G/log",
	"hosts", "%C/hosts",
	"ftpinbound", "/var/spool/fido/bt/ftpin",
	"newsetcdir", "/usr/local/news/etc",
	"dbc_history", "%V/bdc",
	"seq_tick", "%V/seq/tick",
	"toss_pack", "%S/toss/pack",
	"inn_batchdir", "/usr/local/news/spool/outgoing",
	"configdir", "/usr/local/etc/fido/gate",
	"config_gate", "%C/fidogate.conf",
	"routing", "%C/routing",
	"toss_bad", "%S/toss/bad",
	"outpkt_news", "%S/outpkt/news",
	"seq_ff", "%V/seq/ff",
	"libexecdir", "/usr/local/libexec/fidogate",
	"config_main", "%C/fidogate.conf",
	"vardir", "/var/db/fidogate",
	"history", "%V/history",
	"outpkt_mail", "%S/outpkt/mail",
	"outpkt", "%S/outpkt",
	"seq_news", "%V/seq/news",
	"newsbindir", "/usr/local/news/bin",
	"aliases", "%C/aliases",
	"newsspooldir", "/usr/local/news/spool/articles",
	"tic_history", "%V/tic_hist",
	"outrfc_news", "%S/outrfc/news",
	"charsetmap", "%L/charset.bin",
	"newslibdir", "/usr/local/news",
	"passwd", "%C/passwd",
	"inbound", "/var/spool/fido/bt/in",
	"hubroutedb", "%V/route",
	"lockdir", "/var/run/fidogate",
	"outrfc_mail", "%S/outrfc/mail",
	"lock_tic_hist", "tic_hist",
	"tick_hold", "%B/tick",
	"seq_msgid", "%V/seq/msgid",
	"seq_toss", "%V/seq/toss",
	"uuinbound", "/var/spool/fido/bt/uuin",
	"lock_history", "history",
	"acl", "%C/acl",
	"seq_split", "%V/seq/split",
	"btbasedir", "/var/spool/fido/bt",
	"seq_mail", "%V/seq/mail",
	"spyes", "%C/spyes",
	"uplinks", "%C/uplinks",
	"netmaildir", "/var/spool/fido/bt/netmail",
	"newsvardir", "/usr/local/news/db",
	"packing", "%C/packing",
	"ftnacl", "%C/ftnacl",
	"toss_toss", "%S/toss/toss",
	"seq_pkt", "%V/seq/pkt",
	"bindir", "/usr/local/bin",
     );
my %CONFIG_abbrev =
    (
##Automatically generated by subst.pl, DO NOT EDIT!!!##
	"B", "btbasedir",
	"N", "bindir",
	"V", "vardir",
	"C", "configdir",
	"L", "libexecdir",
	"S", "spooldir",
	"P", "pinbound",
	"K", "lockdir",
	"I", "inbound",
	"G", "logdir",
     );



sub CONFIG_read {
    my($file) = @_;
    my($key, $arg);
    local *C;

    $file = CONFIG_expand($file);

    open(C,"$file") || die "config.pl: can't open config file $file\n";
    while(<C>) {
	chop;
	next if( /^\s*\#/ );	# comments
	next if( /^\s*$/  );	# empty
	s/\s*$//;		# remove trailing white space
	s/^\s*//;		# remove leading white space
	($key,$arg) = split(' ', $_, 2);
	$key =~ tr/A-Z/a-z/;
	if($key eq "include") {
	    CONFIG_read($arg);
	    next;
	}
	if($key eq "dosdrive") {
	    my ($d, $path) = split(' ', $arg);
	    $CONFIG_dosdrive{lc($d)} = $path;
	    next;
	}
	if($key eq "zone") {
	    my ($z, $rest) = split(' ', $arg, 2);
	    $CONFIG_zone{$z} = $rest;
	    next;
	}
	$CONFIG{$key} = $arg if(!$CONFIG{$key});
    }
    close(C);
}


sub CONFIG_get1 {
    my($key) = @_;
    my($ukey);

    $ukey = $key;
    $ukey =~ tr/a-z/A-Z/;
    return $ENV{"FIDOGATE_$ukey"} if($ENV{"FIDOGATE_$ukey"});

    return $CONFIG{$key} if($CONFIG{$key});
    return $CONFIG_default{$key};
}


sub CONFIG_get {
    my($key) = @_;
    my($ret);
    my($exp);

    $key =~ tr/A-Z/a-z/;
    return CONFIG_expand( CONFIG_get1($key) );
}


sub CONFIG_expand {
    my($v) = @_;
    my($exp);

    if($v =~ /^%([A-Z])/) {
	$exp = CONFIG_get1($CONFIG_abbrev{$1});
	$v =~ s/^%./$exp/;
    }

    return $v;
}


sub CONFIG_debug {    
    my($key);

    for $key (keys %CONFIG) {
	print "$key = $CONFIG{$key} -> ", CONFIG_get($key), "\n";
    }
}

##############################################################################

# read config
my $CONFIG = "%C/fidogate.conf";
CONFIG_read($CONFIG);

my $INEWS       = CONFIG_get("INEWS");
my $SENDMAIL    = CONFIG_get("SENDMAIL");


require "getopts.pl";
&Getopts('g:s:t:nm:');

if($opt_g) {
    $NEWSGROUPS = $opt_g;
}
if($opt_s) {
    $SUBJECT    = $opt_s;
}
if($opt_t) {
    $SUBJECT    = "$SUBJECT $opt_t";
}
if($opt_n) {
    open(OUT, "|$INEWS") || die "logreport: can't open pipe to inews\n";
    select(OUT);
    $out_flag = 1;
}
if($opt_m) {
    open(OUT, "|$SENDMAIL $opt_m")
	|| die "logreport: can't open pipe to sendmail\n";
    select(OUT);
    $out_flag = 1;
}



while(<>) {
    
    if( /^(... .. ..:..:..) ftntoss packet.*\((\d+)b\) from ([0-9:\/.]+) / ) {
	$first = $1 if(! $first);
	$size = $2;
	$node = $3;

	$in_total += $size;
	$in_node{$node} += $size;
    }

    if( /(... .. ..:..:..) ftnpack .*packet \((\d+)b\) for ([0-9:\/.]+) / ) {
	$last = $1;
	$size = $2;
	$node = $3;

	$out_total += $size;
	$out_node{$node} += $size;
    }

}


sub in_bynumber  { $in_node{$b}  <=> $in_node{$a};  }
sub out_bynumber { $out_node{$b} <=> $out_node{$a}; }



print "Newsgroups: $NEWSGROUPS\n" if($opt_n);
print "Subject: $SUBJECT\n";

print "\n";


print "Period $first -- $last\n\n";


printf "In:  total           %7ldK\n", $in_total/1024;
for $n (sort in_bynumber keys(%in_node)) {
    printf "     %-16s%7ldK\n", $n, $in_node{$n}/1024;
}

printf "\n";

printf "Out: total           %7ldK\n", $out_total/1024;
for $n (sort out_bynumber keys(%out_node)) {
    printf "     %-16s%7ldK\n", $n, $out_node{$n}/1024;
}


if($out_flag) {
    close(OUT);
}
