import static org.opendatakit.aggregate.gradle.Util.setPropertiesValue

String buildTarget = "${buildDir}/docker-compose".toString()

task dockerComposeClean() {
  delete "${buildTarget}"
}

task dockerComposeBuild(dependsOn: [build, dockerComposeClean], type: Copy) {
  into "${buildTarget}"
  from fileTree(dir: 'docker-compose')

  into('webapps/ROOT') {
    from zipTree("${buildDir}/libs/${archivesBaseName}-${version}.war")
  }

  doLast {
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/jdbc.properties", "jdbc.url", "jdbc:postgresql://db/aggregate?autoDeserialize=true")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/jdbc.properties", "jdbc.username", "aggregate")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/jdbc.properties", "jdbc.password", "aggregate")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/jdbc.properties", "jdbc.schema", "aggregate")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.realm.realmString", "ODK Aggregate")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.channelType", "ANY_CHANNEL")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.superUserUsername", "administrator")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.superUser", "")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.port", "80")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.securePort", "443")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.deviceAuthentication", "digest")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.hostname", "")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.server.secureChannelType", "ANY_CHANNEL")
    setPropertiesValue("${buildTarget}/webapps/ROOT/WEB-INF/classes/security.properties", "security.help.about", "auto-generated on 1970-01-01T00:00:00+0000 for ODK Aggregate")
    println ""
    println "Your Docker Compose build for Aggregate is in build/docker-compose."
    println "Please, refer to https://github.com/getodk/aggregate/tree/master/docs/build-and-run-with-docker-compose.md for instructions."
  }
}
