#!/usr/local/bin/perl
$locks='/news/locks';
$newsgroups='/usr/local/news/etc/newsgroups';

sub Die {

 $where=@_;

 if (!$where) {
	print "Can't read newsgroups file\n";
 } else {
	print "Can't write newsgroups file\n";
 }
 unlink "$locks/LOCK.newsgroups";
 exit 1;
}

die "Can't find inn.newsgroups" unless (-e 'inn.newsgroups');

$locks='/news/locks';
$newsgroups='/usr/local/news/etc/newsgroups';

while (-e "$locks/LOCK.newsgroups") {
	print "Newsgroups file in use ,sleeping...\n";
	sleep 1;
}

`touch $locks/LOCK.newsgroups`;

open(NGF,$newsgroups)||&Die(0);

while (<NGF>) {
	if (/^(\S+)\s+(.+)$/) {
		$des{$1}=$2;
	}		
}
close NGF;

open(CH,'inn.newsgroups');
while(<CH>) {
	if (/^\+(\S+)\s+(.+)/) {
		$des{$1}=$2;
	} elsif (/^-(\S+)/) {
		undef $des{$1};
	}
}
close CH;

$t=time;
rename "$newsgroups","$newsgroups.$t";

open(NGF,">$newsgroups")||&Die(1);
foreach $i (sort keys %des) {
	print NGF "$i\t$des{$i}\n";
}
close NGF;

unlink "$locks/LOCK.newsgroups";

