import java.time.format.DateTimeFormatter
import java.time.OffsetDateTime

apply plugin: 'java-library'
apply plugin: PublishJarsPlugin

publishJars {
  artifactId = 'engine-core'
}

repositories {
    mavenCentral()
}

configurations {
  api {
    //In the gradle build jts gets included by org.jaitools:jt-utils
    //but this causes problems with WFS parsing.
    //Oddly the same jt-utils dependency in a maven build does not bring in jts.
    exclude group: 'org.locationtech', module: 'jts'
    exclude group: 'commons-beanutils', module: 'commons-beanutils'
  }
}

dependencies {
  // the engine exports pretty much all of it's dependencies to api. This is because
  // other RiskScape plugins make use of these deps themselves.
  // TODO consider is this is something to work on.
  api project(':engine:api')
  implementation project(':engine:i18n')
  api project(':engine:common')
  testImplementation project(':engine:api').sourceSets.test.output
  testImplementation testFixtures(project(':engine:common'))
  testImplementation project(':test:shared')
  testImplementation group: 'org.skyscreamer', name: 'jsonassert', version: '1.5.0'
  api group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.4.21'
  api group: 'org.json', name: 'json', version: '20180130'
  api group: 'org.locationtech.jts.io', name: 'jts-io-common', version: project.property('jtsVersion')
  api group: 'net.sourceforge.javacsv', name: 'javacsv', version: '2.0'
  // https://mvnrepository.com/artifact/org.apache.commons/commons-math3
  api group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'

  api group: 'org.geotools', name: 'gt-main', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-shapefile', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-coverage', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-arcgrid', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-geotiff', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-wfs-ng', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-epsg-wkt', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-cql', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-csv', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-geopkg', version: project.property('geotoolsVersion')
  api group: 'org.geotools', name: 'gt-geojson', version: project.property('geotoolsVersion')
  api group: 'org.geotools.xsd', name: 'gt-xsd-kml', version: project.property('geotoolsVersion')
  implementation group: 'javax.media', name: 'jai_core', version: '1.1.3'
  //org.pacesys for swift resource loader, currently in seismic plugin
  api group: 'org.pacesys', name: 'openstack4j-core', version: '3.1.0'
  api group: 'org.pacesys.openstack4j.connectors', name: 'openstack4j-httpclient', version: '3.1.0'

  testImplementation group: 'org.geotools', name: 'gt-render', version: project.property('geotoolsVersion')

  // The postgres driver needs to live here, it can't be next to the postgis plugin
  // because of how the jdbc DriverManager works.  Even if the driver is available
  // in the classloader, the DriverManager checks that the caller of DriverManager#getDriver
  // is in the same classloader as the driver itself, which is isn't for us because the
  // connection pool library is in the engine's classloader.  I have no idea why it's
  // designed this way, but after the postgres-custom version of commons-dbcp
  // disappeared in favour of the engine's one, this issue reared its ugly head
  implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.10'
}

task geotoolsPatchesJar(type: Jar) {
  archiveFileName = 'riskscape-geotools-patches.jar'
  from("src/main/java") {
    include "org/geotools/**/*"
  }
}

task(buildProperties, dependsOn: 'processResources')  {
  outputs.upToDateWhen {
    // always update
    false
  }
  doLast {

    def riskscapeCommit
    // allow the build to succeed if git isn't installed - we run gradle inside
    // docker images sometimes, and they don't always have git installed
    if ("which git".execute().waitFor() == 0) {
      def proc = "git rev-parse HEAD".execute()
      def outputStream = new StringBuffer();
      proc.waitForProcessOutput(outputStream, System.err)
      riskscapeCommit = outputStream.toString()
    } else {
      riskscapeCommit = "Unknown"
    }

    new File(sourceSets.main.output.resourcesDir, "build.properties").text = """
Riskscape-Version: $riskscapeVersion
Riskscape-Commit-Info: $riskscapeCommit
Buildtime: ${DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(OffsetDateTime.now())}
"""
  }
}
jar.dependsOn('buildProperties')
// get it to happen at the same time
jar.dependsOn('geotoolsPatchesJar')
