#!/usr/local/bin/perl -w-- # -*- perl -*-
#
# $Id: files_with_symbolic_name,v 1.2 1994/09/10 18:13:51 qjb Exp $
# $Source: /home/qjb/scripts/RCS/files_with_symbolic_name,v $
# $Author: qjb $
#

# 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 (scalar(@ARGV) != 2);
($dir, $symname) = @ARGV;
$dir =~ s,/$,,;			# strip trailing /

&usage if (! -d $dir);

$| = 1;  # unbuffered output for instant gratification

open(FIND, "find $dir/. -type f -name '*,v' -print|") ||
    die "$whoami: can't run find over $dir/.: $!\n";
while (<FIND>)
{
    chop;
    $line = $_;
    $line =~ s,$dir/.,$dir,;
    $line =~ s,/RCS/,/,;
    $line =~ s/,v$//;
    (print $line, "\n") if (&hasname($line, $symname));
}
close(FIND);

sub hasname {
    local($file, $name) = @_;
    local($in_symnames) = 0;
    local($found) = 0;
    $name = "\t" . $name . ":";

    if (open (RLOG, "rlog -h $file|"))
    {
	while (<RLOG>)
	{
	    chop;
	    if ($in_symnames == 0)
	    {
		if (m/^symbolic names/)
		{
		    $in_symnames = 1;
		}
	    }
	    else
	    {
		if (m/^[a-z]/)
		{
		    $in_symnames = 0;
		}
		else
		{
		    s/:(.*)/:/;
		    ($found = 1) if ($name eq $_);
		}
	    }
	}
	close(RLOG);
    }
    $found;
}

sub usage {
    print STDERR "Usage: $whoami directory symbolic-name\n";
    print STDERR "  Prints names of all files under directory " .
	"that have a revision under RCS\n";
    print STDERR "  marked with the given symbolic name.\n";
    exit 1;
}
