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

# $Header: patbase.SH,v 2.0.1.1 88/08/05 00:29:09 lwall Exp $
#
# $Log:	patbase.SH,v $
# Revision 2.0.1.1  88/08/05  00:29:09  lwall
# patch1: now depends on perlpath
# 
# Revision 2.0  88/06/28  23:19:25  lwall
# Baseline.
# 
# 


die "pat [files]\n" unless $#ARGV >= 0;

do readpackage();

$RCSEXT = ',v' unless $RCSEXT;

if ($ARGV[0] eq '-a') {
    open(MANI,"MANIFEST.new") || die "No MANIFEST.new found.\n";
    @ARGV = ();
    while (<MANI>) {
        chop;
	($_) = split(' ');
        push(@ARGV,$_);
    }
    close MANI;
}

foreach $file (@ARGV) {
    $files = do rcsargs($file);
    @files = split(' ',$files);
    $revs=0;
    $rlog = `rlog -r$baserev -r$revbranch $files 2>&1`;
    ($revs) = ($rlog =~ /selected revisions: (\d+)/);
    if (!$revs) {
	print "patbase: $file has never been checked in--checking in...\n";
	$comment = rcscomment($file);
	system 'rcs', '-i', "-c$comment", @files if $comment;
	(system 'ci', "-l$baserev", @files) ||
	  (system 'rcs', "-Nlastpat:$baserev", @files);
    }
    elsif ($revs == 1) {
	print "Last revision for $file is $baserev.\n";
	system 'rcs', "-Nlastpat:$baserev", @files;
    }
    else {
	($lastrev) = ($rlog =~ /revision $revbranch\.(\d+)/);
	print "Last revision for $file is $revbranch.$lastrev.\n";
	system 'rcs', "-Nlastpat:$revbranch.$lastrev", @files;
    }
}
unlink ".rlog$$";

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 rcsargs {
    local($result) = '';
    local($_);
    while ($_ = shift(@_)) {
	if ($_ =~ /^-/) {
	    $result .= $_ . ' ';
	}
	elsif ($#_ >= 0 && do equiv($_,$_[0])) {
	    $result .= $_ . ' ' . $_[0] . ' ';
	    shift(@_);
	}
	else {
	    $result .= $_ . ' ' . do other($_) . ' ';
	}
    }
    $result;
}

sub equiv {
    local($s1, $s2) = @_;
    $s1 =~ s|.*/||;
    $s2 =~ s|.*/||;
    if ($s1 eq $s2) {
	0;
    }
    elsif ($s1 =~ s/$RCSEXT$// || $s2 =~ s/$RCSEXT$//) {
	$s1 eq $s2;
    }
    else {
	0;
    }
}

sub other {
    local($s1) = @_;
    ($dir,$file) = ('./',$s1) unless local($dir,$file) = ($s1 =~ m|(.*/)(.*)|);
    local($wasrcs) = ($file =~ s/$RCSEXT$//);
    if ($wasrcs) {
	`mkdir $dir` unless -d $dir;
	$dir =~ s|RCS/||;
    }
    else {
	$dir .= 'RCS/';
	`mkdir $dir` unless -d $dir;
	$file .= $RCSEXT;
    }
    "$dir$file";
}

sub rcscomment {
    local($file) = @_;
    local($comment) = '';
    open(FILE,$file);
    while (<FILE>) {
	if (/^(.*)\$Log[:\$]/) {		# they know better than us (hopefully)
	    $comment = $1;
	    last;
	}
    }
    close FILE;
    unless ($comment) {
	if ($file =~ /\.SH$|[Mm]akefile/) {
	    $comment = '# ';
	}
	elsif ($file =~ /\.U$/) {
	    $comment = '?RCS: ';
	}
	elsif ($file =~ /\.man$/) {
	    $comment = "''' ";
	}
    }
    $comment;
}

