#!/usr/bin/perl -w
#
# TestPlan: NFS Complex Test 
#
# Written by :  AppLabs Technologies
#
# Date : 05/10/05
#
# Description :This Script creates specified number of sequential symlinks to a file in different directories on NFS server.
#
# PASS : Given number of sequential symlinks to a file should get created in different directories, FAIL otherwise.
#

use Getopt::Long;
use global_library;
use strict;

my $timestorun = 1; #Default value : Number of times a script should run.
my $createfilename = "/TestFile"; #Default Value : Name of the file to be created.
my $workingdirectory = "/TC_C_20_2"; 
my $noofsymlinks = 50; #Default value : Number of symlinks to be created
my $directoryformat = "/dir";
my $linkformat = "/filelink";

&GetOptions('help' => \my $help, 'fsmount=s' => \my $localmountpoint, 'linkscount=i' => \$noofsymlinks, ,'debug=i'=>\$global_library::Debug,'timestorun=i' =>\$timestorun);

if($help)
{
        print "\nThis Script creates specified number of sequential symlinks to a file in different directories on NFS server.\n";
        print "\nExample: #fs_nsequentialfilesymlinks.pl --fsmount <mount point> --linkscount <number> --timestorun <number>\n";
        exit;
}

print "\nThis script is running on $^O Operating System.\n";
$localmountpoint=&global_library::RemoveSlash($localmountpoint);

if($createfilename ne "/TestFile")
{ 
	$createfilename = "/" . "$createfilename";
}

my $testcaseid="TC_C_20_2";
my $testscriptname = "fs_nsequentialfilesymlinks.pl";
my $expectedresult="Given number of sequential symlinks to a file should be created in different directories.\n";
print "TEST CASE ID: $testcaseid\n";
print "TEST SCRIPT NAME: $testscriptname\n";
print "EXPECTED RESULT: $expectedresult\n\n";
if(-e $localmountpoint)
{
	my $execctr;
	for(my $execctr=1 ; $execctr<=$timestorun; $execctr++)
	{
		print "Script run count : $execctr \n";
		if(!&global_library::CreateWorkingDirectory("$localmountpoint$workingdirectory"))
		{
			&global_library::WriteStatus("E",$testscriptname);
			exit 2;	
		}
		if(!&global_library::CreateFile("$localmountpoint$workingdirectory$createfilename"))	
		{
			&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
			&global_library::WriteStatus("E",$testscriptname);
			exit 2;		
		}
		if(!&global_library::VerifyFileDirCreation("$localmountpoint$workingdirectory$createfilename"))
    		{	
			&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");		
			&global_library::WriteStatus("E",$testscriptname);	
			exit 2;		
		}
		my $ChainSource;
		my $ChainTail = "$localmountpoint$workingdirectory$createfilename";
		for(my $lfilectr=1;$lfilectr<=$noofsymlinks;$lfilectr++)
		{
			print "\n";
			$ChainSource = $ChainTail;
			if(!&global_library::CreateDirectory("$localmountpoint$workingdirectory$directoryformat$lfilectr"))
			{
				&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
				&global_library::WriteStatus("E",$testscriptname);
				exit 2;
			}
			$ChainTail = "$localmountpoint$workingdirectory$directoryformat$lfilectr$linkformat$lfilectr";
			if(!&global_library::CreateLink("$ChainSource","$ChainTail"))
			{
				&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
				&global_library::WriteStatus("F",$testscriptname);
				exit 1;
			}
			if(!&global_library::VerifyLink("$ChainSource","$ChainTail"))
			{
				&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
				&global_library::WriteStatus("F",$testscriptname);
				exit 1;
			}
		}
		if(!&global_library::WriteFile("$localmountpoint$workingdirectory$createfilename"))	
		{
			&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
			&global_library::WriteStatus("E",$testscriptname);
			exit 2;		
		}
		for(my $lfilectr2=$noofsymlinks;$lfilectr2>=1;$lfilectr2--)
		{
			&global_library::PrintStatus("Checking written content in $createfilename using link : $linkformat$lfilectr2");
			if(my $x = &global_library::GetFileSize("$localmountpoint$workingdirectory$directoryformat$lfilectr2$linkformat$lfilectr2"))
    			{
    				if($lfilectr2>1 && $x > 0)
    				{	
    					&global_library::PrintStatus("Test passed at $lfilectr2 levels of chained links.");
					&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
					&global_library::WriteStatus("P",$testscriptname);	
					exit 0;		
				}
			}
		}		
	}
	&global_library::RemoveWorkingDirectory("$localmountpoint$workingdirectory");
	&global_library::WriteStatus("F",$testscriptname);
	exit 0;
}
else
{
	&global_library::PrintStatus("Shared Resource not found. Execution Terminated");
	&global_library::WriteStatus("E",$testscriptname);
	exit 2;
}
