plugins {
  id 'application'
  id 'checkstyle'
  id 'de.fuerstenau.buildconfig' version '1.1.8'
  id 'idea'
  id 'jacoco'
}

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

sourceSets {
  main {
    java {
      srcDirs = ['src']
    }
    resources {
      srcDirs = ['res']
    }
  }
  test {
    java {
      srcDirs = ['test/java']
    }
    resources {
      srcDirs = ['test/resources']
    }
  }
}

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

dependencies {
  compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
  compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
  compile group: 'commons-io', name: 'commons-io', version: '2.6'
  compile group: 'org.bushe', name: 'eventbus', version: '1.4'
  compile(group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.5.5') {
    exclude group: 'commons-logging'
  }
  compile group: 'net.sf.kxml', name: 'kxml2', version: '2.3.0'
  compile(group: 'org.getodk', name: 'javarosa', version: '2.17.2') {
    exclude group: 'org.slf4j'
  }
  compile group: 'org.hsqldb', name: 'hsqldb', version: '2.4.0'
  compile(group: 'com.brsanthu', name: 'google-analytics-java', version: '2.0.0') {
    exclude group: 'org.slf4j'
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
  }
  compile group: 'com.github.lgooddatepicker', name: 'LGoodDatePicker', version: '10.3.1'
  compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5'
  compile group: 'org.apache.httpcomponents', name: 'fluent-hc', version: '4.5.5'

  testCompile group: 'junit', name: 'junit', version: '4.12'
  testCompile('junit:junit:4.12') {
    exclude group: 'org.hamcrest'
  }
  testCompile 'org.hamcrest:hamcrest-library:1.3'
  testCompile 'com.github.npathai:hamcrest-optional:2.0.0'
  testCompile 'org.assertj:assertj-swing-junit:3.8.0' // Required for Swing tests
  testCompile "com.github.dreamhead:moco-core:0.12.0"

  compile files('lib/smallsql-0.21.jar')

  runtime group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
  compile group: 'org.slf4j', name: 'jcl-over-slf4j', version: '1.7.25'
  compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
  compile('io.sentry:sentry-logback:1.6.8') {
    exclude group: 'org.slf4j'
    exclude group: 'ch.qos.logback'
  }

  compile 'de.grundid.opendatalab:geojson-jackson:1.8'
}

jacocoTestReport {
  reports {
        xml.enabled = true
  }
}

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

ant.condition(property: 'os', value: 'windows') {
  os(family: 'windows')
}
ant.condition(property: 'os', value: 'unix') {
  os(family: 'unix')
}

// Use the result of git describe --tags --dirty as the version name
// From http://stackoverflow.com/questions/17097263#24121734
def getVersionName = { ->
  try {
    def stdout = new ByteArrayOutputStream()
    exec {
      switch (ant.properties.os) {
        case 'windows':
          commandLine 'cmd', '/c', 'git', 'describe', '--tags', '--dirty', '--always'
          break
        case 'unix':
          commandLine 'git', 'describe', '--tags', '--dirty', '--always'
          break
      }
      standardOutput = stdout
    }
    return stdout.toString().trim()
  }
  catch (ignored) {
    return null
  }
}

String getValue(String key, String defaultValue) {
  if (this.hasProperty(key))
    return this.getProperty(key)
  else
    return defaultValue
}

buildConfig {
  appName = 'ODK Briefcase'
  version = getVersionName()
  clsName = 'BuildConfig'
  packageName = 'org.opendatakit.briefcase.buildconfig'
  buildConfigField 'String', 'GOOGLE_TRACKING_ID', getValue("googleAnalytics.trackingId", "UA-91951913-3")
  buildConfigField 'Boolean', 'SENTRY_ENABLED', getValue("sentry.enabled", "false")
  buildConfigField 'String', 'SENTRY_DSN', getValue("sentry.dsn", "https://b6f023a5f17e44d9a46447f8827e2a41:1b1f33d68b1b4404b85cd627563d37f9@sentry.io/287258")
}

mainClassName = 'org.opendatakit.briefcase.Launcher'

jar {
  archiveBaseName.set buildConfig.appName
  version = buildConfig.version
  archiveFileName.set "ODK-Briefcase-${version}.jar"
  from configurations.runtime.collect { it }

  // A single-click Java app requires a flat jar or class loader.
  // Fat/Flat/uber/shadow jars won't work because BouncyCastle is signed.
  // Eclipse's Jar-in-Jar loader is the most maintained.
  // https://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/plain/org.eclipse.jdt.ui/jar-in-jar-loader.zip
  from zipTree('lib/jar-in-jar-loader.zip')

  manifest {
    attributes 'Main-Class': 'org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader'
    attributes 'Rsrc-Main-Class': mainClassName
    attributes 'Class-Path': '.'
    attributes 'Rsrc-Class-Path': './ ' + (configurations.runtime.collect { it.getName() }).join(' ')
  }
}

// Useful for testing
task explodedJar(type: Copy) {
  into '$buildDir/libs/$jar.baseName $jar.version'
  with jar
}
