package nxm.sys.lib;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

/** Imports an environment variables from the OS shell.

    @author  Jay Hallam, J. Czechowski IV, Neon Ngo
    @since   NeXtMidas 2.1.0
*/
public class Import {

  /** This constructor was automatically provided by Java,
   *  but it is unneeded as all the methods are static methods.
   *  @deprecated now that Javadocs are required, it is temporarily
   *              being explicitely provided but deprecated
   */
  @Deprecated
  public Import () {}

  /** Gets a table containing all of the current environment variables. This method always creates
      a new table with a copy of the current environment variables.
      @return table containing all of the current environment variables.
   */
  public static Table getEnvTable () {
    Table      tbl = new nxm.sys.lib.Table();
    Properties env = getProperties();
    for (Enumeration<?> e = env.propertyNames(); e.hasMoreElements();) {
      String name = (String)e.nextElement();
      if (name.indexOf(".") >= 0) {
        System.out.println("ENV VAR WITH DOT => Name "+name);
      } else {
        tbl.put(name,env.getProperty(name));
      }
    }

    return tbl;
  }

  /** Gets the value of an OS environment variable.
      @param key The name of the variable.
      @return The value of the variable or null if not found.
      @see System#getenv(String)
  */
  public static String getEnv (String key) {
    return System.getenv(key);
  }

  /** Gets the value of an OS environment variable ignoring SecurityException by
      returning the specified default value.
      @param key The name of the variable.
      @param defVal The value to return if variable is not found of if in a restricted security environment.
      @return The value of the variable or defVal if not found.
      @see System#getenv(String)
      @since NeXtMidas 3.1.0
   */
  public static String getEnv (String key, String defVal) {
    String value = null;
    try {
      value = System.getenv(key);
    } catch (SecurityException se) {
      // ignore, will use defVal
    }
    if (value == null) value = defVal; // not found or SecurityException, so use defVal
    return value;
  }

  /** Get the environment as a Properties object
      @return a Properties object
      @see System#getenv()
  */
  private static Properties getProperties () {

    Properties env = new Properties();
    try {
      Map<String,String> envMap = System.getenv();
      for (Map.Entry<String,String> entry: envMap.entrySet()) {
        env.setProperty(entry.getKey(), entry.getValue());
      }
    } catch (Exception e) {
      throw new MidasException("Unable to get environment variables",e);
    }
    return env;
  }

  /** Expand OS Environment variables (e.g. $ENVNAME or ${ENVNAME}) from
      specified text string. This will ignore any broken/mismatch open/close
      braces and continue.
      @param text input string to expand OS environment variables
      @return text string with OS environment variables expanded
      @since NeXtMidas 3.1.2
      @see StringUtil#expandVariables(String, Map, int)
   */
  public static String expandEnvVars (String text) {
    return StringUtil.expandVariables(text, System.getenv(), StringUtil.NO_ERROR);
  }
}
