// 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.WinFileSystem;

import java.io.File;

// This action installs two native libraries to the system32
// directory on Windows, one as a regular library, one as a
// shared library.
//
// Also see InstallToSystem32InstallationHandler.java in the
// handlers directory. In that handler, the files are diverted
// during the installation, which makes it easier to handle a
// large number of files.
//
// The files are copied from the regular installation, so the
// action must be registered as a post-install action in the
// install4j GUI.
public class InstallToSystem32Action 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 installFiles(context);
    }

    // If you want the unattended installer to install the files
    // as well, you have to override the performUnattendedAction
    // method. The actual installation has been factored out to the
    // separate installFiles method.
    public boolean performUnattendedAction(InstallerContext context) {
        try {
            return installFiles(context);
        } catch (UserCanceledException e) {
            // Never happens for unattended installers
            return false;
        }
    }

    // Helper method that installs the libraries
    private boolean installFiles(InstallerContext context) throws UserCanceledException {

        // We install 2 native libraries to the system32 directory.
        // Since this action will be registered as a post-install action, all files
        // have already been installed to the installation directory. Here, the
        // libraries are located right in the installation directory.

        // The installation directory is available in the context
        File installationDirectory = context.getInstallationDirectory();

        // The system32 directory can be obtained from a utility class in
        // the install4j API.
        File systemDirectory = WinFileSystem.getSystemDirectory();

        // The source files are:
        File librarySourceFile = new File(installationDirectory, "library.dll");
        File sharedSourceFile = new File(installationDirectory, "shared.dll");

        // The target files are:
        File libraryTargetFile = new File(systemDirectory, librarySourceFile.getName());
        File sharedTargetFile = new File(systemDirectory, sharedSourceFile.getName());

        // This call installs the first library. The InstallerContext has
        // several installFile methods that allow you to install files.
        // You don't have to worry about the uninstallation, install4j will take
        // care of that.
        context.installFile(librarySourceFile, libraryTargetFile);

        // For the second library, we use specific file options.
        // Here, we specify that the library is shared, i.e. it will only
        // be uninstalled if it is not used by any other application.
        // Windows handles this with a reference counter.
        //
        // There are other options that you can set, like whether the file
        // should be uninstalled, the overwrite policy and the file
        // modification time.
        FileOptions options = new FileOptions();
        options.setShared(true);
        // Now, we pass the options object as the third parameter to the
        // installFile method of the context.
        context.installFile(sharedSourceFile, sharedTargetFile, options);

        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;
    }
}
