/*
 * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.nio.file.Files;
import java.nio.file.Path;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.Executor;
import jdk.jpackage.test.TKit;
import jdk.jpackage.test.JavaTool;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.Annotations.Parameter;


/**
 * Tests generation of app image with --mac-app-store and --runtime-image. jpackage should able
 * to generate app image if runtime image does not have "bin" folder and fail otherwise.
 */

/*
 * @test
 * @summary jpackage with --mac-app-store and --runtime-image
 * @library ../helpers
 * @library /test/lib
 * @build jdk.jpackage.test.*
 * @build MacAppStoreRuntimeTest
 * @modules jdk.jpackage/jdk.jpackage.internal
 * @requires (os.family == "mac")
 * @run main/othervm -Xmx512m jdk.jpackage.test.Main
 *  --jpt-run=MacAppStoreRuntimeTest
 */
public class MacAppStoreRuntimeTest {

    private static String getRuntimeImage(boolean stripNativeCommands) throws IOException {
        final Path workDir = TKit.createTempDirectory("runtime").resolve("data");
        final Path jlinkOutputDir = workDir.resolve("temp.runtime");
        Files.createDirectories(jlinkOutputDir.getParent());

        // List of modules required for test app.
        final var modules = new String[] {
            "java.base",
            "java.desktop"
        };

        List<String> jlinkArgs = new ArrayList<>();
        jlinkArgs.add("--output");
        jlinkArgs.add(jlinkOutputDir.toString());
        jlinkArgs.add("--add-modules");
        jlinkArgs.add(String.join(",", modules));
        jlinkArgs.add("--strip-debug");
        jlinkArgs.add("--no-header-files");
        jlinkArgs.add("--no-man-pages");
        if (stripNativeCommands) {
            jlinkArgs.add("--strip-native-commands");
        }

        new Executor()
                .setToolProvider(JavaTool.JLINK)
                .dumpOutput()
                .addArguments(jlinkArgs)
                .execute();

        return jlinkOutputDir.toString();
    }

    @Test
    @Parameter("true")
    @Parameter("false")
    public static void test(boolean stripNativeCommands) throws Exception {
        JPackageCommand cmd = JPackageCommand.helloAppImage();
        cmd.addArguments("--mac-app-store", "--runtime-image", getRuntimeImage(stripNativeCommands));

        if (stripNativeCommands) {
            cmd.executeAndAssertHelloAppImageCreated();
        } else {
            cmd.execute(1);
        }
    }
}