#!/usr/local/bin/perl

	$VERSION		= "1.2";
	$MOD_DATE		= "09.Aug.94";
	$AUTHOR			= "Paul Hulford";

# 02-Nov-94 PDH update description of script 
# 09-Aug-94 PDH change $0 to hgcolltree command name

# This utilty lists the complete tree of collection nodes headed
# by the supplied collection name
# It lists each collection only once even it is encountered several
# times 

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

die "\nhgcolltree 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 "         Recursively list the child collections of a collection\n";
	print "         and then their children etc. Collections are listed by\n"; 
	print "         name\n"; 
	print "\n";
	print "Usage:\n";
	print "         hgcolltree -h\n";
	print "         hgcolltree -v\n";
	print "         hgcolltree -c collection\n"; 
	print "\n";
	print "Options:\n";
	print "        -h              -- help\n";
	print "        -v              -- version information\n";	
	print "        -c collection   -- Name of start collection\n"; 
	print "\n";
}

$|=1;

$root_coll = $opt_c || die("ERROR! -c option required\n"); 

$ii=0;  #currently processing this element of list


$NameList{$root_coll}++;

while( $coll_name = (keys %NameList)[0] )
{
	print "$coll_name\n";

	delete $NameList{$coll_name};

	open( CL, "hginfo -M -C \"$coll_name\" |" );

	while(<CL>)
	{
		/^Name/ || next;
		/^Name=(.*)$/ && ($child_name = $1);

		while(<CL>){ /^Subdocs/ && last };

		/^Subdocs=(.*)$/ && ($no_docs="$1"); 
		
		$NameList{$child_name}++ if $no_docs;
		print "$child_name\n" if ! $no_docs;
	}	

	close CL;
}

