import static org.opendatakit.aggregate.gradle.Util.getValue
import static org.opendatakit.aggregate.gradle.Util.getVersionName

buildscript {
  repositories {
    jcenter()
  }
  dependencies {
    classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
    classpath "com.avast.gradle:gradle-docker-compose-plugin:0.6.8"
  }
}

plugins {
  id 'java'
  id 'maven'
  id 'war'
  id 'idea'
  id 'checkstyle'
  id 'de.fuerstenau.buildconfig' version '1.1.8'
}
apply plugin: 'gwt'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'
apply plugin: 'docker-compose'

group = 'org.opendatakit'
version = getVersionName()
description = ""

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
  mavenCentral()
  maven { url = 'https://oss.sonatype.org/content/groups/public' }

  flatDir {
    dirs 'libs'
  }
}

def versions = [
    spring        : '4.3.4.RELEASE',
    springSecurity: '4.2.0.RELEASE',
    gwt           : [
        servlet      : '2.8.2',
        googleMapsV3 : '1.0.1',
        visualization: '1.1.2'
    ],
    jackson       : '2.8.5',
    geronimo      : '1.0',
    httpcomponents: [
        client: '4.5.2',
        core  : '4.4.5',
        mime  : '4.5.2'
    ],
    commons       : [
        lang3     : '3.5',
        fileUpload: '1.3.2',
        dbcp2     : '2.1.1'
    ],
    googleApi     : [
        client: '1.22.0',
        sheets: 'v4-rev38-1.22.0',
        drive : 'v3-rev52-1.22.0'
    ],
    gson          : '2.8.0',
    kxml2         : '2.3.0',
    odk           : [
        javarosa: '2.17.2'
    ],
    jdbc          : [
        mysql     : '5.1.44',
        postgresql: '42.1.4.jre7'
    ],
    servletApi    : '3.1.0',
    junit         : '4.12',
    mockito       : '2.+'
]

configurations.all {
  exclude group: 'commons-logging', module: 'commons-logging'
}

dependencies {
  compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
  compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25' // JCL over SLF4J
  compile group: 'org.slf4j', name: 'log4j-over-slf4j', version: '1.7.25'
  compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.25'

  compile group: 'org.springframework', name: 'spring-aop', version: versions.spring
  compile group: 'org.springframework', name: 'spring-aspects', version: versions.spring
  compile group: 'org.springframework', name: 'spring-beans', version: versions.spring
  compile group: 'org.springframework', name: 'spring-context', version: versions.spring
  compile group: 'org.springframework', name: 'spring-context-support', version: versions.spring
  compile group: 'org.springframework', name: 'spring-core', version: versions.spring
  compile group: 'org.springframework', name: 'spring-expression', version: versions.spring
  compile group: 'org.springframework', name: 'spring-instrument', version: versions.spring
  compile group: 'org.springframework', name: 'spring-jdbc', version: versions.spring
  compile group: 'org.springframework', name: 'spring-tx', version: versions.spring
  compile group: 'org.springframework', name: 'spring-instrument-tomcat', version: versions.spring
  compile group: 'org.springframework', name: 'spring-web', version: versions.spring
  compile group: 'org.springframework.security', name: 'spring-security-config', version: versions.springSecurity
  compile group: 'org.springframework.security', name: 'spring-security-core', version: versions.springSecurity
  compile group: 'org.springframework.security', name: 'spring-security-crypto', version: versions.springSecurity
  compile group: 'org.springframework.security', name: 'spring-security-web', version: versions.springSecurity
  compile group: 'com.googlecode.gwt-google-maps-v3', name: 'gwt-google-maps-v3', version: versions.gwt.googleMapsV3
  compile group: 'com.google.gwt.google-apis', name: 'gwt-visualization', version: versions.gwt.visualization
  compile group: 'com.google.gwt', name: 'gwt-servlet', version: versions.gwt.servlet
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: versions.jackson
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: versions.jackson
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: versions.jackson
  compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: versions.jackson
  compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-json-provider', version: versions.jackson
  compile group: 'com.fasterxml.jackson.jaxrs', name: 'jackson-jaxrs-xml-provider', version: versions.jackson
  compile group: 'org.apache.geronimo.specs', name: 'geronimo-jaxrs_1.1_spec', version: versions.geronimo
  compile group: 'org.apache.httpcomponents', name: 'httpclient', version: versions.httpcomponents.client
  compile group: 'org.apache.httpcomponents', name: 'httpcore', version: versions.httpcomponents.core
  compile group: 'org.apache.httpcomponents', name: 'httpmime', version: versions.httpcomponents.mime
  compile group: 'org.apache.commons', name: 'commons-lang3', version: versions.commons.lang3
  compile group: 'org.apache.commons', name: 'commons-dbcp2', version: versions.commons.dbcp2
  compile group: 'commons-fileupload', name: 'commons-fileupload', version: versions.commons.fileUpload
  compile group: 'com.google.api-client', name: 'google-api-client', version: versions.googleApi.client
  compile group: 'com.google.api-client', name: 'google-api-client-appengine', version: versions.googleApi.client
  compile group: 'com.google.apis', name: 'google-api-services-sheets', version: versions.googleApi.sheets
  compile group: 'com.google.apis', name: 'google-api-services-drive', version: versions.googleApi.drive
  compile group: 'com.google.code.gson', name: 'gson', version: versions.gson
  compile group: 'net.sf.kxml', name: 'kxml2', version: versions.kxml2
  compile group: 'org.getodk', name: 'javarosa', version: versions.odk.javarosa
  compile group: 'mysql', name: 'mysql-connector-java', version: versions.jdbc.mysql
  compile group: 'org.postgresql', name: 'postgresql', version: versions.jdbc.postgresql
  compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: versions.servletApi
  testCompile group: 'junit', name: 'junit', version: versions.junit
  testCompile group: 'org.springframework', name: 'spring-test', version: versions.spring
  testCompile group: 'org.mockito', name: 'mockito-core', version: versions.mockito
}

gwt {
  gwtVersion = versions.gwt.servlet
  modules 'org.opendatakit.aggregate.AggregateUI'
  minHeapSize = "512M"
  maxHeapSize = "1024M"
}

war {
  if (getValue(this, 'warMode', 'complete') == "installer") {
    println "Excluding config files from WAR"
    war.rootSpec.exclude "jdbc.properties"
    war.rootSpec.exclude "logging.properties"
    war.rootSpec.exclude "odk-common-security-settings.xml"
    war.rootSpec.exclude "odk-security-settings.xml"
    war.rootSpec.exclude "odk-settings.xml"
    war.rootSpec.exclude "security.properties"
  }
}

gretty {
  contextPath = '/'
  host = 'localhost'
  logDir = 'logs'
}

buildConfig {
  appName = 'ODK Aggregate'
  version = getVersionName()
  clsName = 'BuildConfig'
  packageName = 'org.opendatakit.aggregate.buildconfig'
}

// Required to use fileExtensions property in checkstyle file
checkstyle {
  toolVersion = '7.6.1'
}

dockerCompose {
  postgresql {
    useComposeFiles = ['db/postgresql/docker-compose.yml']
  }
  mysql {
    useComposeFiles = ['db/mysql/docker-compose.yml']
  }
}

task("checkConf") {
  group = 'build'
  description = 'Checks that all the conf files are present'

  doLast {
    def missingFiles = [
        "jdbc.properties",
        "security.properties",
        "odk-settings.xml"
    ].findAll({ filename -> !new File("${projectDir}/src/main/resources/${filename}").exists() })
    if (!missingFiles.isEmpty())
      if(!project.gradle.startParameter.taskRequests.args[0].any{it.contains("docker")}){
        throw new GradleException("Missing configuration files at src/main/resources: ${missingFiles}")
      }
  }
}

compileJava.dependsOn checkConf

apply from: 'installer.gradle'
apply from: 'packer.gradle'
apply from: 'docker-compose.gradle'
apply from: 'docker.gradle'
