#!/mit/watchmaker/vaxbin/perl
	eval "exec /mit/watchmaker/vaxbin/perl -S $0 $*"
		if $running_under_some_shell;

# $Header: patpost.SH,v 2.0.1.1 88/08/05 00:30:10 lwall Exp $
#
# $Log:	patpost.SH,v $
# Revision 2.0.1.1  88/08/05  00:30:10  lwall
# patch1: now depends on perlpath
# 
# Revision 2.0  88/06/28  23:19:50  lwall
# Baseline.
# 

$inews='inews';
$orgname='MIT Project Athena';
if ($inews eq 'inews') {
    $inews = '/usr/lib/news/inews' if -f '/usr/lib/news/inews';
}

chdir '..' if -f '../.package';

do readpackage();

chop($orgname = `cat $orgname`) if $orgname =~ m|^/|;

if ($ARGV[0] eq '-r') {
    shift; $repost = ' (REPOST)';
}

while ($_ = shift) {
    if (/^(patch)?[1-9][\d\-]*$/) {
	s/^patch//;
	push(@argv,$_);
    }
    else {
	push(@newsgroups,$_);
    }
}
$newsgroups = join(',',@newsgroups) unless $#newsgroups < 0;
die "patpost newsgroups [patches]\n" unless $newsgroups;

@ARGV = @argv;
open(PL,"patchlevel.h") || die "Can't open patchlevel.h\n";
while (<PL>) {
    $maxnum = $1 if /^#define\s+PATCHLEVEL\s+(\d+)/;
}
close PL;
die "Malformed patchlevel.h file.\n" if $maxnum eq '';

if ($#ARGV < 0) {
    $argv = $maxnum;
    @ARGV = $argv;
}
else {
    $argv = do rangeargs(@ARGV);
    @ARGV = split(' ',$argv);
}

$argv =~ s/ $//;

if ($#ARGV < 0) {
    die "No patches specified.\n";
}
elsif ($#ARGV) {
    print "Posting $package $baserev patches $argv to $newsgroups...\n";
}
else {
    print "Posting $package $baserev patch $argv to $newsgroups...\n";
}

chdir 'bugs' || die "Can't cd to bugs\n";

fork && exit;

until ($#ARGV < 0) {
    $patnum = shift;
    open(XHEAD,">.xhead$$") || die "Can't create temp file.\n";
    print XHEAD
"Newsgroups: $newsgroups
Subject: $package $baserev patch #$patnum$repost
Summary: This is an official patch for $package $baserev.  Please apply it.
Expires: 
References: 
Sender: 
Distribution: 
Organization: $orgname
Keywords: 

";
    open(PATCH,"patch$patnum") || die "Can't open patch$_\n";
    while (<PATCH>) {
	print XHEAD;
    }
    close XHEAD;
    system "$inews -h <.xhead$$";
}
unlink ".xhead$$";

sub readpackage {
    if (! -f '.package') {
        if (-f '../.package' || -f '../../.package') {
            die "Run in top level directory only.\n";
        }
        else {
            die "No .package file!  Run packinit.\n";
        }
    }
    open(package,'.package');
    while (<package>) {
        next if /^:/;
        next if /^#/;
        if (($var,$val) = /^\s*(\w+)=(.*)/) {
            $val = "\"$val\"" unless $val =~ /^['"]/;
            eval "\$$var = $val;";
        }
    }
    close package;
}

sub rangeargs {
    local($result) = '';
    local($min,$max,$_);
    open(PL,"patchlevel.h") || die "Can't open patchlevel.h\n";
    while (<PL>) {
        $maxspec = $1 if /^#define\s+PATCHLEVEL\s+(\d+)/;
    }
    close PL;
    die "Malformed patchlevel.h file.\n" if $maxspec eq '';
    while ($#_ >= 0) {
	$_ = shift(@_);
	while (/^\s*\d/) {
	    s/^\s*(\d+)//;
	    $min = $1;
	    if (s/^,//) {
		$max = $min;
	    }
	    elsif (s/^-(\d*)//) {
		$max = $1;
		if ($max == 0 && $maxspec) {
		    $max = $maxspec;
		}
		s/^[^,],?//;
	    }
	    else {
		$max = $min;
	    }
	    for ($i = $min; $i <= $max; ++$i) {
		$result .= $i . ' ';
	    }
	}
    }
    $result;
}
