package com.sun.solarisinstall.panels;

import java.io.*;
import java.lang.*;
//
// FS Object
//
public class SolarisFSObj {

	private String 	FSname = null;		// Has a name, e.g. ROOT
	private String  FSlabel = null;		// Has a label, e.g. "/"
	private int	ReqSize = 0;		// Required disk size in KB
	private int	AllocateSize = 0;	// Allocated disk size in MB
	private	final int Overhead = 124;	// 24 % Overhead required
	
	// Constructor
	public SolarisFSObj(String name, String label) {
		FSname = name;
		FSlabel = label;
	}

	public void add(int size) {
		ReqSize = 0;
		AllocateSize = size;
	}

	public void delete() {
		ReqSize = 0;
		AllocateSize = 0;
	}

	public boolean isAllocated() {
		if (AllocateSize > 0) 
			return true;
		else 
			return false;
	}

	public String getName() {
		return FSname;
	}

	public String getLabel() {
		return FSlabel;
	}

   //if we failed get parent, it might be beacuse we are a PC, and 
  //use a different File.getparent() for PC operating systems.  In
  //this case try to get the parent by parsing the text
  public String getParent() {
	  
    if ( FSlabel.compareTo("swap") == 0 ) {
      return null;
    }
    
    File f = new File(FSlabel);
 
    //this code is taken from the JDK source's File.getParent()
    //method, and is used to find the parent of a Unix directory
    //on PC's or whatever
    int index = FSlabel.lastIndexOf('/'); //Unix specificity
    if (index < 0) //no parent found
      return "/";
    if (FSlabel.indexOf('/') != index)
      return FSlabel.substring(0, index);
    if (index < FSlabel.length() - 1)
      return FSlabel.substring(0, index + 1);
    return null;
    
    
    /* the old code
       File f = new File(FSlabel);
       if ( f.getParent() == null) 
       return "/";
       else
       {
       System.out.println("Parent is: "+f.getParent());
       return f.getParent();
       }
       */
  }
  
	
	public void addReqSize(int sz) {
		ReqSize += sz;
	}

	public void delReqSize(int sz) {
		ReqSize -= sz;
	}

	public void setReqSize(int size) {
		ReqSize = size;
	}

	public int getReqSize() {
	        // If ReqSize is 0, don't do math which in some cases changes
	        // value, giving bogus numbers.
	        if (ReqSize == 0) {
		        return ReqSize;
		}
		if (FSlabel.compareTo("swap") == 0) {
			return (ReqSize / 1024);
		} 
		else
		if (FSlabel.compareTo("/export/swap") == 0) {
			return (((ReqSize * 120) + 102399) / 102400);
		}
		else
		if (FSlabel.compareTo("/export/root") == 0 || 
		    FSlabel.compareTo("/export") == 0  ) {
			return ((((ReqSize+(8 * 1024)) * 130) + 102399) / 102400);
		} else  {
			return (((ReqSize * Overhead) + 102399) / 102400);
		}
	}

	public int getReqKB() {
	        // If ReqSize is 0, don't do math which in some cases changes
	        // value, giving bogus numbers.
	        if (ReqSize == 0) {
		        return ReqSize;
		}
		if (FSlabel.compareTo("swap") == 0) {
			return (ReqSize);
		} 
		else
		if (FSlabel.compareTo("/export/swap") == 0) {
			return ((ReqSize * 120) / 100);
		}
		else
		if (FSlabel.compareTo("/export/root") == 0 || 
		    FSlabel.compareTo("/export") == 0  ) {
			return (((ReqSize + 8 ) * 130) / 100);
		} else  {
			return (ReqSize);
		}
	}

	public void setAllocateSize(int size) {
		AllocateSize = size;
	}

	public int getAllocateSize() {
		return AllocateSize;
	}

	public SolarisWebStartError validate() {
		SolarisWebStartError errorInfo = new SolarisWebStartError();
		int req = getReqSize();
		if ( req > AllocateSize) {
			errorInfo.setErrorCode (SolarisWebStartError.BAD_FS_SIZE);
			errorInfo.appendInfo((Object) FSlabel);
			errorInfo.appendInfo((Object) Integer.toString(req));
			errorInfo.appendInfo((Object) Integer.toString(AllocateSize));
		}
		return errorInfo;
	}
}
