// We depend on root project because we use "commitsAfterTag" from it
evaluationDependsOn(":")

description = "Timebase Commons Server API Library"

dependencies {
    api project(':java:timebase:pub')
    api project(':java:timebase:snmpagent')

    api ('com.epam.deltix:timebase-lang')
    api ('com.epam.deltix:timebase-util')
    api ('com.epam.deltix:timebase-messages')

    implementation 'javax.xml.bind:jaxb-api'

    api 'org.apache.tomcat.embed:tomcat-embed-core' // Tomcat core
    implementation 'org.apache.tomcat.embed:tomcat-embed-jasper' // Tomcat JSP support
    implementation 'org.apache.tomcat.embed:tomcat-embed-logging-juli' // Tomcat Logging

    compileOnly 'javax.servlet.jsp.jstl:jstl-api'; // JSTL Support
    compileOnly 'org.apache.taglibs:taglibs-standard-impl' // Tomcat JSTL Support
    implementation 'javax.servlet:javax.servlet-api:3.1.0' // Tomcat Requires
    implementation 'org.glassfish:javax.el:3.0.0' // Tomcat Requires

    implementation 'io.micrometer:micrometer-registry-prometheus'

    implementation 'javax.mail:mail'
    implementation 'commons-collections:commons-collections'

    implementation 'org.apache.commons:commons-text'

    testImplementation 'commons-io:commons-io'

    implementation 'com.github.sarxos:windows-registry-util:0.3'

    implementation 'com.epam.deltix:gflog-api',
            'com.epam.deltix:gflog-core',
            'com.epam.deltix:gflog-jul',
            'com.epam.deltix:gflog-jcl',
            'com.epam.deltix:gflog-slf4j',
            'com.epam.deltix:gflog-mail'
}

import org.apache.tools.ant.filters.ReplaceTokens

// TODO: Copy to same folder in not the best idea. We should
// 1) move template to a separate source set (we don't want it in the output)
// 2) move generated class to a separate source path
task versionClass(type: Copy) {
    inputs.property("version", project.version)
    inputs.property("revision", rootProject.ext.revision)
    inputs.property("commitsAfterTag", String.valueOf(rootProject.ext.commitsAfterTag)) // Note: Gradle is supposed to accept null values but as for now (Gradle 5.4.1) it fails if value is null
    inputs.property("commitTimestamp", rootProject.ext.commitTimestamp)

    def parsedVersion = project.version.split('\\.')
    //outputs.file("$project.projectDir/src/main/java/deltix/util/Version.java")

    from "$project.projectDir/src/main/java/deltix/util"
    into "$project.projectDir/src/main/java/deltix/util"
    include '**/Version-VCS.txt'
    rename 'Version-VCS.txt', 'Version.java'
    filter(ReplaceTokens, tokens: [MAJOR: parsedVersion[0], MINOR: parsedVersion[1], BUILDNAME: parsedVersion[2], WCREV: rootProject.ext.revision, COMMITS_AFTER_TAG: String.valueOf(rootProject.ext.commitsAfterTag), BUILD_DATE: rootProject.ext.commitTimestamp])
}

project.tasks.getByName('compileJava').dependsOn(versionClass)
// Note: Right now Gradle caches outputs of "compileJava" even if file "Version.java" was changed.
// TODO: We should explicitly tell Gradle that task "compileJava" depends on outputs of "versionClass" talk.

clean.doFirst {
    delete file("$project.projectDir/src/main/java/deltix/util/Version.java")
}

task copyWebapp(type: Copy) {
    description 'Copy default webapp war file into quantserver pub resources'
    dependsOn ':java:timebase:web:war'
    mustRunAfter clean
    mustRunAfter processResources

    from (project(':java:timebase:web').file('build/libs')) {
        include '*.war'
        rename 'timebase-web(.*).war', 'timebase-web.war'
    }
    into sourceSets.main.output.resourcesDir.absolutePath + '/webapp'
}

jar.dependsOn copyWebapp
