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

# This script cleans the newsgroups file:
#   * Groups no longer in the active file are removed.
#   * Duplicate entries are removed.  The last of a set of duplicates
#     is the one retained.  That way, you could simply append the
#     new/revised entries from a docheckgroups run and then this script
#     will remove the old ones.
#   * Groups with no description are removed.
#   * Groups matching the $remove regexp are removed.

use warnings;

$remove = '';
# $remove='^alt\.';

open ACT, $INN::Config::active or die "Can't open $INN::Config::active: $!\n";
while (<ACT>) {
    ($group) = split;
    $act{$group} = 1 if ($remove eq "" || $group !~ /$remove/o);
}
close ACT;

open NG, $INN::Config::newsgroups
  or die "Can't open $INN::Config::newsgroups: $!\n";
while (<NG>) {
    chomp;
    ($group, $desc) = split /\s+/, $_, 2;
    next unless (defined $act{$group});

    next if (!defined $desc);
    next if ($desc =~ /^[?\s]*$/);
    next if ($desc =~ /^no desc(ription)?(\.)?$/i);

    $hist{$group} = $desc;
}
close NG;

open NG, ">$INN::Config::newsgroups.new"
  or die "Can't open $INN::Config::newsgroups.new for write: $!\n";
foreach my $grp (sort keys %act) {
    if (defined $hist{$grp}) {
        print NG "$grp\t$hist{$grp}\n" or die "Can't write: $!\n";
    }
}
close NG or die "Can't close: $!\n";

rename "$INN::Config::newsgroups.new", $INN::Config::newsgroups
  or die "Can't rename $INN::Config::newsgroups.new "
  . "to $INN::Config::newsgroups: $!\n";
