/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.nplusc.izc.InstallPak;
import de.nplusc.izc.tools.IOtools.FileTK;
import de.nplusc.izc.tools.UiToolz.BootStrap;
import de.nplusc.izc.tools.UiToolz.UiTools;
import de.nplusc.izc.tools.baseTools.Tools;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URISyntaxException;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
/**
 *
 * @author LH
 */
public class ModeSel// extends javax.swing.JFrame
{
    public static boolean silentMode=false;
    public static boolean packMode = false;
    public static String filePath = "";
    public static boolean isBundle = false;
    public static int topleft_coords_x = -1;//Detektion gegen diese Werte. -1=nicht initialisiert
    public static int topleft_coords_y = -1;//^
    private static String[] pargs;
    
    
    public static String[] getArgs()
    {
        return pargs;
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        pargs=args;
        String libpath = "";
        try
        {
            //http://www.softicons.com/free-icons/application-icons/installer-icon-by-macuser64/installer-icon
            //http://www.iconfinder.com/icondetails/61818/128/box_icon
            //http://openclipart.org/detail/172351/ppa-icon-by-myromance123-172351
            
            //installs to C:\program files\iZsetup\
            
            //setDefauötPosition
            //852, 331
            
            //HAX_ALERT
            
           libpath = FileTK.getFilePath(new File(ModeSel.class.getProtectionDomain()
                   .getCodeSource().getLocation().toURI().getPath()).getPath());
        }
        catch (URISyntaxException ex)
        {
            ex.printStackTrace();
        }
        
        
        System.setProperty( "java.library.path", libpath);
 
        Field fieldSysPath;
        try
        {
        fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
        fieldSysPath.setAccessible( true );
        fieldSysPath.set( null, null );
        }
                catch (NoSuchFieldException | SecurityException |IllegalAccessException ex)
        {
            ex.printStackTrace();
        }
        
        
        
        
        topleft_coords_x = (UiTools.getScreenRes()[0]/2)-(852/2);
        topleft_coords_y = (UiTools.getScreenRes()[1]/2)-(330/2);
        
        //args = new String[]{"-file",""
        for (int i = 0; i < args.length; i++)
        {
            System.err.println(i+"|"+args[i]);
            
        }
        //String file = "default.izsetup";
        

        //if(izsetupReader.getSysSpezSetupFile(file,true)!=null)
        //{
          //  packMode=true;
        //}
        //DSAB_FOR_DBG
        //if(izsetupReader.getSysSpezSetupFile(file)==null&&!args[0].equals("-file"))
        //{
        //    return;
        //}
        String file="";
        boolean enableFile = false;
        if(args.length>1)
        {
            //Tester.main(args);
            if(args[0].equals("-file"))
            {
                file=args[1];
                enableFile = true;
                //-file gamestls.izsetup --silent
            }
            if(args[0].equals("-bundle"))
            {
                file=args[1];
                isBundle = true;
                enableFile = true;
                //-file gamestls.izsetup --silent
            }
            else
            {
                isBundle=false;
            }
            for (String arg : args)
            {
                if(arg.equals("--silent"))
                {

                    silentMode=true;
                    //packMode=false;
                    if(izsetupReader.getSysSpezSetupFile(file,true)!=null)
                    {
                        packMode=true;
                    }
                    else
                    {
                        packMode=false;
                    }
                }
                if(arg.equals("--pack"))
                {
                    packMode=true;

                }
            }
        }
        //DBG_START
        //silentMode=false;
        //packMode=false;
        if(!enableFile)
        {
            System.out.println("Usage help for iZsetup CLI");
            System.out.println("izsetup [-file|-bundle] <path> <options>");
            System.out.println("-file for izsetup  or izpackage (requires --pack option to use izpackage");
            System.out.println("--pack file is packfile");
            System.out.println("--silent unattended mode");
            return;
        }
       // isBundle = true;
        //file="default.izbundle";
        //DBG_END
        Tools.DebugHelperPrint(file, true, "iZsetup.enableCoreDebug");
        Tools.DebugHelperPrint("isBundle+|+packMode+|+silentMode", true, "iZsetup.enableCoreDebug");
        Tools.DebugHelperPrint(isBundle+"|"+packMode+"|"+silentMode, true, "iZsetup.enableCoreDebug");
        if(!isBundle&&!packMode)
        {
            InstallManager.Main.setInstallScript(file);
        }
        filePath=file;
        try {
	    UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
	} catch( ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e ) { 
	    //e.printStackTrace();
	}
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            @SuppressWarnings("ResultOfObjectAllocationIgnored")
            @Override
            public void run()
            {
                System.err.println(packMode+"|"+silentMode);
                if(packMode&&!silentMode)
                {
                    new BootStrap(new PackagerGUI());
                }
                else
                {
                    if(silentMode)
                    {
                        if(!packMode)
                        {
                            InstallManager.Main.createQueue();                    
                            new BootStrap(new InstallProgressGui());
                        }
                        else
                        {
                            PackManager p = new PackManager();
                            p.loadPackageData(filePath);
                            p.createQueue();
                            new BootStrap(new InstallProgressGui(p.getQueue()));
                        }
                    }
                    else
                    {
                        if(isBundle)
                        {
                            new BootStrap(new BundleVerifyGui(filePath));
                        }
                        else
                        {
                        new BootStrap(new ModeSelectionGUI());
                        }
                    }
                }
            }
        });
    }             
}
