/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


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

import fr.gouv.finances.cp.utils.RegAccess;
import fr.gouv.finances.cp.utils.StartUrl;
import fr.gouv.finances.cp.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());
    }

}
