#!/usr/local/bin/perl5
#
#   Copyright (C) 1998,1999,2002
#   Associated Universities, Inc. Washington DC, USA.
# 
#   This program is free software; you can redistribute it and/or modify it
#   under the terms of the GNU General Public License as published by the Free
#   Software Foundation; either version 2 of the License, or (at your option)
#   any later version.
# 
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#   more details.
# 
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   675 Massachusetts Ave, Cambridge, MA 02139, USA.
# 
#   Correspondence concerning AIPS++ should be addressed as follows:
#          Internet email: aips2-request@nrao.edu.
#          Postal address: AIPS++ Project Office
#                          National Radio Astronomy Observatory
#                          520 Edgemont Road
#                          Charlottesville, VA 22903-2475 USA
#

require "find.pl";

$rev = "";
@tags = ();
@Tags = ();
$dir = "";

$usage = "usage \"atag [-r <revision>] -[t|T] <tag> [-[t|T] <tag>]* <directory>\"\n";

sub wanted {
    
    if ( /,v$/ &&  (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
	-f _ && -r _ ) {
	my $file = $_;
	foreach $tag ( @tags ) {
            open( SYS, "rcs -q -T -n$tag:$rev $file 2>&1 |" );
            while (<SYS>) { print; }
            close(SYS);
	}
	foreach $tag ( @Tags ) {
            open( SYS, "rcs -q -T -N$tag:$rev $file 2>&1 |" );
            while (<SYS>) { print; }
            close(SYS);
	}
    }
}

while ( ($_ = shift(@ARGV)) ) {
    if ( m/^-r$/ ) {
        die "Multiple revisions, $usage" unless ! $rev;
        $rev = shift(@ARGV);
	next;
    }
    if ( m/^-r(\S+)$/ ) {
        die "Multiple revisions, $usage" unless ! $rev;
        $rev = $1;
	next;
    }
    if ( m/^-t$/ ) {
        my $tag = shift(@ARGV);
        die "empty tag, $usage" unless "$tag";
        die "bad tag, $usage" unless $tag =~ m/[a-zA-Z]/;
        die "bad tag, $usage" if $tag =~ m/\s/;
        push( @tags, $tag );
	next;
    }
    if ( m/^-t(\S+)$/ ) {
        $tag = $1;
        die "empty tag, $usage" unless $tag;
        die "bad tag, $usage" unless $tag =~ m/[a-zA-Z]/;
        die "bad tag, $usage" if $tag =~ m/\s/;
        push( @tags, $tag );
        next;
    }
    if ( m/^-T$/ ) {
        my $tag = shift(@ARGV);
        die "empty tag, $usage" unless $tag;
        die "bad tag, $usage" unless $tag =~ m/[a-zA-Z]/;
        die "bad tag, $usage" if $tag =~ m/\s/;
        push( @Tags, $tag );
	next;
    }
    if ( m/^-T(\S+)$/ ) {
        $tag = $1;
        die "empty tag, $usage" unless $tag;
        die "bad tag, $usage" unless $tag =~ m/[a-zA-Z]/;
        die "bad tag, $usage" if $tag =~ m/\s/;
	print "\t\t\t=>> $tag\n";
        push( @Tags, $tag );
        next;
    }

    die "Multiple directories, $usage" unless ! $dir;
    $dir = $_;
}

die "No tag, $usage" unless scalar(@tags) > 0 || scalar(@Tags) > 0;
die "No directory, $usage" unless $dir;
die "Bad directory, $usage" unless stat($dir) && -d _;

$| = 1;
find($dir);
