/*
 *    Copyright 2011 Ken Gilmer
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.knapsack;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Class for knapsack and framework configuration.  
 * It also handles creating the initial default configuration if it doesn't exist upon start.
 * 
 * @author kgilmer
 *
 */
public class Config extends Properties {
	private static final long serialVersionUID = -5479563157788056552L;
	private static final String CRLF = System.getProperty("line.separator");

	/**
	 * Optional system property that defines root directory where knapsack runs.
	 */
	public static final String CONFIG_KEY_ROOT_DIR = "org.knapsack.rootDir";

	/**
	 * Emit log events to stdout
	 */
	public static final String CONFIG_KEY_LOG_STDOUT = "org.knapsack.log.stdout";

	/**
	 * Name of knapsack's configuration file
	 */
	public static final String CONFIGURATION_FILENAME = "felix.conf";
	public static final String CONFIGURATION_RESOURCE_FILENAME = "/" + CONFIGURATION_FILENAME;

	public static final String CONFIG_KEY_OVERWRITE_CONFIGADMIN = "org.knapsack.configAdmin.overwrite";
	
	/**
	 * If true the internal Logger will be started with the framework.
	 */
	public static final String CONFIG_KEY_BUILTIN_LOGGER = "org.knapsack.builtin.logger";

	/**
	 * If true the internal ConfigAdmin will be started with the framework.
	 */
	public static final String CONFIG_KEY_BUILTIN_CONFIGADMIN = "org.knapsack.builtin.configAdmin";
	
	/**
	 * A list of directory names which hold bundles that should be installed and optionally started.
	 */
	public static final String CONFIG_KEY_BUNDLE_DIRS = "org.knapsack.bundleDirs";
	
	/**
	 * Filename for bundle directory
	 */
	public static final String DEFAULT_BUNDLE_DIRECTORY = "bundle";
	
	/**
	 * Only one instance of Config per runtime.
	 */
	private static Config ref;
	
	public final static String BASE_SCRIPT_FILENAME = ".knapsack-command.sh";
	public static final String SCRIPT_DIRECTORY_NAME = "bin";

	private final File baseDirectory;
	
	public static Config getRef() throws IOException {
		if (ref == null)
			throw new IOException("Config needs to be instantiated by bootstrap.");
		
		return ref;
	}

	/**
	 * Initialize state
	 * 
	 * @throws IOException
	 * @throws InterruptedException 
	 */
	protected Config(File baseDirectory) throws IOException {		
		this.baseDirectory = baseDirectory;
		load(new FileInputStream(getConfigFile()));
		
		//Specify the root of the knapsack install if not explicitly defined.
		if (!this.containsKey(CONFIG_KEY_ROOT_DIR))
			this.put(CONFIG_KEY_ROOT_DIR, baseDirectory);
		
		//Specify a default bundle directory if not explicitly defined.
		if (!this.containsKey(CONFIG_KEY_BUNDLE_DIRS))
			this.put(CONFIG_KEY_BUNDLE_DIRS, DEFAULT_BUNDLE_DIRECTORY);
	}
	
	/**
	 * Get the knapsack configuration file.
	 * 
	 * @return
	 * @throws IOException
	 */
	private File getConfigFile() throws IOException {	
		return new File(baseDirectory, CONFIGURATION_FILENAME);
	}

	/**
	 * Parse value as boolean.
	 * 
	 * @param key
	 * @return true if key value is 'true', false otherwise.
	 */
	public boolean getBoolean(String key) {
		if (this.containsKey(key))
			return Boolean.parseBoolean(this.get(key).toString());
		
		return false;
	}

	/**
	 * Convenience method to return value as a string or null if key doesn't exist.
	 * @param key
	 * @return
	 */
	public String getString(String key) {	
		if (this.containsKey(key))
			return this.get(key).toString();
		
		return null;
	}
}
@@ -2926,17 +2926,30 @@
 ption %7B%09
-%09
+%0A%09%09ref = this;
 %0A%09%09this.

fix static initialization of Configuration