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

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

$orgname='MIT Project Athena';

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

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

do readpackage();

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

@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 "Sending $package $baserev patches $argv to $dest...\n";
}
else {
    print "Sending $package $baserev patch $argv to $dest...\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
"To: $dest
Subject: $package $baserev patch #$patnum
Organization: $orgname

[The latest patch for $package version $baserev is #$maxnum.]

";
    open(PATCH,"patch$patnum") || die "Can't open patch$patnum\n";
    while (<PATCH>) {
        print XHEAD;
    }
    close XHEAD;
    system "/usr/lib/sendmail -odq -t <.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;
}
