package com.rho;

import java.util.Enumeration;
import java.io.IOException;

public class RhoConf {
    String      m_strConfFilePath = "";
    String      m_strRhoRootPath = "";
    private java.util.Hashtable m_mapValues = new java.util.Hashtable();
    
    private static RhoConf m_Instance;
	private static String CONF_FILENAME = "apps/rhoconfig.txt";
    
    public static RhoConf getInstance(){return m_Instance;}
    
	void setConfFilePath(String path){ m_strConfFilePath = path; }
	String getConfFilePath(){ return m_strConfFilePath; }
    
	void setRhoRootPath(String szRootPath){ m_strRhoRootPath = szRootPath;}
	String getRhoRootPath(){ return m_strRhoRootPath;}
	
    public void saveToFile(){
        String strData = saveToString();
    	SimpleFile oFile = null;

    	try{
	        oFile = RhoClassFactory.createFile();
	        oFile.delete(getConfFilePath());
	        
        	oFile.open( getConfFilePath(), false, false);
	        oFile.write( 0, strData.getBytes() );
	        oFile.close();
    	}catch(Exception exc){
    		if ( oFile != null )
    			try{ oFile.close(); }catch(IOException exc2){}
    	}
        
    }
    
    void loadFromFile(){
    	SimpleFile oFile = null;
    	try{
	        oFile = RhoClassFactory.createFile();
	        oFile.open( getConfFilePath(), true, false);
	        
	        if ( oFile.isOpened() ){
	            String strSettings = oFile.readString();
	            oFile.close();
	            loadFromString( strSettings );
	        }
	        
    	}catch(Exception exc){
    		if ( oFile != null )
    			try{ oFile.close(); }catch(IOException exc2){}
    	}
    }
    
    void loadFromString(String szSettings){
		Tokenizer stringtokenizer = new Tokenizer(szSettings, "\n");
		while (stringtokenizer.hasMoreTokens()) {
			String tok = stringtokenizer.nextToken();
			tok = tok.trim();
			if (tok.length() == 0) {
				continue;
			}
			
			if ( tok.length() > 0 && tok.charAt(0) == '#' )
				continue;
			
			int i = tok.indexOf('=');
			String name;
			String value;
			if (i > 0) {
				name = tok.substring(0, i);
				value = tok.substring(i + 1);
			} else {
				name = tok;
				value = "";
			}
			name = name.trim();
			value = value.trim();
			
			if (value.startsWith("\'") && value.endsWith("\'")) {
				value = value.substring(1,value.length()-1);
			}
				
			setPropertyByName(name,value);
		}
	}

    void setPropertyByName(String name, String value ){
    	m_mapValues.put(name,value);
    }
	
    String saveToString(){
    	String strData = "";
    	Enumeration enValues = m_mapValues.elements();
    	Enumeration enKeys = m_mapValues.keys();
		while (enValues.hasMoreElements()) {
			String key = (String)enKeys.nextElement();
			String value = (String)enValues.nextElement();
			
            strData += key;
            strData += "=\'";
            strData += value;
            strData += "\'\n";
		}
		
    	return strData;
    }

    public String getString(String szName){
    	String value = (String)m_mapValues.get(szName);
    	if ( value != null )
    		return value;

        return "";
    }

    public int getInt(String szName){
    	String value = (String)m_mapValues.get(szName);
    	if ( value != null && value.length() > 0 )
    		return Integer.parseInt(value);

        return 0;
    }

    public boolean  getBool(String szName){
        return getInt(szName) == 0 ? false : true;
    }

    public void   setString(String szName, String str){
    	m_mapValues.put(szName,str);
    }

    public void   setInt(String szName, int nVal){
    	m_mapValues.put(szName,Integer.toString(nVal));
    }

    public void   setBool(String szName, boolean bVal){
        setInt(szName, bVal ? 1 : 0 );
    }

    boolean  isExist(String szName){
    	return m_mapValues.containsKey(szName);
    }
    
    public static void InitRhoConf(){
        m_Instance = new RhoConf();
    	
    	String szRootPath = "";
    	try{
    		szRootPath = RhoClassFactory.createFile().getDirPath("");
    		//TODO: implement recursive dir creation
    		RhoClassFactory.createFile().getDirPath("apps");
    	}catch(Exception exc){}

    	
    	m_Instance.setConfFilePath(szRootPath + CONF_FILENAME);
    	m_Instance.setRhoRootPath(szRootPath);
    }

    public void loadConf(){
    	loadFromConfFromJar();
    	loadFromFile();
    }
    
    private void loadFromConfFromJar()
    {
		java.io.InputStream fstream = null;
		try {
			fstream = RhoClassFactory.createFile().getResourceAsStream(getClass(),
				 "/" + CONF_FILENAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if ( fstream == null )
			return;
		 
		try{
			byte[] data = new byte[fstream.available()];
			int len = fstream.read(data);
			if ( len == 0 )
				return;
			
			String strSettings = new String(data,0,len);
			loadFromString(strSettings);
		}catch(java.io.IOException exc){
			 
		}
   }
    
}

@@ -4258,32 +4258,24 @@
    %09loadFrom
-ConfFrom
 Jar();%0A    %09
@@ -4286,24 +4286,44 @@
 FromFile();%0A
+    %09loadFromJad();%0A
     %7D%0A    %0A 
@@ -4350,16 +4350,8 @@
 From
-ConfFrom
 Jar(
@@ -4859,10 +4859,445 @@
  %7D%0A    %0A
+   void loadFromJad()%0A   %7B%0A%09%09try%7B%0A%09%09%09IRhoRubyHelper systemInfo = RhoClassFactory.createRhoRubyHelper();%0A%09%09%09Enumeration keysEnum = m_mapValues.keys();%0A%09%09%09while( keysEnum.hasMoreElements() )%0A%09%09%09%7B%0A%09%09%09%09String name = (String)keysEnum.nextElement(); %0A%09%09%09%09String strValue = systemInfo.getAppProperty(name);%0A%09%09%09%09if ( strValue != null && strValue.length() %3E 0 )%0A%09%09%09%09%09m_mapValues.put(name, strValue);%0A%09%09%09%7D%0A%09%09%7Dcatch(Exception exc)%7B%0A%09%09%09 %0A%09%09%7D%0A   %7D%0A
 %7D%0A

Fix load from jad