#!/usr/local/bin/perl

	$VERSION		= "1.4";
	$MOD_DATE		= "10.Oct.94";
	$AUTHOR			= "Paul Hulford";

# 10-Oct-94 PDH hgmodify -N option changed to -oid
# 10-Oct-94 PDH hgmodify -K option changed to -comm 
# 04-Aug-94 PDH Cannont background system calls since too easy
#               to overload server 
# 04-Aug-94 PDH Need -E on hinfo query else fails on 1 obj coll 
# 29-Jul-94 PDH Change $0 to hgchattribs

# This utility allows you to change an attribute for all
# documents within a collection 

require "getopts.pl";
&Getopts("vhc:o:n:") || &help && die;

die "\nhgchattribs v$VERSION - $AUTHOR $MOD_DATE\n\n" if $opt_v;
&help && exit if $opt_h;

#
# Help options
#

sub help
{
	print "\n";
	print "Description:\n";
	print "         Changes an attribute of all objects in a\n";
	print "         collection with that attribute\n"; 
	print "\n";
	print "Usage:\n";
	print "         hgchattribs -h\n";
	print "         hgchattribs -v\n";
	print "         hgchattribs -c collection [-o old_attrib] [-n new_attrib]\n"; 
	print "\n";
	print "Options:\n";
	print "        -h              -- help\n";
	print "        -v              -- version information\n";	
	print "        -c collection   -- Name of collection\n"; 
	print "        -o old_attrib   -- Old attribute and value\n"; 
	print "        -n new_attrib   -- New attribute and value\n"; 
	print "\n";
	print "Notes:\n";
	print "        old_attrib and new_attribute both have the\n";
	print "        form \"Attribute=value\". See man hginfo\n";
	print "        One of these args must be present\n";
	print "        Only one present means that this attribute\n";
	print "        is either deleted or added only\n";
	print "\n";
}

$collection = $opt_c || die("ERROR! -c option required\n"); 
$old_attrib = $opt_o; 
$new_attrib = $opt_n;
$old_attrib || $new_attrib || die("ERROR! -o or -n option required\n"); 

$list = `hginfo -C "$collection" -E ObjectID`;
@objs = split(' ',$list); 
 
$ii=0; # count how many we change
$| = 1;

while($objs[0])
{
	chop;
	system("hgmodify -oid $objs[0] -comm \"rem $old_attrib\"") 
		if ($old_attrib && ! $new_attrib);
	system("hgmodify -oid $objs[0] -comm \"add $new_attrib\"") 
		if ( ! $old_attrib && $new_attrib);
	system("hgmodify -oid $objs[0] -comm \"rem $old_attrib\\add $new_attrib\"") 
		if ($old_attrib && $new_attrib);
	print ".";
	$ii++;
	shift @objs;
}

print "\n\n$ii Objects Processed.\n";

