/********************************************************************************
 * Copyright (c) 2020, 2024 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
 ********************************************************************************/
plugins {
    id 'application'
    id "org.springframework.boot" version "3.3.11"
}

apply from: "$rootDir/gradle/java.gradle"
apply plugin: 'io.spring.dependency-management'

springBoot {
    applicationName = 'jifa'
}

dependencies {
    implementation project(':common')
    implementation project(':analysis')

    runtimeOnly project(':analysis:heap-dump:provider')
    runtimeOnly project(':analysis:jfr')
    runtimeOnly project(':analysis:gc-log')
    runtimeOnly project(':analysis:thread-dump')

    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-websocket'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'

    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
    implementation 'org.springframework.retry:spring-retry'

    implementation 'io.kubernetes:client-java:20.0.0'

    implementation 'com.mysql:mysql-connector-j:8.2.0'
    runtimeOnly 'com.h2database:h2:2.2.220'

    implementation 'com.hierynomus:sshj:0.35.0'

    implementation 'com.aliyun.oss:aliyun-sdk-oss:3.17.0'

    implementation 'com.amazonaws:aws-java-sdk-s3:1.12.657'

    // test
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

run.enabled = false
jar.enabled = false
startScripts.enabled = false
distTar.enabled = false
distZip.enabled = false

mainClassName = 'org.eclipse.jifa.server.Launcher'

tasks.named('compileJava') {
    inputs.files(tasks.named('processResources'))
}


import org.springframework.boot.gradle.tasks.run.BootRun

import java.nio.file.Files

tasks.named("processResources") {
    if (!Files.exists(projectDir.toPath().resolve("src/main/resources/static"))
            || gradle.startParameter.taskNames.contains("clean")) {
        dependsOn(':frontend:build')
    }
}

tasks.named("clean") {
    delete(projectDir.toPath().resolve("src/main/resources/static"))
}

// for kryo
applicationDefaultJvmArgs = ['--add-opens=java.base/java.lang=ALL-UNNAMED',
                             '--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED',
                             '-Djdk.util.zip.disableZip64ExtraFieldValidation=true']

static void setJvmOptions(BootRun bootRun) {
    bootRun.jvmArgs('--add-opens=java.base/java.lang=ALL-UNNAMED',
            '--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED',
            '-Djdk.util.zip.disableZip64ExtraFieldValidation=true')
}

static void setDatabase(BootRun bootRun) {
    // Note: adjust args here according to your environment
    bootRun.environment("MYSQL_HOST", "localhost:3306")
    bootRun.environment("MYSQL_DATABASE", "jifa")
    bootRun.environment("MYSQL_USER", "root")
    bootRun.environment("MYSQL_PASSWORD", "password")
}

tasks.register('runStandaloneWorker', BootRun) {
    group('jifa')
    classpath(project.sourceSets.main.runtimeClasspath)
    mainClass.convention(mainClassName)

    args('--jifa.role=standalone-worker', '--jifa.open-browser-when-ready=true')
    setJvmOptions(it)
}

tasks.register('runStandaloneWorkerWithMysql', BootRun) {
    group('jifa')
    classpath(project.sourceSets.main.runtimeClasspath)
    mainClass.convention(mainClassName)

    args('--jifa.role=standalone-worker', '--jifa.open-browser-when-ready=true')
    setJvmOptions(it)
    setDatabase(it)
}

tasks.register('runMaster', BootRun) {
    group('jifa')
    classpath(project.sourceSets.main.runtimeClasspath)
    mainClass.convention(mainClassName)

    args('--jifa.role=master')
    setJvmOptions(it)
    setDatabase(it)
}

tasks.register('runStaticWorker', BootRun) {
    group('jifa')
    classpath(project.sourceSets.main.runtimeClasspath)
    mainClass.convention(mainClassName)

    args('--jifa.role=static-worker', '--jifa.port=9102')
    setJvmOptions(it)
    setDatabase(it)
}

bootJar {
    manifest {
        attributes('Implementation-Title': 'Eclipse Jifa')
    }
    archiveFileName = 'jifa.jar'
}

bootDistTar {
    archiveFileName = 'jifa.tar'
}

bootDistZip {
    archiveFileName = 'jifa.zip'
}