buildscript {
    ext.cubaVersion = '7.2.13'

    repositories {
        maven {
            url System.getenv('CUBA_WORK_URL')
            credentials {
                username(rootProject.hasProperty('repoUser') ? rootProject['repoUser'] : 'cuba')
                password(rootProject.hasProperty('repoPass') ? rootProject['repoPass'] : 'cuba123')
            }
        }

        maven {
            url System.getenv('CUBA_JCENTER_URL')
        }

        maven {
            url System.getenv('CUBA_MAIN_URL')
        }

        mavenLocal()
    }

    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    }
}

def modulePrefix = 'gatediadoc'

def globalModule = project(":${modulePrefix}-global")
def coreModule = project(":${modulePrefix}-core")
def webModule = project(":${modulePrefix}-web")

def servletApi = 'javax.servlet:javax.servlet-api:3.1.0'

def libsDir = "$System.env.EXT_LIBS_DIR"

apply(plugin: 'cuba')

cuba {
    artifact {
        group = 'ru.gptrans.gatediadoc'

        if (project.hasProperty('projVersion')) {
            version = project.projVersion
        } else {
            version = '0.1'
        }

        if (project.hasProperty('projSnapshot')) {
            isSnapshot = project.projSnapshot
        } else {
            isSnapshot = false
        }
    }
}

dependencies {
    appComponent("com.haulmont.cuba:cuba-global:$cubaVersion")
    appComponent('com.haulmont.addon.restapi:restapi-global:7.2.3')
}

def postgres = 'org.postgresql:postgresql:42.2.9'

configure([globalModule, coreModule, webModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('org.junit.jupiter:junit-jupiter-api:5.5.2')
        testCompile('org.junit.jupiter:junit-jupiter-engine:5.5.2')
        testCompile('org.junit.vintage:junit-vintage-engine:5.5.2')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }

    test {
        useJUnitPlatform()
    }
}

configure(globalModule) {
    dependencies {
        if (!JavaVersion.current().isJava8()) {
            runtime('javax.xml.bind:jaxb-api:2.3.1')
            runtime('org.glassfish.jaxb:jaxb-runtime:2.3.1')
        }
    }

    entitiesEnhancing {
        main {
            enabled = true
        }
    }
}

configure(coreModule) {

    configurations {
        jdbc
        dbscripts
    }

    dependencies {
        compile(globalModule)
        compileOnly(servletApi)
        jdbc(postgres)
        testRuntime(postgres)
    }

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}-core/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}-core"
        appJars(modulePrefix + '-global', modulePrefix + '-core')
    }

    task createDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
        host = 'localhost'
        dbName = 'gatediadoc'
        dbms = 'postgres'
        dbUser = 'postgres'
        dbPassword = 'postgres'
    }

    task createTestDb(dependsOn: assembleDbScripts, description: 'Creates local database', type: CubaDbCreation) {
        dbms = 'postgres'
        host = 'localhost'
        dbName = 'gatediadoc_test'
        dbUser = 'postgres'
        dbPassword = 'postgres'
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
        host = 'localhost'
        dbName = 'gatediadoc'
        dbms = 'postgres'
        dbUser = 'postgres'
        dbPassword = 'postgres'
    }
}

configure(webModule) {
    configurations {
        webcontent
    }

    dependencies {
        compileOnly(servletApi)
        compile(globalModule)
    }

    task webArchive(type: Zip) {
        from file("$buildDir/web")
        from file('web')
        classifier = 'web'
    }

    artifacts {
        archives webArchive
    }

    task deployConf(type: Copy) {
        from file('src')
        include "ru/gptrans/gatediadoc/**"
        into "$cuba.appHome/${modulePrefix}/conf"
    }

    task clearMessagesCache(type: CubaClearMessagesCache) {
        appName = "${modulePrefix}"
    }

    deployConf.dependsOn clearMessagesCache

    task cleanConf(description: 'Cleans up conf directory', type: Delete) {
        delete "$cuba.appHome/${modulePrefix}/conf"
    }

    task deploy(dependsOn: [assemble, cleanConf], type: CubaDeployment) {
        appName = "${modulePrefix}"
        appJars(modulePrefix + '-global', modulePrefix + '-web')
    }
}

task undeploy(type: Delete, dependsOn: ":${modulePrefix}-web:cleanConf") {
    delete("$cuba.tomcat.dir/shared")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}-core")
    delete("$cuba.tomcat.dir/webapps/${modulePrefix}")
}

task restart(dependsOn: ['stop', ":${modulePrefix}-core:deploy", ":${modulePrefix}-web:deploy"], description: 'Redeploys applications and restarts local Tomcat') {
    doLast {
        ant.waitfor(maxwait: 6, maxwaitunit: 'second', checkevery: 2, checkeveryunit: 'second') {
            not {
                socket(server: 'localhost', port: '8787')
            }
        }
    }
}

task buildUberJar(type: CubaUberJarBuilding) {
    coreJettyEnvPath = 'modules/core/web/META-INF/jetty-env.xml'
    logbackConfigurationFile = 'etc/uber-jar-logback.xml'
    webJettyConfPath = 'etc/jetty.xml'
    singleJar = true
    appProperties = ['cuba.automaticDatabaseUpdate': true]
}

task copyLibs(type: Copy) {
    from "$libsDir"
    include "*.jar"
    into "$cuba.tomcat.dir/lib"
}

apply from: 'extra.gradle'