/*
 * Copyright
 *   2005 axYus - www.axyus.com
 *   2005 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 fr.gouv.finances.cp.utils.RegAccess;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.cp.xemelios.updater.AbstractConfigurator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author chm
 */
public class Core33027 extends AbstractConfigurator {
    
    public static final transient String SYS_PROP_XEMELIOS_TOOLS_DIR = "${"+Constants.SYS_PROP_XEMELIOS_TOOLS_DIR+"}";
    public static final transient String PROP_XEMELIOS_TOOLS_DIR = "xemelios.tools.directory";

    public Core33027() {
        super();
    }
    @Override
    public void run() throws Exception {
        String prop = props.replace(SYS_PROP_XEMELIOS_TOOLS_DIR);
        boolean propSet = !SYS_PROP_XEMELIOS_TOOLS_DIR.equals(prop);
        if(!propSet) {
            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(PROP_XEMELIOS_TOOLS_DIR, "${xemelios.prg}/root/tools");
                xemProps.store(new FileOutputStream(xemPropsFile),"");
            }
        }
        if(System.getProperty("os.name").toLowerCase().startsWith("windows")) {
            String regName = RegAccess.getRegistryEntry("HKEY_CLASSES_ROOT\\.xml");
            File f = new File(new File(props.replace("${xemelios.prg}")+"/root"),"xemelios-reader.reg");
            String location = props.replace("${xemelios.prg}");
            location = StringUtils.replace(location, "/", "\\");
            location = StringUtils.replace(location,"\\", "\\\\");
            PrintWriter pw = new PrintWriter(f);
            pw.println("REGEDIT4");
            pw.println();
            pw.println("[HKEY_CLASSES_ROOT\\"+regName+"\\shell\\openAsPES]");
            pw.println("@=\"Voir comme document PES Aller...\"");
            pw.println();
            pw.println("[HKEY_CLASSES_ROOT\\"+regName+"\\shell\\openAsPES\\command]");
            pw.println("@=\"\\\""+location+"\\\\root\\\\viewdoc.cmd\\\" \\\"%1\\\" \\\"PES_Aller\\\"\"");
            pw.println();
            pw.println("[HKEY_CLASSES_ROOT\\"+regName+"\\shell\\openAsXemelios]");
            pw.println("@=\"Voir comme document Xemelios...\"");
            pw.println();
            pw.println("[HKEY_CLASSES_ROOT\\"+regName+"\\shell\\openAsXemelios\\command]");
            pw.println("@=\"\\\""+location+"\\\\root\\\\viewdoc.cmd\\\" \\\"%1\\\"\"");
            pw.flush(); pw.close();
            String cmdLine = "regedit /s \""+f.getAbsolutePath()+"\"";
            Runtime.getRuntime().exec(cmdLine);
        }
        setPreviousConfigurator(new Core33021());
    }

}
