#------------------------------------------------------------
# Clearcase File Move/Rename Automation

# Rational Software Corporation.CopyRight 2001

#------------------------------------------------------------

$inputparam=$#ARGV;
if ($inputparam<1){
	print "No Destination information provided";
	exit(0);
}
#is it a Snapshotview?
@whatview=getviewinfo(".");

if (grep(/SNAPSHOT/,@whatview)){
	$snapshot=0;
}
else{
	if (grep(/DYNAMIC/,@whatview)){
		$snapshot=1;
	}
	else{
		print "The present working directory is not in a View.";
		exit(0);
	}
}

# 2 params
if ($inputparam==1){
	$source=$ARGV[0];
	$target=$ARGV[1];
	


	# is the source an element?
	$c=`cleartool ls $source 2>&1`;
	if ($c=~"Error"){
		#Cannot perform rename/move.
		print $c;
		exit(0);
	}
	# yes, the source is an element.
	else{
		#print "$source".' is an element';
		#is the source a file or a directory?
		#0 refers to file type and 1 refers to Folder type.
		
		if (-d $source){
			print "source must be a file";
			exit (0);
		}
		#checkout the parent of the source
		#print "\n";
		#print "checking out the parent of the source";
		$cmd=`cleartool co -nc . 2>&1`;
		#is the destination a file and not an element?
		if (!(-d $target)){
			@cmdT=`cleartool ls $target 2>&1`;
			$match="Rule";
			if (grep($match,@cmdT)){
				#print "\n";
				#print "does the target exist?";
				if (-e $target){
					
					#print "checking out source with -ndata";	
					$cmd=`cleartool co -ndata -nc $source 2>&1`;
				}
			}
			else{
				#print "\n";
				#print "not checking out source";	
				#$cmd=`cleartool co  -nc $source 2>&1`;
			}
					
		}	
		else{
			#print "\n";
			#print "not checking out source";	
			#$cmd=`cleartool co  -nc $source 2>&1`;
		}
					
	}
	
	#destination type?
	$targetType=0;
	if (-d $target){
		$targetType=1;
	}
	#is the destination an element?
	$c=`cleartool ls $target 2>&1`;
	
	if ($c=~"not"){
		if ($targetType==1){
		print "The Destination directory is not an element in the Vob";
		exit(0);
		}
	}
	else{
		if ($targetType==1){
			#checkout the directory element
			#print "\n";
			#print "checking out destination directory element";	
			$cmd=`cleartool co -nc $target 2>&1`;
		}
	}
		
	#For all other cases,checkout parent and perform move
	$parent1=getparent($target);
	#print "\n";
	#print "destination's parent:";
	#print $parent1."\n";
	
	if (($parent1 ne ".") or ($parent1 ne $target)){
		
		#print "\n";
		#print "Checkout parent of target";
		$cmd=`cleartool co -nc $parent1 2>&1`;
	}
	#move source to destination
	$ret=ccmove($source,$target);
	if ($ret=~"Moved"){
		#print "\n";
		#print "checkin the parents";
		if ($parent1 ne "."){
		$cmd=`cleartool ci -nc -identical $parent1 2>&1`;
		}
		$cmd=`cleartool ci -nc -identical . 2>&1`;
		print "\n";
		print $ret;
		exit(0);
	}
	else{
		print $ret;
		print "\n";
		exit(0);		
	}
}
if ($inputparam>1){
#to do
	$t=$ARGV[$inputparam];
	#is the destination a directory element?
	if (-d $t){
		$parT=getparent($t);
		$dirtest=`cleartool ls $parT 2>&1`;
		if (grep($t,$dirtest)){
			#checkout the destination
			#print "\n,Checkout target dir";
			$cc=`cleartool co -nc $t 2>&1`;
		}
		else{
			print "\n";
			print "The destination must be a directory element";
			exit(0);
		}
	}
	else{
			print "The destination is not a directory element";
			exit(0);
	}
	
	#checkout the source dir
	$cc=`cleartool co -nc . 2>&1`;
	$src1="";
	for ($i=0;$i<$inputparam-1;$i++){
		$src1=$src1." ".$ARGV[$i];
		#print $src1;
		#print "\n";
	}
	$src1=$src1." ".$ARGV[$inputparam-1];
	#print $src1;
	#print "\n";
	#move source to destination
	@ret=ccmove($src1,$t);
	print @ret;
	#checkin source:
	$cc=`cleartool ci -nc -identical . 2>&1`;


}


#----------------------------------------------

#ccmove Function:

sub ccmove{
	my ($src,$tgt)=@_;
	#perform a cc move operation
	#print "\n";
	#print "Moving source to target";
	$cmd1=`cleartool move $src $tgt 2>&1`;
	$cmd2="";
	if ($snapshot==0){
		#print "snapshot view"."\n";
		$oldtgt=$tgt.".renamed";
		$cmd2=`move $oldtgt $tgt 2>&1`;
	}
	#checkin the target elem.
	if ($cmd1=~"Moved"){
		#if the target is co then ci
		#print "\n";
		#print "is the target co? ";
		#print $tgt;
		#get tgt parent
		if (-d $tgt){
		$parentofT=getparent($tgt);
		#print "\n";
		#print "parent of tgt:".$parentofT;
		
		}
		else{
			$parentofT=$tgt;
		}
		#now check-in as necessary
		@cmd3=`cleartool lsco $parentofT 2>&1`;
		if (grep($tgt,@cmd3)){
			#print "\n";
			#print "checking in target";
			$cmd3=`cleartool ci -nc $tgt 2>&1`;
		}
		

	}
	return $cmd1;
	
	
}

#----------------------------------------------------------

#iserror function
sub iserror{
	my ($errStr)=@_;
	if ($errStr=~"Error"){
		return 1;
	}
	else{
		return 0;
	}
}


#--------------------------------------------------------

#getParent function
sub getparent{
	my ($str)=@_;
	@parsedir=split("\\\\",$str);
	$index=$#parsedir;
	if ($index==-1){
		$parent=".";
	}
	else{
		$parent=$parsedir[0];
		if ($index>0){
			for ($i=1;$i<$index;$i++) {
				$parent=$parent."\\".$parsedir[$i];
			}
		}
	}
	return $parent;
}
	
#----------------------------------------------------------------

#getViewInfo function


sub getviewinfo{
my ($FolderPath) = @_;

# Define Variables
my $ViewTag = "NONE";
my $ViewType = "NONE";
my $VOBType = "NONE";


# $FolderPath =~ s/\\/\\\\/g;

# Check if Argument exist
if ( !$FolderPath )
{
	return "$ViewTag, $ViewType, $VOBType\n";
	exit(0);
}

# Check Folder Path exist
if ( ! -e $FolderPath )
{
	return "$ViewTag, $ViewType, $VOBType\n";
	exit(0);
}

chdir($FolderPath);

$ViewOutput = `cleartool lsview -long -cview 2>&1`;

if (!$ViewOutput)
{	
	return "$ViewTag, $ViewType, $VOBType\n";
	exit(0);
}
else
{
	@viewOutput = split('\n', $ViewOutput);
	foreach $line ( @viewOutput )
	{
		@line = $line;	
		if ( grep(/Tag:/, @line) > 0 )
		{
			chomp($line);
			@tag = split( ' ', $line );
			$ViewTag = $tag[1];
		}

		if ( grep(/View attributes:/, @line) > 0 )
		{
			# Check to see if it is a snapshot or Dynamic View
			if ( grep(/snapshot/i, @line) > 0 )
			{

				$ViewType = "SNAPSHOT";
			}
			else
			{

				$ViewType = "DYNAMIC";
			}

			# Checks to see if it is 
			if ( grep(/ucmview/i, @line) > 0 )
			{

				$VOBType = "UCM";
			}
			else
			{
				$VOBType = "NONUCM";
			}
		}
	}

	if ( $ViewTag ne "NONE" )
	{
		# This is needed if it is a Dynamic view and a NONUCM since the lsview does not list the
		# View Attributes Key in the listing
		if ( $ViewType eq "NONE" )
		{
			$ViewType = "DYNAMIC";
			$VOBType = "NONUCM";
		}
	}		
}

return $ViewTag.$ViewType.$VOBType;
}




		
			
