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

import com.install4j.api.InstallationHandler;
import com.install4j.api.Util;
import com.install4j.api.windows.WinFileSystem;

import java.io.File;

// Installation handler that installs all files in the "system32" directory
// of the distribution tree to the Windows directory if the target system
// is Windows and doesn't install those files for other platforms.
//
// Also see InstallToSystem32Action.java in the actions directory.
// In that action, the files are copied after the installation. Here,
// they are diverted during the installation, which makes it easier to
// handle a large number of files.
//
// An installation handler can be registered on the "Advanced tasks"
// tab of the installer section in the install4j GUI.
public class InstallToSystem32InstallationHandler extends InstallationHandler {

    // has to be implemented. If false is returned, the file is not installed.
    // We do not install the files in the "system32" directory if the target
    // platform is not Windows.
    public boolean shouldInstall(File file) {
        if (Util.isWindows()) {
            // for Windows, we install all files.
            return true;
        } else if (isInSystem32Dir(file)) {
            // for other platforms, the  "system32" directory is ignored
            return false;
        } else {
            // files not in the "system32" directory are always installed
            return true;
        }
    }

    // has to be implemented. Here, you can divert the installation to a different
    // directory on a per file basis. We divert files in the "system32" directory
    // to the system32 directory. We don't have to worry about other platforms,
    // since files in "system32" are not installed for them (see shouldInstall) and
    // this method is not invoked for them.
    public File getInstallationDirectory(File defaultInstallationDirectory, File file) {
        if (isInSystem32Dir(file)) {
            // The Windows directory can be obtained from a utility class in
            // the install4j API. Note that we return the installation root
            // directory, so the files will end up in the system32 directory
            // below the Windows directory.
            return WinFileSystem.getWindowsDirectory();
        } else {
            // Other files are installed to the selected installation directory.
            return defaultInstallationDirectory;
        }

    }

    // helper function that determines if a file is in the "system32" directory
    private boolean isInSystem32Dir(File file) {
        return file.getPath().startsWith("system32" + File.separator);
    }

}
