/*
    InstallJBoss
    This is an example of how to install JBoss.
    Notes:
     1. Change "InstallerName" to the name of the file for the jboss installer.
     2. Change "InstallGroup" to the group's name you want to assign jboss (see
        jboss docs for more details).
     3. Change the "InstallDir" constants to wherever you want to install jboss
        for each OS. 
     4. Be sure that the "InstallerName" is included in the list of distributed files.
     5. Include InstallJBoss in "After uninstaller created".
     6. You are welcome to adapt this to meet your needs as long as you
        only use it with the JExpress product
    Copyright 2012 DeNova
    
*/

import com.denova.runtime.Exec;
import com.denova.runtime.OS;
import com.denova.runtime.WindowsDirs;
import com.denova.ui.Swinger;
import com.denova.util.PropertyList;
import com.denova.JExpress.Installer.CustomInstaller;
import com.denova.JExpress.Installer.InstallPropertyNames;
import com.denova.JExpress.Installer.StatusPanel;
import java.io.File;


/**  Run a third party program */
public class InstallJBoss extends StatusPanel
                                  implements InstallPropertyNames
{
    // specify the name of the jboss installer that you included in the 
    // Select Files panel of JExpress -- do NOT include the full path; just the jar filename
    private static final String InstallerName = "jems-installer-1.2.0.jar";
    private static final String InstallGroup = "default";


    private boolean panelActivated = false;
    private String progressLabel = "Installing " + InstallerName;

    
    private class ThirdPartySwinger extends Swinger
    {
        public void swingBefore()
        {
            updateProgressBarLabel(progressLabel);
        }
        
        public void workInBackground()
        {
            final String WinInstallDir = WindowsDirs.getProgramManagerDirectory();
            final String MacInstallDir = "/opt/jboss";
            final String UnixInstallDir = "/opt/jboss";

            // get the full path to the jboss installer which 
            // you included in the Select files panel 
            String applicationDirectory = getPropertyList().getProperty(
                 ApplicationDirectory, "");
            File exeFile = new File(applicationDirectory, InstallerName);
            if (exeFile.exists())
            {
                try {
                    String installDir = null;
                    File javaFile = null;
                    
                    // get the home of the currently running jvm
                    String activeJavaHome = System.getProperty("java.home", "");
                    File javaBinDir = new File(activeJavaHome, "bin");
                    if (OS.isWindows())
                    {
                        javaFile = new File(javaBinDir, "java.exe");
                        installDir = WinInstallDir;
                    }
                    else
                    {
                        javaFile = new File(javaBinDir, "java.exe");
                        if (OS.isMacOsX())
                        {
                            installDir = MacInstallDir;
                        }
                        else
                        {
                            installDir = UnixInstallDir;
                        }
                    }
                    
                    // the install dir needs to include quotes if it has spaces
                    if (installDir.indexOf(" ") > 0)
                    {
                        installDir = Exec.quoteArg(installDir);
                    }
                    
                    // create the command which will start the installer from the
                    // command line -- we don't want the user to have to do anything
                    // so we have to pass all the required commands
                    String [] command = {
                     javaFile.getPath(), "-jar", InstallerName, "-installGroup", InstallGroup, "installpath=" + installDir};
                    Exec. runCommand  (command);
                }
                
                catch ( Exception e )
                {
                   CustomInstaller.logException ( "Unable to run program",
                                                  e );
                }
            }
            
            else {
                CustomInstaller. logToInstaller  ( InstallerName + 
                                                   " is not located in " + 
                                                   applicationDirectory +
                                                   "\nUnable to run " + InstallerName);
            }
        }
        
        public void swingAfter()
        {
            panelActivated = true;
            
            // since there's no user interaction, simply move to the next panel
            showNextPanel ();
        }
    }
    
    
    /**Constructor for the InstallJBoss object */
    public InstallJBoss(PropertyList properties)
    {
        super(properties);
        
        updateProgressBarLabel(progressLabel);
    }


    /** 
     * Enter the panel. 
     *
     * Do not call this method; only the wizard should call it.
     */
    public synchronized void enter()
    {
        if ( panelActivated )
        {
            // if another panel moves to this one
            // twice, then just bypass this panel
            showNextPanel ();

        }

        else
        {
            ThirdPartySwinger swinger = new ThirdPartySwinger();
            swinger.execute();
        }
    }

    public void reset () {

        // another panel can reactive this panel by calling reset
        panelActivated = false;
    }
    
} // InstallJBoss