/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 Christophe 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 fr.gouv.finances.cp.xemelios.updater.AbstractConfigurator;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 *
 * @author christophe.marchand
 */
public class Core4000 extends AbstractConfigurator {

    public Core4000() {
        super();
    }

    @Override
    public void run() throws Exception {
        Properties authProps = new Properties();
        File authPropsFile = new File(props.replace("${xemelios.authentication.config.filename}"));
//System.out.println("authentication.properties="+authPropsFile.getAbsolutePath());
        if(authPropsFile.exists()) {
            if(DataLayerManager.getImplementation().getParameterValue("authEngineClassName")==null) {
                FileInputStream fis = new FileInputStream(authPropsFile);
                authProps.load(fis);
                fis.close();
                String className = authProps.getProperty("authentication.manager");
//System.out.println("authentication.manager="+className);
                Properties props = new Properties();
                if("fr.gouv.finances.cp.xemelios.auth.dgcp.Manager".equals(className)) {
                    props.setProperty("authEngineClassName", "fr.gouv.finances.cp.xemelios.auth.dgcp.DgcpAuthEngine");
                    props.setProperty("auth.ldap.service.provider",authProps.getProperty("auth.ldap.service.provider"));
                    props.setProperty("auth.ldap.server.url", authProps.getProperty("auth.ldap.server.url"));
                    props.setProperty("auth.ldap.userDn", authProps.getProperty("auth.ldap.userDn"));
                } else if("fr.gouv.finances.cp.xemelios.auth.ldap.LdapManager".equals(className)) {
                    props.setProperty("authEngineClassName", "fr.gouv.finances.cp.xemelios.auth.ldap.LdapAuthEngineImpl");
                    props.setProperty("auth.ldap.baseUrl",authProps.getProperty("auth.ldap.baseUrl"));
                    props.setProperty("auth.ldap.userDN", authProps.getProperty("auth.ldap.userDN"));
                    props.setProperty("auth.ldap.applicationDN", authProps.getProperty("auth.ldap.applicationDN"));
                } else if("fr.gouv.finances.cp.xemelios.auth.ldapAphm.LdapManager".equals(className)) {
                    props.setProperty("authEngineClassName", "fr.gouv.finances.cp.xemelios.auth.ldapAphm.LdapAuthEngineImpl");
                    props.setProperty("auth.ldap.baseUrl", authProps.getProperty("auth.ldap.baseUrl"));
                    props.setProperty("auth.ldap.adminLogin", authProps.getProperty("auth.ldap.adminLogin"));
                    props.setProperty("auth.ldap.adminPassword", authProps.getProperty("auth.ldap.adminPassword"));
                    props.setProperty("auth.ldap.searchPath", authProps.getProperty("auth.ldap.searchPath"));
                } else {
                    props.setProperty("authEngineClassName", "fr.gouv.finances.dgfip.xemelios.auth.impl.basic.OldBasicAuthEngineImpl");
                }
                for(Enumeration enumer = props.propertyNames();enumer.hasMoreElements();) {
                    String key = (String)enumer.nextElement();
                    DataLayerManager.getImplementation().setParameterValue(key,props.getProperty(key));
                }
            }
            authPropsFile.delete();
        }
        Properties xemProps = new Properties();
        File xemPropsFile = new File(props.replace("${xemelios.prg}/root/xemelios.properties"));
        if(!xemPropsFile.exists()) {
        } else {
            xemProps.load(new FileInputStream(xemPropsFile));
            xemProps.setProperty(Constants.SYS_PROP_AUTH_REF_URL, "http://www.xemelios.org/collectivites/referentiel.xml");
            xemProps.store(new FileOutputStream(xemPropsFile),"");
        }
        // raccourcis
        String userHome = System.getProperty("user.home");
        File home = new File(userHome);
        File desktop = new File(home,"Bureau");
        if(desktop.exists() && desktop.isDirectory()) {
            File shortcut = new File(desktop,"Xemelios V3.lnk");
            if(shortcut.exists()) {
                shortcut.renameTo(new File(desktop,"Xemelios V4.lnk"));
            }
        }
        File startMenu = new File(home,"Menu Démarrer");
        if(startMenu.exists() && startMenu.isDirectory()) {
            File programs = new File(startMenu,"Programmes");
            if(programs.exists() && programs.isDirectory()) {
                File xemelios = new File(programs,"Xemelios");
                if(xemelios.exists() && xemelios.isDirectory()) {
                    File shortcut = new File(xemelios,"Xemelios V3.lnk");
                    if(shortcut.exists()) {
                        shortcut.renameTo(new File(xemelios,"Xemelios V4.lnk"));
                    }
                }
            }
        }

        setPreviousConfigurator(new Core33130());
    }

}
