/*
 * 
 * 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.ui.MainWindow;
import fr.gouv.finances.cp.xemelios.updater.AbstractConfigurator;
import java.io.File;
import java.io.FileOutputStream;

/**
 *
 * @author Christophe Marchand <christophe.marchand@axyus.com>
 */
public class Core4_0_0_21 extends AbstractConfigurator {
    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Core4_0_0_21.class);

    @Override
    public void run() throws Exception {
        if(System.getProperty("os.name").toLowerCase().indexOf("windows")>=0) {
            String allUserHome = System.getenv("ALLUSERSPROFILE");
            File home = new File(allUserHome);
            File startMenu = new File(home,"Menu Démarrer");
            if(!startMenu.exists() || !startMenu.isDirectory()) {
                startMenu = new File(home,"Start Menu");
                if(!startMenu.exists() || !startMenu.isDirectory()) startMenu = null;
            }
            if(startMenu!=null) {
                File programs = new File(startMenu,"Programmes");
                if(!programs.exists() || !programs.isDirectory()) {
                    programs = new File(startMenu,"Programs");
                    if(!programs.exists() || !programs.isDirectory()) programs=null;
                }
                if(programs!=null) {
                    File xemelios = new File(programs,"Xemelios");
                    if(xemelios.exists() && xemelios.isDirectory()) {
                        File xemeliosReader = new File(xemelios,"Xemelios Reader.lnk");
                        if(!xemeliosReader.exists()) {
                            createShortcut(xemeliosReader);
                        }
                    }
                }
            }
        }
        setPreviousConfigurator(new Core4_0_0_17());
    }

    private void createShortcut(File target) {
        String CRLF = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        sb.append("Set oWS = WScript.CreateObject(\"WScript.Shell\")").append(CRLF);
        sb.append("sLinkFile = \"").append(target.getAbsolutePath()).append("\"").append(CRLF);
        sb.append("Set oLink = oWS.CreateShortcut(sLinkFile)").append(CRLF);
        File root = new File(new File(props.replace("${xemelios.prg}")),"root");
        File xemeliosReader = new File(root,"viewdoc.cmd");
        sb.append("oLink.TargetPath = \"").append(xemeliosReader.getAbsolutePath()).append("\"").append(CRLF);
        sb.append("oLink.Description = \"Xemelios Reader\"").append(CRLF);
        File icon = new File(root,"xemelios.ico");
        sb.append("oLink.IconLocation = \"").append(icon.getAbsolutePath()).append("\"").append(CRLF);
        sb.append("oLink.WindowStyle = \"7\"").append(CRLF);
        sb.append("oLink.WorkingDirectory = \"").append(root.getAbsolutePath()).append("\"").append(CRLF);
        sb.append("oLink.Save").append(CRLF);

        File script = new File(root,"xemelios-reader.vbs");
        try {
            FileOutputStream fos = new FileOutputStream(script);
            fos.write(sb.toString().getBytes());
            fos.flush(); fos.close();
        } catch(Exception ex) {
            logger.error("while writting script",ex);
        }
        String[] cmd = {"CSCRIPT",script.getAbsolutePath()};
        try {
            Runtime.getRuntime().exec(cmd);
        } catch(Exception ex) {
            logger.error("while running script",ex);
        }
        //script.delete();
    }

}
