#!/usr/bin/perl

$NewListFileName = "echo5020.lst";
$LocalNewsgroups = "/usr/local/lib/ifmail/local_news";
$OutNewsgroups = "/usr/spool/ifmail/filefix/newsgroups.out";
$NewsgroupsReload = "/usr/local/lib/ifmail/filefix/reload.newsgroups";
$Distribution = "fido7";
$alt2koi = "/usr/local/lib/ifmail/filefix/alt2koi8";

# Name groups: -1 - lowercase, 0 - not change, 1 - uppercase
$CaseName = 1; 

if( $#ARGV == 1 ) {
	$NewList = $ARGV[0];
	$PathList= $ARGV[1];
} else {
	die "Invalid arguments";
}
$nl = $NewList;
$nl =~ tr/A-Z/a-z/;
exit( 0 ) if( $nl ne $NewListFileName );

if( !open( out, ">".$OutNewsgroups )) {
	print STDERR "Cannot create $OutNewsgroups. $!.\n";
	exit( 1 );
}
if( -e $LocalNewsgroups ) {
	if( !open( LocalNewsgroups )) {
		close( out );
		print STDERR "Cannot open $LocalNewsgropups. $!.\n";
		exit( 1 );
	}
	while( <LocalNewsgroups> ) {
		chop;
		next if( /^[ \t]*[#;]/ );
		print out $_."\n";
	}
	close( LocalNewsgroups );
}
if( defined( $Distribution ) && ( $Distribution !~ /^[ \t]*$/ )) {
	$Distribution .= "."; 
} else {
	$Distribution = ""; 
}
if( !open( in, $alt2koi." <".$PathList."/".$NewList." |" )) {
	print STDERR "Cannot exec '".$alt2koi." <".$PathList."/".$NewList." |' $!\n";
	close( out );
	exit( 1 );
}
while( <in> ) {
	chop; chop; # MS-DOS
	next if( /^[ \t]*$/ );
	next if( /^[ \t]*;/ );
	( $flag, $name, $desc, $moderator, $address ) = split( /,/, $_, 5 );
	if( $CaseName == 1 ) {
		$name =~ tr/a-z/A-Z/;
	} elsif( CaseName == -1 ) {
		$name =~ tr/A-Z/a-z/;
	}
	print out $Distribution.$name."\t".$desc."\n";
}
close( in );
close( out );
if( system( $NewsgroupsReload )) {
	print STDERR "Cannot exec $NewsgroupsReload.\n";
	exit( 1 );
}
exit( 0 );

