/*
 * Copyright
 *  2010 axYus - http://www.axyus.com
 *  2010 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.plugin.signerfichier;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Properties;

/**
 *
 * @author cmarchand
 */
public class Starter {
    private static final Class[] PARAMETERS = new Class[] { URL.class };

    public static void main(String args[]) {
        Properties props = new Properties();
        try {
            File localDir = new File(System.getProperty("user.dir"));
            // chargement des jars pour reconstituer le classpath
            File[] jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-signerfichier-pes.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);

            localDir = localDir.getParentFile();
            jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-signerfichier-pes.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);

            File propertyFile = new File(localDir,"xemelios.properties");
            if(propertyFile.exists()) {
                props.load(new FileInputStream(propertyFile));
            } else {
                System.err.println("unable to found "+propertyFile.getAbsolutePath());
            }


            localDir = new File(localDir,"lib");
            jars = localDir.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.getName().toLowerCase().endsWith(".jar") && !"plugin-signerfichier-pes.jar".equals(pathname.getName());
                }
            });
            for(File f:jars) addFile(f);
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        new WizardSignerFichier(props).run();
        System.exit(0);
    }

    public static void addFile(File f) throws IOException {
        addURL(f.toURL());
    }

    // bitonio trouvé sur le net
    public static void addURL(URL u) throws IOException {
        URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
        Class sysclass = URLClassLoader.class;

        try {
            Method method = sysclass.getDeclaredMethod("addURL", PARAMETERS);
            method.setAccessible(true); // voyou
            method.invoke(sysloader, new Object[] { u });
        } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
        }

    }

}
