/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.izstreamer;

import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.IOtools.ZipFileHandler;
import de.nplusc.izc.tools.IOtools.iZformats.IZpackage;
import de.nplusc.izc.tools.baseTools.Messagers;
import de.nplusc.izc.tools.baseTools.Tools;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.util.prefs.Preferences;
import org.apache.commons.io.FileUtils;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;

/**
 *
 * @author LH
 */
public class iZstreamer
{

    public static void main(String[] args)
    {
        if (args.length > 0 && args[0].equals("--mkdist"))
        {
            Preferences.userRoot().put("Toolkit.enableCoreDebug", true + "");
            Preferences.userRoot().put("Toolkit.enableFinerDebug", true + "");

            String jpath = new File(Player.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
            System.out.println(jpath);
            try
            {
                new File(jpath + File.separator + "portableApp").createNewFile(); //erzeuge markerFile für portableMode des iZpacks
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
            FileTK.writeFile("@echo off\n"
                    + "set bindir=%~dp0\n"
                    + "cd %bindir%\n"
                    + "start javaw -jar %bindir%\\iZStreamer.jar --appsource #sfxjar#\n"
                    + "REM DUMMYDUMMYDUMMYDUMMY", jpath + File.separator + "params.sfx");//<<<<HACK!
            new File(jpath + "release.jar").delete();//alte jar ausm weg räumen falls nicht schon geschehen
            String apath = jpath + File.separator + "iZstreamerStandalone.jar";
            IZpackage.wrapArchive(apath, jpath);
            System.out.println("zippern");
            System.out.println(apath);
            if (args.length == 0)
            {
                Tools.runCmdStreamable(null, System.out, true, "zip", "-A", apath);//sfxen
            }
            System.out.println("zipped");
            System.exit(0);
            //sfx-jar mus unloaded sein damit selfpatch geht
        }
        else
        {
            if (args.length > 1)
            {
                if (args[0].equals("--appsource"))
                {
                    try
                    {
                        ZipFileHandler ex;


                        String bitness = System.getProperty("os.arch");
                        String v_url = "http://nplusc.de/izstream/iZstream-vlc-" + bitness + "-win.zip";
                        String v_dlp = new File(Player.class.getProtectionDomain().
                                getCodeSource().getLocation().getPath()).getParent() + "\\vlc.zip";

                        String versionurl = "http://nplusc.de/izstream/iZstream-vlc.version";
                        String v_dlvp = new File(Player.class.getProtectionDomain().
                                getCodeSource().getLocation().getPath()).getParent() + "\\vlc_new.version";
                        String v_compareTo = new File(Player.class.getProtectionDomain().
                                getCodeSource().getLocation().getPath()).getParent() + "\\vlc.version";
                        FileUtils.copyURLToFile(new URL(versionurl), new File(v_dlvp));
                        new File(v_compareTo).createNewFile();
                        Thread.sleep(1000);
                        System.out.println("v_dlvp"+v_dlvp);
                        System.out.println("v_compareTo"+v_compareTo);
                        boolean redl = !FileUtils.contentEquals(new File(v_dlvp), new File(v_compareTo));
                        if (redl)
                        {
                            //checkversion
                            org.apache.commons.io.FileUtils.copyURLToFile(new URL(v_url), new File(v_dlp));
                            ex = new ZipFileHandler(v_dlp);
                            ex.extractAllToDiskWithMonitoringProgressbar(FileTK.getFilePath(v_dlp), null);
                            ex.unload();
                            FileUtils.forceDelete(new File(v_compareTo));//SO GEHTS RICHTIG!
                            FileUtils.copyFile(new File(v_dlvp),new File(v_compareTo));
                            FileUtils.forceDelete(new File(v_dlp));
                            FileUtils.forceDelete(new File(v_dlvp));
                            IZpackage.wrapArchive(args[1], FileTK.getFilePath(v_dlp));//selfmerge
                        }
                        else
                        {
                            FileUtils.forceDelete(new File(v_dlvp));
                        }
                        for (int i = 0; i < 2; i++)
                        {
                            try
                            {
                                String lpath = FileTK.getFilePath(Player.class.getProtectionDomain().getCodeSource().getLocation().getPath());
                                System.out.println("lpath" + lpath);
                                NativeLibrary.addSearchPath(
                                        RuntimeUtil.getLibVlcLibraryName(), lpath);
                                Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
                                i = 3;//breakt die schleife bei success
                                Player.main(null);
                                return;
                            }
                            catch (UnsatisfiedLinkError lickme)//sollte man nicht aber so erkenn ich ob die lib da ist :P
                            {
                                lickme.printStackTrace();
                                if (i == 0)
                                {

                                    try
                                    {
                                        org.apache.commons.io.FileUtils.copyURLToFile(new URL(v_url), new File(v_dlp));
                                        ex = new ZipFileHandler(v_dlp);
                                        ex.extractAllToDiskWithMonitoringProgressbar(FileTK.getFilePath(v_dlp), null);
                                        ex.unload();
                                        FileUtils.forceDelete(new File(v_dlp));
                                    }
                                    catch (IOException exe)
                                    {
                                        exe.printStackTrace();
                                    }
                                    IZpackage.wrapArchive(args[1], FileTK.getFilePath(v_dlp));
                                }
                                else//FUUU
                                {
                                    Messagers.SingleLineMsg("Fehler beim laden der Komponenten", "Verdammt");
                                    System.exit(0xdeadbeef);
                                }
                            }
                        }
                    }
                    catch (MalformedURLException ex)
                    {
                        ex.printStackTrace();
                    }
                    catch (IOException ex)
                    {
                        ex.printStackTrace();
                    }
                    catch (InterruptedException ex)
                    {
                        ex.printStackTrace();
                    }
                }
            }
            //System.out.println(Player.class.getProtectionDomain().getCodeSource().getLocation().getPath());
            //System.out.println(new File("/D:/src/zu/test/release.jar").length());
            // 
        }
    }

}
