#!/usr/bin/perl  -w
use AFS::VOS ; 
use AFS::VLDB ; 
use AFS::FS qw(
		isafs lsmount mkmount rmmount
	       ); 
use strict ; 
# read all the upi's from a file 
my @list=`/bin/cat /home/bobby/scripts/duplicate/test` ; 
foreach  (@list)
{
	my $entry=$_ ;
	chomp($entry);
		print "-----------------------------------\n";
	print "Processing user [$entry]\n";
	my $afspath="/afs/.ec.auckland.ac.nz/users/".(split //)[0]."/".(split //)[1]."/$entry";
	print "Getting mount point for $afspath\n"; #DEBUG
	
	my $volume=lsmount("$afspath");	
	if (defined($volume)) 
	{
         	print "The mounted volume [$volume]  \n"; # DEBUG
		my $mounted_volume=&chimp($volume) ;
		print "for user  [$entry] = the mounted volume is [$mounted_volume]\n ";
		my $good_volume="user.$entry"; 
		print "Comparing good volume=[$good_volume] with mounted_volume=[$mounted_volume]\n" ;
		if ($good_volume ne $mounted_volume)
			{
				print "I am sleeping for 5 secs   Zzzz...\n" ;
				sleep 10 ; 
				my $vos= AFS::VOS->new ; 
				print "{$good_volume}\t{$mounted_volume}\tThe right volume is not mounted\n ";
				my $delvol="${good_volume}.todelete";
				print "delvol= $delvol\n";
				print "Iam renaming [$good_volume] to [$delvol] to be deleted afterwards\n";
				my  $vosrenameres=$vos->rename($good_volume, $delvol); # COMMAND 
				print "vos rename result= $vosrenameres\n";
				
				print "Iam renaming [$mounted_volume] to [$good_volume]\n";
				my $res=$vos->rename($mounted_volume, $good_volume); #COMMAND 
				print "Renamed volume $mounted_volume  to $good_volume\n" if $res;
				print "Now unmounting [$afspath]\n";
			        my $UmountRes=rmmount($afspath); #COMMAND
				print "result of  rmmount = $UmountRes\n";
			        print "Now mounting the right volume\nfs mkmount $afspath $good_volume\n";	
				my $mkmountres=mkmount($afspath,$good_volume);
				print "result of mkmount = $mkmountres \n";
				$vos->DESTROY ; 
			}	else {
					print "The right volume is already mounted :) not doing anything \n";
				     }

				
			
        } else {
		print "the volume is not mounted= $entry \n";  
		}

}





sub chimp 
	{
	my $m_v=$_[0];
	if ($m_v =~ /^#(user\S+)/ || $m_v =~ /^%(user\S+)/  || $m_v =~ /^%ec.auckland.ac.nz:(user\S+)/)
		{
			return "$1"  ;
		}
	}	
