#! /usr/bin/perl
# fixscript will replace this line with code to load INN::Config
#
# Create storage.conf script based on recently read articles so as to put
# read newsgroups into a timecaf spool area instead of the big CNFS area.
#
# Written by Richard Michael Todd in 1999.
#
# Various bug fixes, code and documentation improvements since then
# in 2001, 2007, 2022, 2024, 2026.

use warnings;

$readfile = "$INN::Config::pathdb/readgroups";
$outfile = "$INN::Config::pathdb/storage.conf";

OUTLOOP:
for ($level = 9; $level >= 2; --$level) {
    # clear groups hash.
    foreach my $i (keys %groups) {
        delete $groups{$i};
    }
    if (open(RDF, "$INN::Config::sort $readfile|")) {
        while (<RDF>) {
            chop;
            next if (/^group/);   # bogus
            @foo = split(/ /);    # foo[0] should be group, foo[1] lastreadtime
            @bar = split(/\./, $foo[0]);
            if ($level >= scalar @bar) {
                $grf = join(".", @bar);
            } else {
                $grf = join(".", @bar[0 .. ($level - 1)]) . ".*";
            }
            $groups{$grf} = 1;
        }
        close(RDF);
    }
    $grlist = join(",", keys(%groups));
    last OUTLOOP if (length($grlist) < 2048);
}

open(OUT, ">$outfile") || die "cant open $outfile";
#open(OUT, ">/dev/tty");

print OUT <<"EOF";
method cnfs {
        newsgroups: control,control.*
        class: 1
        options: MINI
}

method timecaf {
        newsgroups: $grlist
        class: 1
}

method cnfs {
        newsgroups: *
        options: MONGO
        class: 0
}
EOF
close(OUT);
exit(0);
