#!/usr/bin/perl
#Contact: arl, pjk
#Usage: list_files_with_tag tagname
#   Returns list of files under CVS control, relative to current 
#   directory, which can be updated or checked out with the tag tagname.
#Modified to also list files not currently checked out (arl, 4/2002)
#Version: $Id: list_files_with_tag,v 1.1 2003/06/13 15:38:02 jla Exp $

#test that a tagname was given as an argument
if( "$#ARGV" ne "0" ) {
   print "\nUsage: list_files_with_tag tagname\n\n";
   print "   Returns list of files under CVS control, relative to current\n";
   print "   directory, which can be updated or checked out with the tag tagname.\n\n";
   exit;
}
$tag = @ARGV[0];

#use cvs status on files in the current directory to determine which
#have the given tag.  This includes those files which have been deleted
#on the branch tag.  Then parse the output into %files.

@cvsstatus = `cvs status -v 2>&1`;
$i = 0;
while($i <= $#cvsstatus) {
    if( $cvsstatus[$i] =~ /cvs status: Examining (\S+)/ ) {
	$thisdir = $1;
    }
    elsif( $cvsstatus[$i] =~ /^File: \w/ ) {
	@thisline = split ' ',$cvsstatus[$i];
	$currentfile = $thisline[1];
    }
    elsif( $cvsstatus[$i] =~ /Existing Tags/ ) {
#skip a line
	$i++;
#Each tag line is of the form tag   (branch/revision...)
	while ($cvsstatus[$i] =~ s/\(.+\)//g) {
#remove blanks
	    $cvsstatus[$i] =~ s/\s+//g;
#create hash of files (unique list).  Don't print "./" in front of files.
	    if ($tag =~ /^$cvsstatus[$i]$/) {
                if( "$thisdir" eq "." ) {
		   $files{"$currentfile"} = 1;
                }
                else {
		   $files{"$thisdir/$currentfile"} = 1;
                }
	    }
	    $i++;
        }
    }
    $i++;
}

#Add list of files that would be checked out with this tag.  
#This will include new files not currently checked out.

@cvsupdate = `cvs update -p -d -r $tag 2>&1`;
$i = 0;
while($i <= $#cvsupdate ) {
   if( $cvsupdate[$i] =~ /Checking out/ ) {
      @line = split(' ', $cvsupdate[$i]);
      $files{"$line[2]"} = 1;
   }
   $i++;
}

@unique_files = sort(keys(%files));

if( @unique_files ) {
   foreach $file (@unique_files) {
      print "$file\n";
   }
}
