#! /usr/bin/perl
# fixscript will replace this line with code to load INN::Config

# backlogstat - display backlog to sites
#
# Based on bklog by Bill Davidsen <davidsen@tmr.com>, contributed in 2003.
#
# Various bug fixes, code and documentation improvements since then
# in 2007, 2022-2025.

use strict;
use warnings;

# Breaks if backlog-directory in innfeed.conf is not "innfeed".
my $dir = "$INN::Config::pathspool/innfeed";

use Getopt::Std;
use vars qw($opt_H $opt_h $opt_n $opt_k $opt_S $opt_d);
$| = 1;

# Define variables Getopt::Std uses for --help and --version.
$Getopt::Std::STANDARD_HELP_VERSION = 1;
our $VERSION = $INN::Config::version;
$VERSION =~ s/INN //;

$0 =~ s!.*/!!;

my $usage = "Usage:
  $0 [-hHkn] [-d dir] [-S name]

  Print innfeed backlog info

Options:
  -d dir  Use \"dir\" instead of \$pathspool/innfeed
  -h      Help - this is all, you got it!
  -H      Output a header at the top of the output
  -k      Scale all numbers in k (1024) units
  -n      Count number of arts, not bytes of backlog filesize
          Note: this may be SLOW for large files!
  -S xx   Display only site names starting with xx
";

sub HELP_MESSAGE {
    print $usage;
    exit(0);
}

# option processing
getopts('d:hHknS:') || die $usage;
HELP_MESSAGE() if defined($opt_h);

# open the directory
$dir = $opt_d if $opt_d;
print "$dir\n";
chdir($dir) or die "Can't cd to $dir";
opendir(DIR, ".") or die "Can't open dir";

my %nodes;
while (my $name = readdir(DIR)) {
    # must be a file, correct name, non-zero size
    my $size;
    next unless -f $name;
    next unless ($size = -s $name);
    next unless $name =~ m/.*\.(in|out)put/;
    my $io = $1;
    (my $nodename = $name) =~ s/\.(in|out)put$//;

    # check for only some sites wanted
    next if ($opt_S && $nodename !~ /^${opt_S}.*/);
    # here we do the counts if asked
    if ($opt_n) {
        # open the file and count lines
        if (open(IN, "<$name")) {
            while (<IN>) { $size = $.; }
            close IN;
        }
    } else {
        # get the size of the file
        $size = -s $name;
    }
    $nodes{$nodename} = () unless defined $nodes{$nodename};
    $nodes{$nodename}{$io} = ($opt_k ? $size / 1024 : $size);
}
closedir DIR;

# output the data for each node
if (my $numnodes = keys %nodes) {
    if ($opt_H) {
        if ($opt_n) {
            print "  <---------- posts ----------->\n";
        } else {
            print "  <---------- bytes ----------->\n";
        }
    }
    my $ofmt;
    if ($opt_k) {
        print "  input(k)  output(k)   total(k) Feed Name\n" if $opt_H;
        $ofmt = ($opt_n ? "%10.2f" : "%10.1f");
    } else {
        print "     input     output      total Feed Name\n" if $opt_H;
        $ofmt = "%10d";
    }
    for my $node (sort keys %nodes) {
        my $hash = $nodes{$node};
        my $size_in = $hash->{in} || 0;
        my $size_out = $hash->{out} || 0;
        my $size_tot = $size_in + $size_out;
        printf "${ofmt} ${ofmt} ${ofmt} %s\n",
          $size_in, $size_out, $size_tot, $node;
    }
} else {
    print "NO backlog!\n";
}

exit 0;
