import org.elasticsearch.gradle.MavenFilteringHack
import org.elasticsearch.gradle.VersionProperties

apply plugin: 'elasticsearch.build'

dependencies {
  provided "org.elasticsearch:elasticsearch:${versions.elasticsearch}"
  provided project(path: ':x-pack-elasticsearch:plugin', configuration: 'runtime')

  testCompile "org.elasticsearch.test:framework:${project.versions.elasticsearch}"
  testCompile project(path: ':x-pack-elasticsearch:transport-client', configuration: 'runtime')
}

Map generateSubstitutions() {
  def stringSnap = { version ->
    if (version.endsWith("-SNAPSHOT")) {
      return version.substring(0, version.length() - 9)
    }
    return version
  }
  return [
    'version': stringSnap(version),
    'xpack.version': stringSnap(VersionProperties.elasticsearch),
    'java.version': targetCompatibility as String
  ]
}

String outputDir = "generated-resources/${project.name}"
task copyXPackPluginProps(type: Copy) {
  from project(':x-pack-elasticsearch:plugin').file('src/main/plugin-metadata')
  from project(':x-pack-elasticsearch:plugin').tasks.pluginProperties
  into outputDir
}
project.sourceSets.test.output.dir(outputDir, builtBy: copyXPackPluginProps)

processResources {
  MavenFilteringHack.filter(it, generateSubstitutions())
}

task buildZip(type:Zip, dependsOn: [jar]) {
  from 'build/resources/main/x-pack-extension-descriptor.properties'
  from 'build/resources/main/x-pack-extension-security.policy'
  from project.jar
}

task integTest(type: org.elasticsearch.gradle.test.RestIntegTestTask) {
  mustRunAfter precommit
}

integTestRunner {
  systemProperty 'tests.security.manager', 'false'
}
integTestCluster {
  dependsOn buildZip
  plugin ':x-pack-elasticsearch:plugin'
  setting 'xpack.security.authc.realms.custom.order', '0'
  setting 'xpack.security.authc.realms.custom.type', 'custom'
  setting 'xpack.security.authc.realms.custom.filtered_setting', 'should be filtered'
  setting 'xpack.security.authc.realms.esusers.order', '1'
  setting 'xpack.security.authc.realms.esusers.type', 'file'
  setting 'xpack.security.authc.realms.native.type', 'native'
  setting 'xpack.security.authc.realms.native.order', '2'
  setting 'xpack.ml.enabled', 'false'

  // This is important, so that all the modules are available too.
  // There are index templates that use token filters that are in analysis-module and
  // processors are being used that are in ingest-common module.
  distribution = 'zip'

  setupCommand 'setupDummyUser',
               'bin/x-pack/users', 'useradd', 'test_user', '-p', 'x-pack-test-password', '-r', 'superuser'
  setupCommand 'installExtension',
                'bin/x-pack/extension', 'install', 'file:' + buildZip.archivePath
  waitCondition = { node, ant ->
    File tmpFile = new File(node.cwd, 'wait.success')
    ant.get(src: "http://${node.httpUri()}/_cluster/health?wait_for_nodes=>=${numNodes}&wait_for_status=yellow",
            dest: tmpFile.toString(),
            username: 'test_user',
            password: 'x-pack-test-password',
            ignoreerrors: true,
            retries: 10)
    return tmpFile.exists()
  }
}
check.dependsOn integTest