#!/usr/bin/perl
# add unknown groups from foreign active files                    -*- perl -*-

# Examine the active file(s) given as input for unknown groups and add
# them to your own active file.

# Olaf Titz, Sep. 95. Public domain.
# $Id: addgrps,v 1.2 1996/02/23 00:21:38 olaf Exp $
# ----------------------------------------------------------------------------

require "psl-aux.pl";
require "getopts.pl";

&auxinit("addgrps");
&Getopts("cm");

$havenew=0;
while(<>) {
    if (++$progress>50) {
       print STDOUT "."; $progress=0;
    }
    ($g,$x,$y,$f) = split;
    if (!$active{$g}) {
       &addgroup($g,$f);
       ++$havenew;
    }
}

# Bonus feature: Check newsgroups, active.times consistency
if ($opt_c) {
    $Cat=$Can=$Cdt=$Cdn=0;
    foreach $i (keys %active) {
       next if ($i =~ /^(control|general|junk)/);
       ($x,$y,$f) = split(/ /, $active{$i});
       next unless ($f =~ /^[ymn]/);
       if (!$activetimes{$i}) {
           $activetimes{$i} = $tstr;
           ++$Cat;
       }
       while ($newsgroups{$i} =~ s/\s*\(Moderated\)$//i) {};
       if ($f eq "m") {
           $newsgroups{$i} .= " (Moderated)";
       }
    }
#    foreach $i (keys %activetimes) {
#       if (!$active{$i}) {
#           delete $activetimes{$i};
#           ++$Cdt;
#       }
#    }
    foreach $i (keys %newsgroups) {
       if (!$active{$i}) {
           delete $newsgroups{$i};
           ++$Cdn;
       }
    }
    print LOG "Corrected:";
    print LOG " added $Cat to active.times, $Can to newsgroups\n";
    print LOG " deleted $Cdt from active.times, $Cdn from newsgroups.\n";
}

# If X and X.misc exist, alias X to X.misc
if ($opt_m) {
    $Cal=0;
    foreach $i (keys %active) {
       next if ($i =~ /^(control|general|junk)/);
       ($x,$y,$f) = split(/ /, $active{$i});
       next unless ($f =~ tr/ymn//);
       $m = "${i}.misc";
       if ($n=$active{$m}) {
           ($x2,$y2,$f2) = split(/ /, $n);
           if ($f2 eq $f) {
               print LOG "aliased $i to $m\n";
               $active{$i} = "$x $y =$m";
               ++$Cal;
           }
       }
    }
    print LOG "Corrected: aliased $Cal\n";
}

print LOG "Added $havenew groups.\n";
&auxexit($havenew||$Cat||$Can||$Cdt||$Cdn||$Cdl||$Cal);
unlink("LOCK");
exit 0;
