// This is example code for the install4j API
// Please feel free to adapt it for your own purposes.

import com.install4j.api.*;

import javax.swing.*;
import java.io.File;
import java.io.IOException;

// This action executes an external installer that is distributed
// as a separate executable. The actual executable is configured
// with a user variable.
//
// This class should be registered as a post-install action in
// the install4j GUI.
public class ExecuteAction extends InstallAction {

    // You override the performAction method of the InstallAction base class
    // to add your own code.
    public boolean performAction(InstallerWizardContext installerWizardContext, ProgressInterface progressInterface) throws UserCanceledException {
        // Set the status message
        progressInterface.setStatusMessage("Executing external installer ... ");

        boolean success = execute(installerWizardContext);
        if (!success) {
            // Show an error if the execution has not suceeded
            Util.showMessage("The external installer could not be executed.", JOptionPane.ERROR_MESSAGE);
        }

        return success;
    }

    // If you want the unattended installer to execute the installer
    // as well, you have to override the performUnattendedAction
    // method. The actual code has been factored out to the
    // separate execute method.
    public boolean performUnattendedAction(InstallerContext installerContext) {

        return execute(installerContext);
    }

    // Helper method that executes the external installer
    private boolean execute(InstallerContext installerContext) {
        // The user variable EXTERNAL_INSTALLER must contain the relative path the installer
        String externalInstallerName = installerContext.getUserVariableValue("EXTERNAL_INSTALLER");
        // If the user variable has not been defined, the action does nothing
        if (externalInstallerName == null) {
            return true;
        }

        // Determine the file of the installer executable
        File externalInstaller = new File(installerContext.getInstallationDirectory(), externalInstallerName);
        // If the file does not exist, raise an error and quit.
        if (!externalInstaller.exists()) {
            return false;
        }
        try {
            // Execute the external installer in with the installaton directory as the working directory
            Process process = Runtime.getRuntime().exec(externalInstaller.getPath(), null, installerContext.getInstallationDirectory());
            // ... and wait for it to complete
            int exitCode = process.waitFor();
        } catch (IOException e) {
            // error when executing
            return false;
        } catch (InterruptedException e) {
            // internal error
            return false;
        }
        // execution has succeded.
        return true;
        // If you require the exit code to be 0, you could change the return statement to:
        // return exitCode == 0;
    }

    // has to be implemented in order to tell the installer how much
    // you want of the progress bar.
    public int getPercentOfTotalInstallation() {
        return 10;
    }

}
