// COM3336 Operating Systems
// Homework 1 part 2
// John Sung
// Started: 10/11/1999
//

import ComplexFile;


// this is the "shell system". This class stores all of the
// file structure and the current working directory. It also
// has interface to execute the commands defined by the EBNF
// form given in the homework part 2 specification.
public class ShellSystem
{
	 ///////////////////////////////////////////
	 // private variables

	 // the actual current working directory
	 ComplexFile CurrentWorkingDir;
	 
	 // constructor for this class
	 public ShellSystem()
	 {
		  super();
	
		  CurrentWorkingDir = new ComplexFile("", null);
	
		  
	 }

	 // make a new directory
	 synchronized public void makeDir(String dirName)
	 {
		  CurrentWorkingDir.createDir(dirName, System.out); 
	 }
	 
	 
	 // change directory up
	 synchronized public void changeDirUp()
	 {
		  // get the parent
		  ComplexFile parentDir = (ComplexFile)CurrentWorkingDir.getParent();

		  // if the parentDir is not null then set it as the
		  // current working dir.
		  if (parentDir != null)
				CurrentWorkingDir = parentDir;
	 }

	 // change diectory down
	 synchronized public void changeDirDown(String dirName)
	 {
		  // attempt to change the directory
		  ComplexFile newCWD = CurrentWorkingDir.findSubDir(dirName, System.out);
		  
		  
		  // if found then change the CWD
		  if (newCWD != null)
				CurrentWorkingDir = newCWD;

		  // else output some error message
		  else
				System.err.println("Directory entry " + dirName + " not found in the current working directory");
	 }
	 
	 
	 // recursive copy the current directory
	 synchronized public void recursiveCopy()
	 {
		  // recursively copy the directory
		  CurrentWorkingDir.
				recursiveCopyDirEntries((ComplexFile)CurrentWorkingDir.getParent());

	 }

	 // disk usage
	 public void diskUsage()
	 {
		  CurrentWorkingDir.recursivePrintDirectoryList(".", System.out);  
	 }

	 // disk usageall
	 public void diskUsageAll()
	 {
		  CurrentWorkingDir.recursivePrintAllList(".", System.out);  
	 }

	 // find
	 public void find(String fileName)
	 {
		  CurrentWorkingDir.recursiveFindAndPrintDirList(".", fileName, System.out);
	 }


	 // create a file
	 synchronized public void createFile(String fileName)
	 {
		  CurrentWorkingDir.createFile(fileName, System.out);
	 }
	 
	 // create a symbolic link
	 synchronized public void createSymbolicLink(String destName,
																String linkName)
																
	 {
		  CurrentWorkingDir.createSymbolicLink(destName,linkName);
	 }

	 // remove a file
	 synchronized public void removeFile(String fileName)
	 {
		  boolean isRemoved = CurrentWorkingDir.removeFileFromDir(fileName,
																					 System.out);
		  
		  if (!isRemoved)
				System.out.println("Failed to remove file " + fileName + ".");
	 }

	 // remove a dir
	 synchronized public void removeDir(String dirName)
	 {
		  boolean isRemoved = CurrentWorkingDir.removeDirFromDir(dirName,
																					System.out);

		  if (!isRemoved)
				System.out.println("Failed to remove directory " + dirName + ".");
	 }

	 
	 
}