#!/afs/athena/contrib/perl/p
# 
# Copy the file post_faq.mit to post_faq.dat, and edit it for local conditions,
# especially where to find perl.
# 
# $Id: post_faq.pl.base,v 1.23 1992/03/30 17:41:44 jik Exp $
# 
# Copyright (c) 1991 Jonathan I. Kamens.  See the GNU Public License
# (any version) for terms of distribution.

push(@INC, "/afs/sipb/project/os2/faq");

require 'faqfile.pl';

# These variables can be overridden by command-line options.

$server = 'news.mit.edu';		# News server to which to connect, if NNTP
$default_inews = "/afs/athena.mit.edu/contrib/sipb/@sys/inews";	# Inews program to use
$idhost = 'athena.mit.edu';		# Host name to put in Message ID's
$configfile = undef;

# These can be overridden by each FAQ.

$faqfile'default_interval = 0;	# Default interval to wait between postings
$faqfile'default_sigfile = undef;	# Signature file to append to posting
$faqfile'default_force = 0;		# Force posting of this FAQ?

# These can't be overridden at all without editing the script.

@months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
	   'Sep', 'Oct', 'Nov', 'Dec');

# This controls the number of days added to the posting interval in
# order to determine how many days into the future the "Expires"
# header date should be set.  I've picked a value of 14, which
# means that I'm assuming that an article will make it to the entire
# USENET within 14 days of when it is posted.
$expire_slop = 14;

%faqs = ();
%only = ();
%omit = ();
$quiet = 0;
$newstamp = time;

# Parse command line arguments

while ($_ = $ARGV[0], /^-/) {
    shift;
    /^-interval$/ 
	&& ((@ARGV > 0) || die "Missing argument to \"-interval\" option.\n")
	    && ($faqfile'default_interval = shift) && next;
    /^-inewscmd$/ 
	&& ((@ARGV > 0) || die "Missing argument to \"-inewscmd\" option.\n")
	    && ($inews = shift) && next;
    /^-server$/ 
	&& ((@ARGV > 0) || die "Missing argument to \"-server\" option.\n")
	    && ($server = shift) && next;
    /^-idhost$/ 
	&& ((@ARGV > 0) || die "Missing argument to \"-idhost\" option.\n")
	    && ($idhost = shift) && next;
    /^-sigfile$/
	&& ((@ARGV > 0) || die "Missing argument to \"-sigfile\" option.\n")
	    && ($faqfile'default_sigfile = shift) && next;
    /^-config$/
	&& ((@ARGV > 0) || die "Missing argument to \"-config\" option.\n")
	    && ($configfile = shift) && next;
    /^-quiet$/
	&& ((@ARGV > 0) || die "Missing argument to \"-quiet\" option.\n")
	    && ($quiet = shift) && next;
    if (/^-omit$/) {
	if (@ARGV > 0) {
	    foreach $i (split(/,/, shift)) {
		$omit{$i}++;
	    }
	    next;
	}
	else {
	    die "Missing argument to \"-omit\" option.\n";
	}
    }
    if (/^-only$/) {
	if (@ARGV > 0) {
	    foreach $i (split(/,/, shift)) {
		$only{$i}++;
	    }
	    next;
	}
	else {
	    die "Missing argument to \"-only\" option.\n";
	}
    }
    /^-force$/ && ($faqfile'default_force = 2) && next;
    /^-debug$/ && ++$debug && next;
    die "Unknown option \"$_\".\n";
}

die "No config file specified!\n" if (! $configfile);

($faqfile'default_sigfile = undef) if ($faqfile'default_sigfile eq "none");

if (! defined($inews)) {
    if ($debug) {
	$inews = "cat";
    }
    else {
	$inews = $default_inews;
    }
}

$ENV{'NNTPSERVER'} = $server;

if ($debug) {
    $configin = $configfile;
    $configout = "/dev/null";
    print "Not modifying config file, because -debug specified.\n"
	if ($quiet < 1);
}
else {
    if (-z $configfile) {
	die "Configuration file is zero size.... aborting!\n";
    }
    $configin = "$configfile.old";
    $configout = $configfile;
    rename($configout, $configin) || 
	die "Renaming $configout: $!.\n";
}
open(CONFIG, "$configin") || die "Opening $configin to read: $!.\n";
open(CONFIGOUT, ">$configout") || die "Opening $configout to write: $!.\n";

while (<CONFIG>) {
    chop;
    local($idname, $file, $oldstamp, $interval, $sigfile, $force, $parent) =
	&faqfile'parse_faq($_);
    ($sigfile = $faqfile'default_sigfile) if (! $sigfile);

    if (! $idname) {
	print CONFIGOUT $_, "\n" ||
	    die "Writing to $configout: $!.\n";
	next;
    }

    if (%only) {
	if (!$only{$idname}) {
	    print "Skipping $idname because of -only.\n"
		if ($quiet < 1);
	    goto nextone;
	}
    }
    elsif (%omit) {
	if ($omit{$idname}) {
	    print "Skipping $idname because of -omit.\n"
		if ($quiet < 1);
	    goto nextone;
	}
    }

    $timeout = ($oldstamp &&
		($newstamp - $oldstamp) / (60 * 60 * 24) + 0.5 > $interval);

    if (!$oldstamp || $timeout || $force) {
	if ($parent && !&faqfile'was_posted($parent)) {
	    print "Skipping $idname because its parent was not posted.\n"
		if ($quiet < 1);
	    goto nextone;
	}
	if ($force) {
	    print "Forcing new posting of $idname.\n"
		if ($quiet < 1);
	}
	elsif (!$oldstamp) {
	    print "No previous timestamp; posting $idname.\n"
		if ($quiet < 1);
	}
	elsif ($timeout) {
	    print "Interval expired; posting $idname.\n"
		if ($quiet < 1);
	}
	open(INEWS, "|$inews");
	open(FAQ, $file) || die "Opening $file (for $idname): $!.\n";
	printf(INEWS "Message-ID: %s\n", 
	       &message_id($idname, $newstamp, $idhost)) ||
	    die "Printing to $inews pipe: $!.\n";
	if ($oldstamp) {
	    printf(INEWS "Supersedes: %s\n",
		   &message_id($idname, $oldstamp, $idhost)) ||
		die "Printing to $inews pipe: $!.\n";
	}
	if ($interval > 0) {
	    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
		gmtime($newstamp + ($interval + $expire_slop) * 
		       (60 * 60 * 24));
	    $year += 1900;
	    printf(INEWS "Expires: %d %s %d %02d:%02d:%02d GMT\n",
		   $mday, $months[$mon], $year, $hour, $min, $sec) ||
		       die "Printing to $inews pipe: $!.\n";
	}
	if ($parent) {
	    printf(INEWS "References: %s\n", 
		   &message_id($parent, &faqfile'timestamp($parent), 
			       $idhost)) ||
				   die "Printing to $inews pipe: $!.\n";
	}
	while (<FAQ>) {
	    if (/\@message-id +([^ \@]+) *\@/i) {
		local($newid) = &message_id($1, $newstamp, $idhost);
		$_ = $` . $newid . $';
	    }
	    print INEWS $_ || die "Printing to $inews pipe: $!.\n";
	}
	if ($sigfile && ($sigfile ne "none")) {
	    open(SIGFILE, $sigfile) || 
		warn "Opening \"$sigfile\": $!.\nSignature will not be included on $idname.\n";
	    print INEWS "-- \n" || die "Printing to $inews pipe: $!.\n";
	    while (<SIGFILE>) {
		print INEWS $_ || die "Printing to $inews pipe: $!.\n";
	    }
	}

	close(INEWS);
	if ($?) {
	    warn "$inews exited with non-zero status posting $idname.\n";
	    goto nextone;
	}
	close(FAQ);
	printf("Article %s posted successfully.\n",
	       &message_id($idname, $newstamp, $idhost))
	    if ($quiet < 2);
	if ($force == 1) {
	    &faqfile'set_force($idname, undef);
	    print "Force disabled on $idname.\n"
		if ($quiet < 2);
	}
	&faqfile'set_posted($idname);
	&faqfile'set_timestamp($idname, $newstamp);
    }
    else {
	print "Interval not expired; not posting $idname.\n"
	    if ($quiet < 1);
    }
nextone:
    print CONFIGOUT &faqfile'configline($idname), "\n" ||
	die "Writing to $configout: $!.\n";
}

close(CONFIG);
close(CONFIGOUT);

sub message_id {
    # Takes three arguments -- an ID name, a time, and a host name --
    # and returns the message ID to use.  If you want to do custom
    # message ID's for some or all of your FAQs, this is the routine
    # to modify.
    # 
    # Beware that this routine is used to generate message IDs of
    # parent articles as well as message IDs of articles currently
    # being posted, so don't put in any implicit dependencies that
    # won't work.

    local($idname, $time, $host) = @_;

    "<${idname}_${time}@${host}>";
}
