plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.7.10'
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'antlr'

version = '2.0.0-beta.1'
group = 'io.georocket'

mainClassName = 'io.georocket.MainKt'

ext {
    vertxVersion = '4.3.2'
    geotoolsVersion = '27.0'
    slf4jVersion = '1.7.36'
    logbackVersion = '1.2.11'
    junitVersion = '5.8.2'
    testcontainersVersion = '1.17.3'
}

repositories {
    mavenCentral()
    exclusiveContent {
        forRepository {
            maven {
                url 'https://repo.osgeo.org/repository/release/'
            }
        }
        filter {
            includeGroup "it.geosolutions.jgridshift"
            includeGroup "org.geotools"
            includeGroup "org.geotools.ogc"
        }
    }
}

configurations.all {
    exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}

dependencies {
    implementation "org.slf4j:jul-to-slf4j:$slf4jVersion"
    implementation "org.slf4j:log4j-over-slf4j:$slf4jVersion"
    implementation "org.slf4j:slf4j-api:$slf4jVersion"

    implementation "ch.qos.logback:logback-classic:$logbackVersion" 
    implementation "ch.qos.logback:logback-core:$logbackVersion"

    implementation "io.vertx:vertx-core:$vertxVersion"
    implementation "io.vertx:vertx-lang-kotlin:$vertxVersion"
    implementation "io.vertx:vertx-lang-kotlin-coroutines:$vertxVersion"
    implementation "io.vertx:vertx-service-discovery:$vertxVersion"
    implementation "io.vertx:vertx-mongo-client:$vertxVersion"
    implementation "io.vertx:vertx-pg-client:$vertxVersion"
    implementation "io.vertx:vertx-web:$vertxVersion"
    implementation "io.vertx:vertx-web-client:$vertxVersion"

    implementation 'software.amazon.awssdk:s3:2.17.236'
    implementation 'de.undercouch:actson:2.0.0'
    implementation 'de.undercouch:underline:1.0.0'
    implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
    implementation 'com.google.guava:guava:31.1-jre'
    implementation 'com.fasterxml:aalto-xml:1.3.2'
    implementation 'com.h2database:h2:2.1.214'
    implementation 'com.vividsolutions:jts:1.13'
    implementation 'commons-io:commons-io:2.11.0'
    implementation 'io.pebbletemplates:pebble:3.1.5'
    implementation 'javax.servlet:javax.servlet-api:4.0.1'
    implementation 'net.java.dev.jna:jna:5.12.1'
    implementation 'net.time4j:time4j-core:4.38'
    implementation 'org.apache.ant:ant:1.10.12'
    implementation 'org.apache.commons:commons-exec:1.3'
    implementation 'org.apache.commons:commons-math3:3.6.1'
    implementation 'org.apache.commons:commons-text:1.9'
    implementation 'org.apache.httpcomponents:httpcore:4.4.15'
    implementation 'org.fusesource.jansi:jansi:2.4.0'
    implementation 'org.jline:jline:3.21.0'
    implementation 'org.yaml:snakeyaml:1.30'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.13.3'
    implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3"

    implementation("org.geotools:gt-epsg-extension:$geotoolsVersion") {
        // exclude invalid dependency
        exclude group: 'javax.media', module: 'jai_core'
    }
    implementation("org.geotools:gt-epsg-hsql:$geotoolsVersion") {
        // exclude invalid dependency
        exclude group: 'javax.media', module: 'jai_core'
    }
    implementation("org.geotools:gt-referencing:$geotoolsVersion") {
        // exclude invalid dependency
        exclude group: 'javax.media', module: 'jai_core'
    }

    // mongodb driver + snappy compression
    implementation 'org.mongodb:bson:4.7.0'
    implementation 'org.mongodb:mongodb-driver-async:3.12.11'
    implementation 'org.mongodb:mongodb-driver-reactivestreams:4.7.0'
    implementation 'org.xerial.snappy:snappy-java:1.1.8.4'
    implementation 'io.projectreactor:reactor-core:3.4.21'

    // embedded mongodb for standalone mode
    implementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo:3.4.7'

    // PostgreSQL
    implementation "org.postgresql:postgresql:42.4.0"
    implementation "org.flywaydb:flyway-core:9.0.1"
    implementation "com.ongres.scram:client:2.1" // for authentication

    // necessary for ALPN / HTTP/2
    runtimeOnly "io.netty:netty-tcnative-boringssl-static:2.0.53.Final"

    implementation "org.jetbrains.kotlin:kotlin-reflect"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactive:1.6.4'

    testImplementation 'com.github.tomakehurst:wiremock-jre8:2.33.2'
    testImplementation 'org.mongodb:mongodb-driver:3.12.11'

    testImplementation "io.mockk:mockk:1.12.4"
    testImplementation "io.vertx:vertx-junit5:$vertxVersion"
    testImplementation "org.assertj:assertj-core:3.23.1"
    testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
    testImplementation "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
    testImplementation 'org.junit.vintage:junit-vintage-engine:$junitVersion'
    testImplementation "org.testcontainers:testcontainers:$testcontainersVersion"
    testImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
    testImplementation "org.testcontainers:postgresql:$testcontainersVersion"

    antlr 'org.antlr:antlr4:4.10.1'
}

test {
    // use junit5 for tests
    useJUnitPlatform()
}

// customize start scripts
startScripts {
    // customize application name
    applicationName = 'georocket'

    // set GEOROCKET_HOME environment variable
    doLast {
        def windowsScriptFile = file(getWindowsScript())
        def unixScriptFile = file(getUnixScript())
        windowsScriptFile.text = java.util.regex.Pattern.compile('^set APP_HOME=.*', java.util.regex.Pattern.MULTILINE)
            .matcher(windowsScriptFile.text).replaceFirst('$0\r\nset GEOROCKET_HOME=%APP_HOME%')
        unixScriptFile.text = java.util.regex.Pattern.compile('^APP_HOME=.*', java.util.regex.Pattern.MULTILINE)
            .matcher(unixScriptFile.text).replaceFirst('$0\nexport GEOROCKET_HOME=\\$APP_HOME')
    }
}

distributions {
    main {
        contents {
            // include 'conf' directory in distribution
            from(projectDir) {
                include 'conf/**/*'
            }

            // include 'docs' in distribution
            from(tasks.getByPath(':docs:asciidoctor')) {
                into "docs"
                eachFile { f ->
                    f.path = f.path.replaceFirst(/html5\//, '')
                }
                includeEmptyDirs = false
            }
        }
    }
}

// Fix command-length issue in windows startscript
// see https://issues.gradle.org/browse/GRADLE-2992
task pathingJar(type: Jar) {
    archiveAppendix = 'pathing'
    manifest { attributes("Class-Path": configurations.runtimeClasspath.collect { it.getName() }.join(' ') + ' ' + jar.archiveFileName ) }
}
applicationDistribution.from(pathingJar) { into "lib" }
startScripts {
    doLast {
        def winScriptFile  = file getWindowsScript()
        def winFileText = winScriptFile.text

        // Remove too-long-classpath and use pathing jar instead
        winFileText = winFileText.replaceAll('set CLASSPATH=.*', 'rem CLASSPATH declaration removed.')
        winFileText = winFileText.replaceAll('("%JAVA_EXE%" .* -classpath ")%CLASSPATH%(" .*)', '$1%APP_HOME%\\\\lib\\\\' + pathingJar.archiveFileName + '$2')

        winScriptFile.text = winFileText
    }
}

compileJava {
    sourceCompatibility = '11'
    targetCompatibility = '11'
}

compileKotlin {
    kotlinOptions.jvmTarget = "11"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "11"
}

task generateVersionFile {
    doLast {
        def dst = new File(project.buildDir, 'generated-src/main/resources/io/georocket')
        dst.mkdirs()
        def versionFile = new File(dst, 'version.dat')
        versionFile.withWriter { w ->
            w << project.version
        }
    }
}

task generateSources(dependsOn: [generateVersionFile, generateGrammarSource])

processResources.dependsOn(generateSources)

compileKotlin.dependsOn(processResources)

// include generated sources
sourceSets.main.java.srcDirs "$buildDir/generated-src/main/java"
sourceSets.main.resources.srcDirs "$buildDir/generated-src/main/resources"
