/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.iZtools.launcher;

import de.nplusc.izc.tools.IOtools.FileTK;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.prefs.Preferences;

/**
 *
 * @author LH
 */
public class iZlaunchCore
{
    private static final Class<?>[] parameters = new Class[]{URL.class};
    //private static final Class<?>[] mparameters = new Class[]{String[].class};

    /**
     * Adds a file to the classpath.
     * @param s a String pointing to the file
     * @throws IOException
     */
    public static void addFile(String s) throws IOException {
        File f = new File(s);
        addFile(f);
    }//end method

    /**
     * Adds a file to the classpath
     * @param f the file to be added
     * @throws IOException
     */
    public static void addFile(File f) throws IOException {
        addURL(f.toURI().toURL());
        System.err.println(f.getAbsolutePath());
    }//end method

    /**
     * Adds the content pointed by the URL to the classpath.
     * @param u the URL pointing to the content to be added
     * @throws IOException
     */
    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);
            method.invoke(sysloader,new Object[]{ u }); 
        } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
        }//end try catch        
    }//end method

    
    
    
    public static void main(String[] args)
    {
        /*
         * iZlaunch format specification:
         * ../bin/path/to.jar
         * de.nplusc.izc.loadme
         * ../../path/to/update/data/from.jar
         * 
         * Alle pfade müssen relativ sein da javaerror
         * 1. zeile= Pfad der ausführbaren annwendung 
         * 2. zeile= klasse die geladen werden soll (Muss die main-methode enthalten
         * 3. Zeile= Pfad zur Update-Datei, relativ zu der in Schritt 1 referenzierten Jar-Datei
         * Diese datei muss relativ zu iZlaunch liegen
         */
        
        
        String jarschpfad = "";
        try
        {
            jarschpfad = FileTK.getFilePath(new File(iZlaunchCore.class.getProtectionDomain().
                    getCodeSource().getLocation().toURI().getPath()).getAbsolutePath());
        }
        catch (URISyntaxException ex)
        {
            ex.printStackTrace();
        }
        String[] cfgfile = FileTK.fileload(jarschpfad+"\\iZlaunch.cfg");
        //System.out.println(new File("iZlaunch.cfg").getAbsolutePath());
        
        String updootpath = new File(FileTK.getFilePath(jarschpfad+"\\"+cfgfile[0])+ "\\"+cfgfile[2]).getAbsolutePath();
        String oldfile = new File(FileTK.getFilePath(jarschpfad+"\\"+cfgfile[0])+"\\old.jar.back").getAbsolutePath();
        
        
        
        
        try
        {
            File fo = new File(oldfile);
            if(fo.exists())
            {
                fo.delete();
            }
            File f = new File(updootpath);
            if(f.exists())
            {
                File f2 = new File(jarschpfad+"\\"+cfgfile[0]);//niemals ne zahl ohne buchstabe oder unterstrich oder MystError
                f2.renameTo(new File(oldfile));
                f.renameTo(new File(jarschpfad+"\\"+cfgfile[0]));
            }
            
            
            //die eingepflanzte jarsch laden
              //Netbeans du bist nervig :(
            addFile(jarschpfad+"\\"+cfgfile[0]);
            URLClassLoader sysloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
              Class clazz = sysloader.loadClass(cfgfile[1]);
              Method method = clazz.getMethod("main",String[].class);
              method.setAccessible(true);
              method.invoke(null, (Object) args);//lädt die class::main//null=!!!!!!
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            try
            {
            ex.getCause().printStackTrace();
            }
            catch(Exception e)
            {
                
            }
            try
            {
                if(Boolean.valueOf(Preferences.userRoot().get("iZsetup.enableCoreDebug", "false")))
                {
                    Thread.sleep(15000);
                }
            }
            catch (InterruptedException ex1)
            {
                ex1.printStackTrace();
            }
                    
        } 
    }
}
