package com.sap.prd.mobile.ios.mios;

/*
 * #%L
 * Xcode Maven Plugin
 * %%
 * Copyright (C) 2012 SAP AG
 * %%
 * 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.
 * #L%
 */

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

import org.apache.commons.io.IOUtils;
import org.apache.maven.project.MavenProject;

public class EffectiveBuildSettings
{
  public static final String PRODUCT_NAME = "PRODUCT_NAME";
  public static final String INFO_PLIST_FILE = "INFOPLIST_FILE";
  public static final String GCC_GENERATE_DEBUGGING_SYMBOLS = "GCC_GENERATE_DEBUGGING_SYMBOLS";
  public static final String CODE_SIGN_IDENTITY = "CODE_SIGN_IDENTITY";
  public static final String CODESIGNING_FOLDER_PATH = "CODESIGNING_FOLDER_PATH";
  public static final String INFOPLIST_FILE = "INFOPLIST_FILE";
  public static final String PUBLIC_HEADERS_FOLDER_PATH = "PUBLIC_HEADERS_FOLDER_PATH";
  public static final String BUILT_PRODUCTS_DIR = "BUILT_PRODUCTS_DIR";
  
  
  private Properties properties;

  public static String getInfoPListFile(MavenProject project, String configuration, String sdk)
  {
    EffectiveBuildSettings settings = new EffectiveBuildSettings(project, configuration, sdk);
    return settings.getBuildSetting(INFO_PLIST_FILE);
  }

  
  public static String getProductName(MavenProject project, String configuration, String sdk)
  {
    EffectiveBuildSettings settings = new EffectiveBuildSettings(project, configuration, sdk);
    return settings.getBuildSetting(PRODUCT_NAME);
  }
  
  public static File getBuildSettingsFile(MavenProject project, String configuration, String sdk)
  {
    return getBuildSettingsFile(project.getBuild().getDirectory(), configuration, sdk);
  }

  public static File getBuildSettingsFile(String directory, String configuration, String sdk)
  {
    return new File(directory, getBuildSettingsFileName(configuration, sdk));
  }

  public static String getBuildSettingsFileName(String configuration, String sdk)
  {
    return "build-settings" + "-" + configuration + "-" + sdk + ".properties";
  }

  
  /**
   * @param configuration
   *          e.g. "Release"
   * @param sdk
   *          e.g. "iphoneos"
   */
  public EffectiveBuildSettings(MavenProject project, String configuration, String sdk)
  {
    this(project.getBuild().getDirectory(), configuration, sdk);
  }
  
  /**
   * @param directory
   *          the project directory
   * @param configuration
   *          e.g. "Release"
   * @param sdk
   *          e.g. "iphoneos"
   */
  public EffectiveBuildSettings(String directory, String configuration, String sdk)
  {
      File file = getBuildSettingsFile(directory, configuration, sdk);
      Properties p = new Properties();
      FileInputStream fis = null;
      try {
        fis = new FileInputStream(file);
        p.load(fis);
        properties = p;
      }
      catch (IOException e) {
        throw new IllegalStateException("Could not read build properties file " + file, e);
      }
      finally {
        IOUtils.closeQuietly(fis);
      }
  }
  
  public String getBuildSetting(String key)
  {
    return properties.getProperty(key);
  }
  
  public String getBuildSetting(String key, String defaultValue)
  {
    return properties.getProperty(key, defaultValue);
  }
}

@@ -972,73 +972,8 @@
 E%22;%0A
-  public static final String INFO_PLIST_FILE = %22INFOPLIST_FILE%22;%0A
   pu
@@ -1717,17 +1717,16 @@
 ing(INFO
-_
 PLIST_FI

Remove duplicate variable for INFO_PLIST in EffectiveBuildSettings