/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package fr.gouv.finances.cp.xemelios.patches;

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

import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.cp.xemelios.updater.AbstractConfigurator;

/**
 * This class patches xemelios.properties file for 3.2.00 release
 * @author chm
 */
public class Core3200 extends AbstractConfigurator {
    public static final transient String PROPERTY_FORCE_RELOAD = "${"+Constants.SYS_PROP_NAVIGATE_FORCE_RELOAD+"}";
    public static final transient String PROPERTY_RESOURCES = "${"+Constants.SYS_PROP_RESOURCES_LOCATION+"}";
    
    /** Creates a new instance of Core3200 */
    public Core3200() {
        super();
    }
    
    public void run() throws Exception {
		String ret = props.replace(PROPERTY_FORCE_RELOAD);
                boolean isPropertyReloadSet = PROPERTY_FORCE_RELOAD.equals(ret);
                ret = props.replace(PROPERTY_RESOURCES);
                boolean isPropertyResourcesSet = PROPERTY_RESOURCES.equals(ret);
		if(!isPropertyReloadSet || !isPropertyResourcesSet) {
			Properties xemProps = new Properties();
			File xemPropsFile = new File(props.replace("${xemelios.prg}/root/xemelios.properties"));
			if(!xemPropsFile.exists()) {
			} else {
				xemProps.load(new FileInputStream(xemPropsFile));
                                if(!isPropertyReloadSet) xemProps.put(PROPERTY_FORCE_RELOAD.substring(2,PROPERTY_FORCE_RELOAD.length()-1),"false");
                                if(!isPropertyResourcesSet) xemProps.put(PROPERTY_RESOURCES.substring(2,PROPERTY_RESOURCES.length()-1),"${xemelios.prg}/root/resources");
                                if(System.getProperty("http.proxyHost")!=null) xemProps.put(Constants.SYS_PROP_PROXY_SERVER,System.getProperty("http.proxyHost"));
                                if(System.getProperty("http.proxyPort")!=null) xemProps.put(Constants.SYS_PROP_PROXY_PORT,System.getProperty("http.proxyPort"));
				xemProps.store(new FileOutputStream(xemPropsFile),"");
			}
		}
		setPreviousConfigurator(new Core3110());
    }
    
}
