/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/current/userguide/building_java_projects.html
 */
 buildscript {
   dependencies {
     classpath "com.google.protobuf:protobuf-gradle-plugin:0.9.4"
   }
 }
plugins {
    // Apply the java plugin to add support for Java
    id "java"

    // Apply the application plugin to add support for building a CLI application
    id "application"

    id "com.github.johnrengelman.shadow" version "8.1.1"

    id "nu.studer.jooq" version "8.0"

    id 'pmd'
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    maven {
        url = "https://plugins.gradle.org/m2/"
    }
    maven {
        url = "https://oss.sonatype.org/content/repositories/snapshots/"
    }
    mavenCentral()
    mavenLocal()
    maven {
        url = "https://maven.cubrid.org/"
    }
}

version = "3.3.1"

java {
    sourceCompatibility = JavaVersion.VERSION_21
    targetCompatibility = JavaVersion.VERSION_21
}
println("Java Version: " + JavaVersion.current())
ext {
    vertxVersion = "4.5.12"
    junitJupiterEngineVersion = "5.10.2"
    jooqVersion = "3.19.3"
    protoVersion = "3.23.3"
    grpcVersion = "1.61.0"
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}

pmd {
    ruleSetFiles = files("dodexstart.xml")
    ruleSets = []
    ignoreFailures = true
    toolVersion = "6.36.0"
}
sourceSets {
    main {
        resources {
            srcDir "handicap/build/classes/java/main"
        }
    }
}

dependencies {
    implementation(platform("io.vertx:vertx-stack-depchain:$vertxVersion"))
    implementation group:"io.vertx", name:"vertx-core", version:"$vertxVersion"
    implementation "io.vertx:vertx-web:$vertxVersion"
    implementation group: "io.agroal", name: "agroal-pool", version: "2.5"
    implementation "io.vertx:vertx-pg-client:$vertxVersion"
    implementation "io.vertx:vertx-mysql-client:$vertxVersion"
    implementation "io.vertx:vertx-jdbc-client:$vertxVersion"
    implementation "io.vertx:vertx-db2-client:$vertxVersion"
    implementation "io.vertx:vertx-rx-java3:$vertxVersion"

    implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.46.0.0'
    implementation group: 'cubrid', name: 'cubrid-jdbc', version: '11.2.1.0038'

    implementation "com.google.firebase:firebase-admin:9.4.1"

    implementation group: "org.neo4j.driver", name: "neo4j-java-driver", version: "4.4.18"
    implementation "io.vertx:vertx-mongo-client:$vertxVersion"
    implementation "io.smallrye.reactive:smallrye-mutiny-vertx-core:2.30.1"
    implementation group: 'javax.ws.rs', name: 'jsr311-api', version: '1.1.1'

    implementation "org.jooq:jooq"
    implementation "io.vertx:vertx-tcp-eventbus-bridge:$vertxVersion"
    implementation "io.vertx:vertx-kafka-client:$vertxVersion"

    implementation "com.google.guava:guava:33.2.1-jre"
    implementation project(":handicap")
    implementation "dmo.fs.utils:generate"

    implementation "io.vertx:vertx-web-openapi-router:$vertxVersion"
    implementation "io.vertx:vertx-web-api-contract:$vertxVersion"

    implementation "io.vertx:vertx-mqtt:$vertxVersion"

    implementation group: "jakarta.xml.bind", name: "jakarta.xml.bind-api", version: "4.0.0"
    runtimeOnly group: "com.sun.xml.bind", name: "jaxb-impl", version: "4.0.0"

    testImplementation "io.vertx:vertx-junit5:$vertxVersion"
    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitJupiterEngineVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junitJupiterEngineVersion"
    testImplementation "junit:junit:4.13.2"
}

application {
    getMainClass().set("io.vertx.core.Launcher")
}

def mainVerticleName = "dmo.fs.vertx.Server"
def mainClassName = "io.vertx.core.Launcher"
def doOnChange = ".${File.separator}gradlew classes"
def watchForChange = "src/main/java/**/*.java, handicap/src/main/**/*"
def debug = ""
def cache = "--java-opts=-Dvertx.disableFileCaching=true"
def OS = System.getProperty("os.name").toLowerCase()
def usr = System.getProperty("user.home")

apply plugin: "nu.studer.jooq"

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs << "-Xlint:deprecation"
    if(project.hasProperty("compiler.prop")) {
	    options.compilerArgs << project.property("compiler.prop")
    }
}

processResources {
    dependsOn handicap:compileJava
}

startScripts {
    dependsOn shadowJar
}

startShadowScripts {
    dependsOn jar
}

run {
     if (System.getProperty("DEBUG", "false") == "true") {
        debug = "--java-opts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dvertx.disableFileCaching=true"
        cache = ""
     }

    environment "VERTXWEB_ENVIRONMENT", "dev"
    if (System.getenv("GOOGLE_APPLICATION_CREDENTIALS") == null) {
      environment "GOOGLE_APPLICATION_CREDENTIALS", "$usr/auth/service-account-key.json"
    }
    if(System.getProperty("GRPC_SERVER") == "true") {
        environment "GRPC_SERVER", "true"
    } else if(System.getProperty("GRPC_SERVER") == "false") {
        environment "GRPC_SERVER", "false"
    }
    args = ['run', mainVerticleName,  debug, "--redeploy=$watchForChange", "--launcher-class=$mainClassName",
    "--on-redeploy=$doOnChange", "--conf=src/main/resources/application-conf.json",
     cache]
}

/*
  The stop, start, list Server commands can be used after running "gradlew shadowJar".
 */
tasks.register('stopServer', Exec) {
    def id = System.getProperty("id") ? System.getProperty("id") : ""
    workingDir "./build/libs"

    //on windows:
    if (OS.indexOf("win") >= 0) {
        commandLine "cmd", "/c", "java" args "-jar", "dodex-vertx-${version}-prod.jar", "stop" id
    } else {
        //on linux
        commandLine "java" args "-jar", "dodex-vertx-${version}-prod.jar", "stop", id
    }
}

tasks.register('startServer', Exec) {
    workingDir "./build/libs"

    //on windows:
    if (OS.indexOf("win") >= 0) {
        commandLine "cmd", "/c", "java" args "-jar", "dodex-vertx-${version}-prod.jar", "start"
    } else {
        //on linux
        commandLine "java" args "-jar", "dodex-vertx-${version}-prod.jar", "start"
    }
}

tasks.register('listServer', Exec) {
    workingDir "./build/libs"

    //on windows:
    if (OS.indexOf("win") >= 0) {
        commandLine "cmd", "/c", "java" args "-jar", "dodex-vertex.${version}-prod.jar", "list"
    } else {
        //on linux
        commandLine "java" args "-jar", "dodex-vertx-${version}-prod.jar", "list"
    }
}

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

shadowJar {
  zip64 = true
  archiveClassifier = "prod"
  mergeServiceFiles {
    include "META-INF/services/io.vertx.core.spi.VerticleFactory"
  }

  manifest{
    attributes 'Main-Verticle': mainVerticleName
  }
}

test {
    // Use junit platform for unit tests
    environment "VERTXWEB_ENVIRONMENT", "test"
    useJUnitPlatform()
    testLogging {
        events "PASSED", "FAILED", "SKIPPED"
    }
}
