#!/usr/local/bin/perl -w-- # -*- perl -*-
#
# $Id: rcs_stamp,v 1.3 1996/07/08 23:03:52 ejb Exp $
# $Source: /home/ejb/scripts/RCS/rcs_stamp,v $
# $Author: ejb $
#

# This code, from the perl manual page, forces this to be run by perl from 
# perl, sh, or csh.  It must be first.
eval '(exit $?0)' && eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
& eval 'exec /usr/local/bin/perl -S $0 $argv:q'
    if 0;

$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

&usage if (@ARGV < 1);
$stamp = shift(@ARGV);
@dirs = (scalar(@ARGV) ? @ARGV : ("."));
map { s,/$,,; $_ .= "/."; } @dirs;
my $dirs = join(' ', @dirs);

open(FIND, "find $dirs \\( -name '*,v' -o -name '.*,v' \\) -type f -print |");
while (<FIND>)
{
    s,/\./,/,g;
    s,^\./,,;
    chop;
    push(@files, $_);
}
close(FIND);

# print extra blank line for ease of reading in a log
print "\n";
while (scalar(@files) > 0)
{
    @check = splice(@files, 0, 200);
    $cmd = "rlog -h " . join(' ', @check) . " | egrep '^(RCS file|head):'";
    open(CMD, "$cmd|") || die "$whoami: failed to run $cmd\n";
    while (<CMD>)
    {
	chomp;
	if (s/^RCS file: //)
	{
	    $file = $_;
	    $file =~ s,(\W?)RCS/,$1,;
	    $file =~ s/,v$//;
	}
	elsif (s/^head: //)
	{
	    push(@result, "$stamp $file $_\n");
	}
	else
	{
	    warn "$whoami: unknown grep output $_\n";
	}
    }
    close(CMD);
}

for (sort @result)
{
    print;
}

sub usage
{
    die "Usage: $whoami stamp [dir ...]\n";
}
