/********************************************************************************
 * Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 ********************************************************************************/
import java.nio.file.Files
import java.util.stream.Collectors

plugins {
    id 'java'
    id "dev.equo.p2deps" version "1.7.8"
}

apply from: "$rootDir/gradle/base.gradle"

import org.gradle.internal.os.OperatingSystem;

p2deps {
    into 'compileOnly', {
        addFilter('platform', {
            String os = System.getProperty('eclipse-mat-deps.os')
            String arch = System.getProperty('eclipse-mat-deps.arch', System.getProperty('os.arch'))
            String ws

            if (arch == "amd64") {
                arch = "x86_64"
            }

            if (arch != "x86_64" && arch != "aarch64") {
                throw new IllegalStateException("Unsupported architecture: ${arch}")
            }

            if (os == null) {
                def platform = OperatingSystem.current()
                if (platform == OperatingSystem.LINUX) {
                    os = "linux"
                } else if (platform == OperatingSystem.MAC_OS) {
                    os = "macosx"
                } else if (platform == OperatingSystem.WINDOWS) {
                    if (arch != "x86_64") {
                        throw new IllegalStateException("Unsupported architecture for Windows: ${arch}")
                    }
                    os = "win32"
                } else {
                    throw new IllegalStateException("Unsupported OS: ${platform}")
                }
            }

            if (os == "linux") {
                ws = "gtk"
            } else if (os == "macosx") {
                ws = "cocoa"
            } else if (os == "win32") {
                ws = "win32"
            } else {
                throw new IllegalStateException("Unsupported OS: ${os}")
            }

            it.prop('osgi.os', os)
            it.prop('osgi.ws', ws)
            it.prop('osgi.arch', arch)
        })

        p2repo 'https://download.eclipse.org/eclipse/updates/4.34/'
        install 'com.ibm.icu'
        install 'org.apache.aries.spifly.dynamic.bundle'
        install 'org.apache.felix.scr'
        install 'org.eclipse.core.commands'
        install 'org.eclipse.core.contenttype'
        install 'org.eclipse.core.jobs'
        install 'org.eclipse.core.runtime'
        install 'org.eclipse.equinox.app'
        install 'org.eclipse.equinox.common'
        install 'org.eclipse.equinox.event'
        install 'org.eclipse.equinox.preferences'
        install 'org.eclipse.equinox.registry'
        install 'org.eclipse.equinox.supplement'
        install 'org.eclipse.osgi'
        install 'org.eclipse.osgi.services'
        install 'slf4j.simple'

        install 'org.eclipse.help'
        install 'org.eclipse.jface'
        install 'org.eclipse.jface.text'
        install 'org.eclipse.swt'
        install 'org.eclipse.ui'
        install 'org.eclipse.platform.feature.group'

        p2repo 'https://download.eclipse.org/mat/1.16.1/update-site/'
        install 'org.eclipse.mat.api'
        install 'org.eclipse.mat.hprof'
        install 'org.eclipse.mat.parser'
        install 'org.eclipse.mat.report'
        install 'org.eclipse.mat.ui'

        // Although Eclipse is at 4.34.0 we specify 4.32.0 here, because, Jakarta Annotation-api on
        // 4.34.0 provides both 2.1.1 and 3.0.0, BUT, 3.0.0 is not compatible. And yet, there is no
        // way that I found with the p2 plugin to specify download of a specific targeted version.
        // By using 4.32.0 we get lucky enough that the compatible required deps are all in place.
        p2repo 'https://download.eclipse.org/tools/orbit/simrel/orbit-aggregation/release/4.32.0/'
        p2repo 'https://vlsi.github.io/mat-calcite-plugin-update-site/stable/'
        install 'MatCalcitePlugin'
        install 'jakarta.annotation-api'
        install 'bcutil'
        install 'bcprov'
    }
}

def depsDirName = "mat-deps"
def depsDirPath =
        project(':analysis:heap-dump:provider').projectDir.toPath().resolve('src/main/resources').resolve(depsDirName)

[':analysis:heap-dump:impl', ':analysis:heap-dump:provider', ':analysis:heap-dump:hook'].each {
    name ->
        project("$name") {
            ext {
                mat_deps_dir_name = depsDirName
                mat_deps_dir_path = depsDirPath.toAbsolutePath().toString()
                osgi_jar = 'org.eclipse.osgi-3.22.0.jar'
            }
        }
}

afterEvaluate {
    delete(depsDirPath)

    copy {
        from configurations.compileClasspath
        into "${depsDirPath}"
        include '*.jar'
    }

    List<String> jarNames = new ArrayList<>()
    Files.list(depsDirPath).map {
        it.toFile().getName()
    }.filter {
        it.endsWith(".jar") && !it.startsWith('heap-dump-analysis-impl')
    }.forEach {
        jarNames.add(it)
    }
    jarNames.add('heap-dump-analysis-impl-' + version + '.jar')
    def listFileName = 'list'
    Files.writeString(depsDirPath.resolve(listFileName), jarNames.stream().collect(Collectors.joining(",")))
}
