#! /usr/athena/bin/perl

$ftpdir = "/pub/usenet";
$maildir = "usenet";
$ftproot = "/rtfm/ftp";

@newsgroupss = ();
@titles = ();
@subjects = ();
@filenames = ();

$newsgroups = "";
$subject = "";
$lastnewsgroups = "";
$shortnames = undef;

if ($ENV{'PATH'} !~ m,(^|:)/usr/ucb($|:),) {
    $ENV{'PATH'} .= ":/usr/ucb";
}

sub protect {
    local($str) = @_;

    $str =~ tr/ \011\140\047\042\134\057/_/;

    return($str);
}

sub parse_newsgroups {
    local($group_str) = @_;

    $group_str =~ s/\s*$//;
    $group_str =~ s/^Newsgroups:\s*//;

    return($group_str);
}

sub parse_subject {
    local($sub_str) = @_;

    $sub_str =~ s/\s*$//;
    $sub_str =~ s/^Subject:\s*//;

    return($sub_str);
}

sub doprint {
    @prot = ();

    $plural = (@titles > 1);

    @l1 = ();
    @l2 = ();

    for ($i = 0; $i <= $#titles; $i++) {
	$newsgroups = @newsgroupss[$i];
	$subject = @subjects[$i];

	if ($subject ne "") {
	    push(@l1, "$subject");
	    push(@l2, "$newsgroups");
	}
    }

    for ($i = 0; $i < $#l1; $i++) {
	print "Subject: @l1[$i]\n";
	if ("@l2[$i]" ne "@l2[$i + 1]") {
	    print "Newsgroups: @l2[$i]\n\n";
	}
    }
    print "Subject: @l1[$i]\nNewsgroups: @l2[$i]\n\n";

    print "Available in the indicated USENET newsgroup(s), or via anonymous ftp from\nrtfm.mit.edu (18.181.0.24) in the file", ($plural ? "s" : ""), ":\n\n";

    for ($i = 0; $i <= $#titles; $i++) {
	if ($filenames[$i] =~ s,.*/usenet/([^/]+\.answers/.+)$,$1,) {
	    ;
	}
	else {
	    @newsgroups = split(/,/, @newsgroupss[$i]);
	    $title = $titles[$i];
	    $filenames[$i] = "$newsgroups[0]/$title";
	    if ($shortnames) {
		local($shortname);
		$shortname = &shortname($filenames[$i]);
		if (-f "$ftproot/$ftpdir/$shortname") {
		    $filenames[$i] = $shortname;
		}
	    }
	}
	print "$ftpdir/$filenames[$i]\n";
    }

    print "\nAlso available from mail-server@rtfm.mit.edu by sending a mail\nmessage containing", ($plural ? " any or all of" : ""), ":\n\n";

    for ($i = 0; $i <= $#titles; $i++) {
	print "send $maildir/$filenames[$i]\n";
    }

    print "\nSend a message containing \"help\" to get general information about the\nmail server.\n";

}

sub directory_of {
    local($name) = @_[0];
    $name =~ s,/[^/]*$,,;
    $name;
}

sub filename_of {
    local($name) = @_[0];
    $name =~ s,.*/,,;
    $name;
}

sub shortname {
    local($fullpath) = $_[0];
    local($directory) = &directory_of($fullpath);
    local($filename) = &filename_of($fullpath);
    $filename =~ s/([A-Za-z])[A-Za-z]+/$1/g;
    "$directory/$filename";
}
	
sub newsgroup_archive_names {
    local($newsgroup) = @_;
    local($cname) = $newsgroup;

    $cname =~ s/\./-/g;
    $cname .= "-archive-name";

    if ($newsgroup =~ /\.answers$/) {
	return($cname, 'archive-name');
    }
    else {
	return($cname);
    }
}

sub do_zcat {
    local($file) = @_;
    local($pid);
    
    pipe(ZCATREAD, ZCATWRITE);

    $pid = fork;

    if (! defined($pid)) {
	undef;
    }
    elsif ($pid) {
	close(ZCATWRITE);
	return("ZCATREAD");
    }
    else {
	close(ZCATREAD);
	open(STDOUT, ">&ZCATWRITE");
	exec("/usr/bin/zcat", $file);
	die "Exec'ing \"zcat\": $!.\n";
    }
}

unshift(@ARGV,'-') if $#ARGV < $[;

while (($_ = $ARGV[0]) && /^-./) {
    shift;
    if (/^-shortnames$/) {
	$shortnames++;
    }
    elsif (/^--$/) {
	last;
    }
    else {
	die "Unknown option \"$_\".\n";
    }
}

while ($ARGV = shift) {

    if ($ARGV =~ s/\.Z$//) {
	($fhandle = &do_zcat($ARGV)) ||
	    die "Couldn't zcat $ARGV.\n";
    }
    elsif (open(ARGV, $ARGV)) {
	$fhandle = "ARGV";
    }
    elsif (-f "$ARGV.Z") {
	($fhandle = &do_zcat($ARGV)) ||
	    die "Couldn't zcat $ARGV.\n";
    }
    else {
	die "Couldn't open $ARGV.\n";
    }

    while (<$fhandle>) {
	chop;
	if (/^Subject:/) {
	    $subject = &parse_subject($_);
	}
	elsif (/^Newsgroups:/) {
	    $newsgroups = &parse_newsgroups($_);
	}
	if (($subject ne "") && ($newsgroups ne "")) {
	    last;
	}
	if ($_ eq "") {
	    last;
	}
	if (/^[^ \t][^:]*$/) {
	    last;
	}
    }
    close($fhandle);

    @temp = split(/\//, $ARGV);
    push(@subjects, $subject);
    if ($newsgroups ne "") {
	$lastnewsgroups = $newsgroups;
    }
    push(@newsgroupss, $lastnewsgroups);
    push(@titles, @temp[$#temp]);
    push(@filenames, $ARGV);
    $subject = "";
    $newsgroups = "";
}

if (@titles > 0) {
    &doprint;
}

exit(0);
