package eu.rethink.globalregistry.configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Configuration
{
	private static Configuration	instance			= null;
	
	private Properties				prop;
	
	// fixed info
	private static final String		versionName			= "0.2.5";
	private static final int		versionNumber		= 1297;
	private static final String		versionCode			= "";
	private static final String		versionDate			= "2016-10-24";
	private static final String		productName			= "reTHINK Global Registry";
	private static final String		productNameShort	= "gReg";
	private static String			filename			= "greg.config";
	
	// cvars
	private int						portDHT;
	private int						portServer;
	private String[]				knownHosts;
	private String					networkInterface;
	private String					logPath;
	
	// private Scanner scan;
	
	private Configuration()
	{
		prop = new Properties();
	}
	
	public static Configuration getInstance()
	{
		if (instance == null)
			instance = new Configuration();
		return instance;
	}
	
	// /////////////////////////////////////////////////////////////////////////////////////////////
	// cvar getters
	// /////////////////////////////////////////////////////////////////////////////////////////////
	
	public int getPortDHT()
	{
		return 5001;
	}
	
	public int getPortServer()
	{
		return this.portServer;
	}
	
	public String[] getKnownHosts()
	{
		return this.knownHosts;
	}
	
	public String getNetworkInterface()
	{
		return this.networkInterface;
	}
	
	public String getLogPath()
	{
		return logPath;
	}
	
	public String getConfigFilename()
	{
		return filename;
	}
	
	public void setLogPath(String logPath)
	{
		this.logPath = logPath;
	}
	
	// /////////////////////////////////////////////////////////////////////////////////////////////
	// config file handling
	// /////////////////////////////////////////////////////////////////////////////////////////////
	
	// // TODO: validate config data
	
	/**
	 * Loads configuration file.
	 */
	public void loadConfigurationFile()
	{
		InputStream input = null;
		
		try
		{
			input = new FileInputStream(filename);
			
			prop.load(input);
			
			this.portDHT = 5001;
			this.portServer = Integer.parseInt(prop.getProperty("port_server"));
			String hosts = prop.getProperty("known_hosts");
			this.knownHosts = hosts.split(";");
			this.networkInterface = prop.getProperty("network_interface");
			this.setLogPath(prop.getProperty("log_path"));
			
			input.close();
		}
		catch (IOException e)
		{
			// file does not exist. using default hardcoded values
			
			this.portDHT = 5001;
			this.portServer = 5002;
			this.knownHosts = new String[0];
			this.networkInterface = "eth0";
			this.logPath = "/usr/local/gReg/log";
			
			//e.printStackTrace();
		}
		
	}
	
	/**
	 * Checks if a config file is available.
	 * 
	 * @return boolean
	 */
	public static boolean propFileExists(String path)
	{
		filename = path;
		InputStream input = null;
		try
		{
			input = new FileInputStream(filename);
		}
		catch (FileNotFoundException e)
		{
			System.out.println("no config file found");
			return false;
		}
		finally
		{
			if (input != null)
			{
				try
				{
					input.close();
					System.out.println("config file found");
					return true;
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	
	// /////////////////////////////////////////////////////////////////////////////////////////////
	// static final getters
	// /////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * retrieves the product name as a String
	 * 
	 * @return String
	 */
	public String getProductName()
	{
		return productName;
	}
	
	/**
	 * retrieves the product name short as a String
	 * 
	 * @return String
	 */
	public String getProductNameShort()
	{
		return productNameShort;
	}
	
	/**
	 * retrieves the version number as a String, e.g. "0.1.2"
	 * 
	 * @return String
	 */
	public String getVersionName()
	{
		return versionName;
	}
	
	/**
	 * retrieves the date of the build, e.g. 2014-08-22
	 * 
	 * @return String
	 */
	public String getVersionDate()
	{
		return versionDate;
	}
	
	public int getVersionNumber()
	{
		return versionNumber;
	}
	
	public String getVersionCode()
	{
		return versionCode;
	}
}
@@ -369,16 +369,17 @@
 nName%09%09%09
+%09
 = %220.2.5
@@ -427,14 +427,15 @@
 er%09%09
+%09
 = 1
-297
+301
 ;%0A%09p
@@ -476,16 +476,17 @@
 nCode%09%09%09
+%09
 = %22%22;%0A%09p
@@ -527,16 +527,17 @@
 nDate%09%09%09
+%09
 = %222016-
@@ -588,16 +588,17 @@
 tName%09%09%09
+%09
 = %22reTHI
@@ -665,16 +665,17 @@
 meShort%09
+%09
 = %22gReg%22
@@ -712,16 +712,17 @@
 ename%09%09%09
+%09
 = %22greg.
@@ -730,16 +730,166 @@
 onfig%22;%0A
+%09private static final int%09%09versionDatasetSchema%09= 1;%0A%09private static final int%09%09versionRESTAPI%09%09%09= 1;%0A%09private static final int%09%09versionDHTAPI%09%09%09= 1;%0A
 %09%0A%09// cv
@@ -4525,8 +4525,9 @@
 de;%0A%09%7D%0A%7D
+%0A

Update Configuration.java