#!//usr/bin/perl
#**************************************
#Student name: Davina Chin Lee Yien
#Student id: 7D2A1001
#SPD 251 Assignment 2 Question 2
#**************************************
#
#initialize the value for selection.....
$selection="t";

#while true, will continue looping..
while ( true ){
	
#the menu for user to choose a,b,c,d or e
print "\n===============Menu===============\n";
print "a) Create a file\n";
print "b) Rename a file\n";
print "c) Delete a file\n";
print "d) List all files in directory\n";
print "e) Exit\n";
print "==================================\n";
print"\nEnter your selection:";

#to get user input
$selection=<STDIN>;
#cut off the new line
chomp $selection;

#when selection is equal to e or E, go to function create()
if ($selection eq "a" || $selection eq "A"){
	&create();
}
else{
	#when selection is equal to b or B, go to function rename()
	if ($selection eq "b" || $selection eq "B"){
		&rename();
	}
	else{
		#when selection is equal to c or C, go to function delete()
		if ($selection eq "c" || $selection eq "C"){
			&delete();
		}
		else{
			#when selection is equal to d or D, go to function list()
			if ($selection eq "d" || $selection eq "D"){
				&list();
			}
			else{
				#when selection is equal to e or E, go to function quit()
				if($selection eq "e" || $selection eq "E"){
					&quit();
				}
				else{
					#when selection is neither a, b, c, d or e, error message is prompt
					print "\nError:Enter a, b, c, d or e!!\n";
				}
			}
		}
	}
}
}

#************* sub create() **************************
#this function is use to create new files when the specified files does not exist
sub create{
	#ask user to specify the file name to be create
	print "\nEnter file name:";
	#input by user
	$filename=<STDIN>;
	#cut off the \n 
	chomp($filename);
	#check to see filename exist or not. if exist, error else create the file
	if ( -e $filename){
		die "Error: File already exist!!!\n";
	}
	else{
		#using the shell command to create the files
		`touch $filename`;
		print "Files $filename created.\n";
	}
}

#************** sub rename() *************************
#this function is to rename the specified file name to a new name...
sub rename{
	#to get the target files to be rename
	print "\nEnter target filename:";
	$target=<STDIN>;
	chomp($target);
	#if the files exist, then user are ask to enter a new name for that file
	if (-e $target ){
		print "\nEnter new filename:";
		$new=<STDIN>;
		chomp($new);
		#check whether the new name specify by user exist or not. if exist, error, else rename it
		if (-e $new){
			die "Error: File already exist. Cannot rename!!\n";
		}
		else{
			#rename the file
			rename($target,$new);
			print "File $target change to $new\n";	
		}
	}
	else{
		#no such files, so exit from this function
		die "Error: File does not exist!!\n";
	}
}

#*************** sub delete() ************************
#this function will delete an exist files ....
sub delete{
	#ask for the file to be deleted
	print "\nEnter files to be deleted:";
	$deletion=<STDIN>;
	chomp ($deletion);
	#check whether file exist or not
	if (-e $deletion){
		#delete the file
		unlink($deletion);
		print "Deleting files: $deletion\n";
	}
	else{
		#file does not exist, so cannot delete
		die "Error: File does not exist!!\n";
	}
	
}

#*************** sub list() **************************
#list all the files in current directory
sub list{
	print "\nFile Listing:\n";
	print "========== List ==========\n";
	#to open the directory
	#store the current directory into $direc
	$direc=`pwd`;
	#the output array has all the output from the ls command..
	@output=`ls $direc`;
	#print out each filename in the array
	foreach (@output){
		print ;
	}
	print "\n=========================\n";
	
}

#*************** sub quit() **************************
#exit from the program.........
sub quit{
	print "\nProgram exits\n";
	#program exits here
	exit ();
}

