/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the "Elastic License
 * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
 * Public License v 1"; you may not use this file except in compliance with, at
 * your election, the "Elastic License 2.0", the "GNU Affero General Public
 * License v3.0 only", or the "Server Side Public License, v 1".
 */

import org.elasticsearch.gradle.OS
import org.elasticsearch.gradle.internal.AbstractDependenciesTask

apply plugin: 'elasticsearch.internal-yaml-rest-test'
apply plugin: 'elasticsearch.yaml-rest-compat-test'
apply plugin: 'elasticsearch.internal-cluster-test'
apply plugin: 'elasticsearch.internal-test-artifact'

esplugin {
  description = 'Ingest processor that uses lookup geo data based on IP addresses using the MaxMind geo database'
  classname ='org.elasticsearch.ingest.geoip.IngestGeoIpPlugin'

  bundleSpec.from("${project.buildDir}/ingest-geoip") {
    into '/'
  }
}

tasks.named('internalClusterTestTestingConventions').configure {
  baseClass 'org.elasticsearch.ingest.geoip.AbstractGeoIpIT'
  baseClass 'org.elasticsearch.test.ESTestCase'
}

dependencies {
  implementation('com.maxmind.geoip2:geoip2:4.2.1')
  // geoip2 dependencies:
  runtimeOnly("com.fasterxml.jackson.core:jackson-annotations:${versions.jackson}")
  runtimeOnly("com.fasterxml.jackson.core:jackson-databind:${versions.jackson}")
  runtimeOnly("com.fasterxml.jackson.core:jackson-core:${versions.jackson}")
  // when you upgrade maxmind dependency, please also do so in libs/logstash-bridge/build.gradle
  // elastic_integration plugin embeds this module but references through logstash-bridge
  implementation('com.maxmind.db:maxmind-db:3.1.1')

  testImplementation 'org.elasticsearch:geolite2-databases:20191119'
  internalClusterTestImplementation project(':modules:reindex')
  internalClusterTestImplementation project(':test:fixtures:geoip-fixture')
  yamlRestTestImplementation project(':test:fixtures:geoip-fixture')

  clusterModules project(':modules:reindex')
}

restResources {
  restApi {
    include '_common', 'indices', 'index', 'cluster', 'nodes', 'get', 'ingest'
  }
}

tasks.withType(JavaCompile).configureEach {
  options.compilerArgs << '-Xlint:-classfile'
}

if (OS.current() == OS.WINDOWS) {
  tasks.named("test").configure {
    // Windows cannot cleanup database files properly unless it loads everything on heap.
    // See https://github.com/maxmind/MaxMind-DB-Reader-java#file-lock-on-windows for more information
    systemProperty 'es.geoip.load_db_on_heap', 'true'
  }
}

tasks.named("internalClusterTest") {
  onlyIf("OS != windows") { OS.current() != OS.WINDOWS }
}

tasks.named('forbiddenApisTest').configure {
  //we are using jdk-internal instead of jdk-non-portable to allow for com.sun.net.httpserver.* usage
  modifyBundledSignatures { bundledSignatures ->
    bundledSignatures -= 'jdk-non-portable'
    bundledSignatures += 'jdk-internal'
    bundledSignatures
  }
}

tasks.named("forbiddenPatterns").configure {
  exclude '**/*.mmdb'
}

tasks.withType(AbstractDependenciesTask).configureEach {
  mapping from: /geoip.*/, to: 'maxmind-geolite2-eula'
  mapping from: /maxmind-db.*/, to: 'maxmind-db-reader'
  mapping from: /jackson.*/, to: 'jackson'
}

tasks.named("dependencyLicenses").configure {
  ignoreFile 'elastic-geoip-database-service-agreement-LICENSE.txt'
}

artifacts {
  restTests(new File(projectDir, "src/yamlRestTest/resources/rest-api-spec/test"))
}

tasks.named("yamlRestCompatTestTransform").configure({ task ->
  task.skipTest("ingest_geoip/40_geoip_databases/Test adding, getting, and removing geoip databases",
    "get databases behavior began returning more results in 8.16")
})
