buildscript {
    ext.cubaVersion = '7.2.4'
    repositories {
        maven {
            url 'https://repo.cuba-platform.com/content/groups/work'

            credentials {
                username(rootProject.hasProperty('repoUser') ? rootProject['repoUser'] : 'cuba')
                password(rootProject.hasProperty('repoPass') ? rootProject['repoPass'] : 'cuba123')
            }
        }

    }
    dependencies {
        classpath "com.haulmont.gradle:cuba-plugin:$cubaVersion"
    }
}

def modulePrefix = 'app'

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'

apply(plugin: 'cuba')

cuba {
    artifact {
        group = 'com.company.itam'
        version = '0.1'
        isSnapshot = true
    }
    tomcat {
        dir = "$project.rootDir/deploy/tomcat"
    }
}

dependencies {
    appComponent("com.haulmont.cuba:cuba-global:$cubaVersion")
    appComponent("com.haulmont.reports:reports-global:$cubaVersion")
    appComponent("com.haulmont.charts:charts-global:$cubaVersion")
    appComponent("com.haulmont.bpm:bpm-global:$cubaVersion")
    appComponent("com.haulmont.fts:fts-global:$cubaVersion")
    appComponent('de.diedavids.cuba.wizard:wizard-global:0.6.1')
    appComponent('com.haulmont.addon.ldap:ldap-global:1.5.1')
    appComponent('de.diedavids.cuba.runtimediagnose:runtime-diagnose-global:1.5.0')
    appComponent('de.diedavids.cuba.dataimport:dataimport-global:0.13.0')
    appComponent('com.haulmont.addon.emailtemplates:yet-global:1.4.1')
}

def mssql = 'com.microsoft.sqlserver:mssql-jdbc:7.2.1.jre8'

configure([globalModule, coreModule, webModule]) {
    apply(plugin: 'java')
    apply(plugin: 'maven')
    apply(plugin: 'cuba')

    dependencies {
        testCompile('junit:junit:4.12')
    }

    task sourceJar(type: Jar) {
        from file('src')
        classifier = 'sources'
    }

    artifacts {
        archives sourceJar
    }
}

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(mssql) { exclude(group: 'com.microsoft.azure', module: 'azure-keyvault') }
        testRuntime(mssql) { exclude(group: 'com.microsoft.azure', module: 'azure-keyvault') }
        compile(group: 'net.sf.barcode4j', name: 'barcode4j', version: '2.1')
        compile(group: 'com.itextpdf', name: 'itextpdf', version: '5.5.13.1')
        compile(group: 'com.google.zxing', name: 'javase', version: '3.4.0')
        compile(group: 'com.google.zxing', name: 'core', version: '3.4.0')
    }

    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) {
        dbms = 'mssql'
        host = 'vmcubaapp'
        dbName = 'itamdb'
        dbUser = 'sa'
        dbPassword = 'password'
        dbmsVersion = '2012'
    }

    task updateDb(dependsOn: assembleDbScripts, description: 'Updates local database', type: CubaDbUpdate) {
        dbms = 'mssql'
        host = 'vmcubaapp'
        dbName = 'itamdb'
        dbUser = 'sa'
        dbPassword = 'password'
        dbmsVersion = '2012'
    }
}


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 "com/company/itam/**"
        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 buildScssThemes(type: CubaWebScssThemeCreation)

    task deployThemes(type: CubaDeployThemeTask, dependsOn: buildScssThemes)

    assemble.dependsOn buildScssThemes

    task themesJar(type: Jar) {
        from file('themes')
        classifier = 'themes'
    }

    artifacts {
        archives themesJar
    }
}

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')
            }
        }
    }
}
restart.finalizedBy start
restart.finalizedBy start

task buildWar(type: CubaWarBuilding) {
    singleWar = false
    includeContextXml = true
    includeJdbcDriver = true
    appProperties = [
            'cuba.automaticDatabaseUpdate': false,
            'cuba.webPort'                : 8082,
            'cuba.connectionUrlList'      : 'http://localhost:8082/app-core'
    ]
}