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

import com.install4j.api.*;
import com.install4j.api.windows.WinEnvVars;

import java.io.IOException;

// This action sets an environment variable on Windows. It works
// on all Windows versions >= Win95
//
// This class should be registered as a pre- or post-install action in
// the install4j GUI.
public class SetEnvironmentVariableAction extends InstallAction {

    // You override the performAction method of the InstallAction base class
    // to add your own code.
    public boolean performAction(InstallerWizardContext context, ProgressInterface progressInterface) throws UserCanceledException {
        return setEnvVar(context);
    }

    // If you want the unattended installer to set the environment
    // variable as well, you have to override the performUnattendedAction
    // method. The actual installation has been factored out to the
    // separate setEnvVar method.
    public boolean performUnattendedAction(InstallerContext context) {
        return setEnvVar(context);
    }

    // Helper method that sets the environment variable
    private boolean setEnvVar(InstallerContext context) {

        try {
            // The WinEnvVars class does the job, its set method sets a
            // specified environment variable name to a certain value.
            WinEnvVars.set("MYAPP_HOME", context.getInstallationDirectory().getPath());
        } catch (IOException e) {
            // didn't work
        }

        return true;
    }

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