/*
 * 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
 */

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.gradleup.shadow" version "9.1.0"

    id 'pmd'

    id 'maven-publish'

    id "org.hibernate.orm" version "${hibernateOrmGradlePluginVersion}"
}

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/"
    }
}

group = 'dmo.fs'
version = "4.0.0"

println("Java Version: " + JavaVersion.current()+" -- target: " + java.targetCompatibility)
ext {
    vertxVersion = "5.0.11"
    junitJupiterEngineVersion = "5.10.2"
    jooqVersion = "3.21.2"
    protoVersion = "3.25.5"
    hibernateVersion = "7.2.5.Final"
}

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 "io.vertx:vertx-launcher-application:$vertxVersion"
    implementation "io.vertx:vertx-launcher-legacy-cli:$vertxVersion"
    implementation "com.fasterxml.jackson.core:jackson-databind:2.18.3"
    implementation "io.vertx:vertx-core:$vertxVersion"
    implementation "io.vertx:vertx-web:$vertxVersion"
    implementation "io.agroal:agroal-pool: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 "org.xerial:sqlite-jdbc:3.46.0.0"
    implementation "cubrid:cubrid-jdbc:11.2.1.0038"
    /*
        This is required when using "firebase" database. Comment for other databases using "gRPC"
    */
//    implementation "com.google.cloud:google-cloud-firestore-admin:3.31.8"
    implementation "com.google.firebase:firebase-admin:9.5.0"
    implementation "org.neo4j.driver:neo4j-java-driver:4.4.22"
    implementation "io.vertx:vertx-mongo-client:$vertxVersion"
    implementation "io.smallrye.reactive:smallrye-mutiny-vertx-core:2.30.1"
    implementation "javax.ws.rs:jsr311-api:1.1.1"

    implementation "org.jooq:jooq:$jooqVersion"
    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 "io.vertx:vertx-web-openapi-router:$vertxVersion"
    implementation "io.vertx:vertx-mqtt:$vertxVersion"

    annotationProcessor "org.hibernate.orm:hibernate-jpamodelgen:$hibernateVersion"
    implementation "org.hibernate:hibernate-core:$hibernateVersion"
    implementation "org.hibernate.orm:hibernate-agroal:$hibernateVersion"
    implementation "org.hibernate.validator:hibernate-validator:9.0.0.Final"
    implementation "com.oracle.database.jdbc:ojdbc11:23.8.0.25.04"
    implementation "com.microsoft.sqlserver:mssql-jdbc:12.10.1.jre11"
    implementation "jakarta.persistence:jakarta.persistence-api:3.2.0"

    runtimeOnly "com.h2database:h2:2.4.240"
    runtimeOnly "org.glassfish.jersey.media:jersey-media-json-binding:3.1.10"
    runtimeOnly "org.glassfish.expressly:expressly:6.0.0"
    implementation "jakarta.xml.bind:jakarta.xml.bind-api:4.0.5"

    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"
}

hibernate {  }

def mainVerticleName = "dmo.fs.vertx.Server"
//def mainClassName = "io.vertx.launcher.application.VertxApplication"
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 --sun-misc-unsafe-memory-access=allow"
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
}

ext {
//    javaMainClass = "io.vertx.launcher.application.VertxApplication"
    javaMainClass = "io.vertx.core.Launcher"
}

application {
    getMainClass().set(mainClassName)
    mainClassName = javaMainClass
    applicationDefaultJvmArgs = ["--sun-misc-unsafe-memory-access=allow", "--enable-native-access=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED"]
}

run {

    if (System.getProperty("DEBUG", "false") == "true") {
        debug = "--java-opts=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Dvertx.disableFileCaching=true --sun-misc-unsafe-memory-access=allow"
        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"

  if (System.getenv("DEFAULT_DB") == null || System.getenv("DEFAULT_DB") == "h2") {
      duplicatesStrategy = DuplicatesStrategy.INCLUDE
  }

  mergeServiceFiles {
      include "META-INF/services/io.vertx.core.spi.VerticleFactory"
  }

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

publishing {
    publications {
        customLibrary(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            name = 'dodex-vertx'
            url = layout.buildDirectory.dir("repo")
        }
    }
}

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

