package org.biomoby.registry.properties;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * This class incorporates the logic needed to obtain environment variables from
 * UNIX and UNIX like machines.
 * 
 * @author Eddie created Nov 28, 2005
 */
@SuppressWarnings("unchecked")
public class UNIX implements Environment {
	/**
	 * the singleton
	 */
	public static final UNIX os = new UNIX();

	// empty constructor
	private UNIX() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.biomoby.registry.properties.Environment#getEnv()
	 */
	public Map getEnv() {
		HashMap map = new HashMap();
		Process p;
		try {
			p = Runtime.getRuntime().exec("env");
			BufferedReader br = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			String line = "";
			while ((line = br.readLine()) != null) {
				String[] contents = line.split("=");
				if (contents.length != 2)
					continue; // continue or break?
				String key = contents[0];
				String value = contents[1];
				map.put(key, value);
			}
			p.waitFor();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
		}
		return map;
	}

	/**
	 * Register the terms that System.getProperty("os.name") returns with this
	 * os
	 * 
	 */
	public static final void register() {
		RegistryOS.register("SunOS", os);
		RegistryOS.register("Solaris", os);
		RegistryOS.register("AIX", os); // ?
		RegistryOS.register("FreeBSD", os);// ?
		RegistryOS.register("HP-UX", os); // ?
		RegistryOS.register("Linux", os);
		RegistryOS.register("UNIX", os); // ?
	}

}
