#!/usr/bin/perl

# $Header: /inn/bin/RCS/update_active,v 1.3 1995/11/15 05:30:13 news Exp news $

# Grabs the active file from a remote server, extracts newsgroups from
# selected hierarchies, and adds them to the local active file.
# optionally, can install and 'makeactive' the new list.
#
# INN specific

# John Milburn Postech, Korea

($program = $0) =~ s%.*/%%;

#### =()<require '@<_PATH_PERL_SHELLVARS>@';>()=
require '/var/news/etc/innshellvars.pl';

$active = $inn::active;
$newsbin = $inn::newsbin;

$nntpserver = "newsfeed.kreonet.re.kr"; # The name of the server you
                                    # want to get the active list from
#
# command to get the remote active file
$listcommand = "$newsbin/getlist -h $nntpserver active";
$newactive = "${active}.new";     # Where to put the generated active file

# Modify the following line to remove groups which you don't carry,
# and add any local groups. junk and control should be kept here.
$localgroups = "^junk|^control|^dacom|\!";

# Modify this line to list all hierarchies which you want to receive.
$remotegroups = "^[a-z]";

# Make sure we can write the output file before starting
open(NACTIVE, "> $newactive") || die "$program: can't open new active file $newactive\n";

# Make sure the active file exists
open(ACTIVE, $active) || die "$program: can't open the existing active file $active\n";

# Make sure we can connect to the NNTP server
open(NFILE, "$listcommand |") || die "$program: can't get active list from $nntpserver\n";

# This loop grabs groups from the existing active file which
# are not expected to change, or which _must_ exists (control, junk),
# or which are local, and not available from the server.

`$newsbin/ctlinnd throttle 're-building active'`;

while ( <ACTIVE> ) {
    ($group) = /^(\S+)\s.*$/;
    push(@ogroups,$group . "\n");
    ($group, $high, $low, $mode) = /^(\S+)\s(\S+)\s(\S+)\s([ymxj])$/;

    # Save the high and low marks for currently active groups
    $highmark{$group} = $high;
    $lowmark{$group} = $low;

    # Save local groups (which may not exist on the remote server
    if (/$localgroups/){
        push(@ngroups,$group . "\n");
        print NACTIVE "$group $high $low $mode\n";
    }
}
close(ACTIVE);


while (<NFILE>) {

    if (/$remotegroups/){

	if (! /$localgroups/) {
            ($group, $high, $low, $mode) = /^(\S+)\s(\S+)\s(\S+)\s([ymxj])$/;
            push(@ngroups,$group . "\n");
	    # Use the existing high and low marks, else fill with zeros
	    # for a new group.
	    $high = $highmark{$group} || ($high = "0000000000");
	    $low = $lowmark{$group} || ($low = "0000000000");
            print NACTIVE "$group $high $low $mode\n";
	}
    }
}

# Cleanup
close(NFILE);
close(NACTIVE);


# Make a marker, used for getting the difference array
local(%mark);
# Load the marker with the old groups
grep($mark{$_}++,@ogroups);
# Shove anything not marked into the 'added' array
@added = grep(!$mark{$_},@ngroups);
# Re-set the marker
local(%mark);
# Load with all new groups
grep($mark{$_}++,@ngroups);
# Shove anything not marked into the 'deleted' array
@deleted = grep(!$mark{$_},@ogroups);

print "Groups added:\n";
print " @added\n";
print "Groups removed:\n";
print " @deleted\n";

print "\n";
print "Doing the next step is dangerous, and could trash your active file.\n";
print "Please be absolutely sure that the new active file is correct before\n";
print "using the automatic install feature.\n\n";

print "Would you like me to install the new active file (y/n)? ";

chop($ans = <STDIN>);

if ( $ans =~ /[Yy]/ ) {
    sleep 5;
    rename($active,"$active.old");
    rename($newactive,$active);
    chown((getpwnam("news"))[2,3], $active);
    chmod 0644, $active;
    `$newsbin/ctlinnd reload active 're-building active'`;
    `$newsbin/ctlinnd go 're-building active'`;
}

