package org.plenix.jsp;

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

public class ResourceManager
{
  private static String defaultVersion = null;
  private static String defaultLanguage = null;

  private static String stageDirectory = null;
  private static String configurationDirectory = null;

  static
  {
    try { setDefaultVersion("0.92"); } catch (Exception e) { }
    try { setDefaultLanguage("java"); } catch (Exception e) { }
    try { setStageDirectory(System.getProperty("polyjsp.stage")); } catch (Exception e) { }
    try { setConfigurationDirectory(System.getProperty("polyjsp.configuration")); } catch (Exception e) { }
  }

  public static void setDefaultVersion(String name)
    throws JspException
  {
    if (name != null)
    {
      defaultVersion = name;
    }

    putSystemProperty("polyjsp.default.version", defaultVersion);
  }

  public static String getDefaultVersion()
    throws JspException
  {
    return defaultVersion;
  }

  public static void setDefaultLanguage(String name)
    throws JspException
  {
    if (name != null)
    {
      defaultLanguage = name;
    }

    putSystemProperty("polyjsp.default.language", defaultLanguage);
  }

  public static String getDefaultLanguage()
    throws JspException
  {
    return defaultLanguage;
  }

  public static void setConfigurationDirectory(String name)
    throws JspException
  {
    if (name == null)
    {
      throw new JspException("Invalid null configuration directory");
    }

    configurationDirectory = name;
    File directoryFile = new File(configurationDirectory);
    if (!(directoryFile.isDirectory() && directoryFile.canRead()))
    {
      throw new JspException("Invalid configuration directory '" + configurationDirectory  + "'");
    }

    putSystemProperty("FESI.path", configurationDirectory);
    putSystemProperty("polyjsp.configuration", configurationDirectory);
  }

  public static String getConfigurationDirectory()
    throws JspException
  {
    if (configurationDirectory == null)
    {
      throw new JspException("Configuration directory has not been specified");
    }

    return configurationDirectory;
  }

  public static void setStageDirectory(String name)
    throws JspException
  {
    if (name == null)
    {
      throw new JspException("Invalid null stage directory");
    }

    stageDirectory = name;
    File directoryFile = new File(stageDirectory);
    if (!(directoryFile.isDirectory() && directoryFile.canWrite()))
    {
      throw new JspException("Invalid stage directory '" + stageDirectory  + "'");
    }

    putSystemProperty("polyjsp.stage", stageDirectory);

    String classPath = System.getProperty("java.classpath");
    putSystemProperty("java.classpath", classPath + File.pathSeparator + stageDirectory);
  }

  public static String getStageDirectory()
    throws JspException
  {
    if (stageDirectory == null)
    {
      throw new JspException("Stage directory has not been specified");
    }

    return stageDirectory;
  }

  public static String getResourceName(String fileName)
  {
    return configurationDirectory + File.separator + fileName;
  }

  public static InputStream getResourceStream(String fileName)
    throws IOException
  {
    return new FileInputStream(getResourceName(fileName));
  }

  public static String getResourceContents(String fileName)
    throws IOException
  {
    return Util.getFileContents(getResourceName(fileName));
  }

  private static void putSystemProperty(String name, String value)
  {
    Properties properties = System.getProperties();
    properties.put(name, value);
    System.setProperties(properties);
  }
}
