#!/usr/bin/perl
#
# counting postings
#
# newsfeeds entry:
#counterchan!:*:Tc,WN:/news/bin/counterchan

require "/news/lib/innshellvars.pl" ;

my $dir="$inn::pathspool/counterchan";
my @date=localtime();
$date[4]++;	# Month
$date[5]+=1900;	# Year
my $counterfile=sprintf("%s/groupcounter.%d%02d%02d", $dir, $date[5], $date[4], $date[3]); # The groupcounter file
my %counters;

# Read counterfile if exists
if ( -s $counterfile ) {
	open(COUNT, "< $counterfile");
	while(chomp(my $line=<COUNT>)) {
		my ($count, $group)=split(/\s+/);
		$counters{$group}=$count;
	}
	close(COUNT);
}

# Read STDIN and count postings in groups
while (chomp(my $line=<>)) {
	foreach my $group (split(/,/,$line)) {
		$counters{$group}++;
	}
}

#If done counting write back to $counterfile
open(COUNT, "> $counterfile");
foreach $group (sort {$counters{$b} <=> $counters{$a}} keys(%counters)) {
	print COUNT "$counters{$group}\t$group\n";
}
close(COUNT);

