import org.elasticsearch.gradle.OS
import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.VersionProperties
import org.elasticsearch.gradle.internal.BwcVersions
import org.elasticsearch.gradle.internal.JarApiComparisonTask

import static org.elasticsearch.gradle.internal.InternalDistributionBwcSetupPlugin.buildBwcTaskName

configurations {
  newJar {
    transitive = false
  }
}

dependencies {
  newJar project(":libs:${project.name}")
}

buildParams.bwcVersions.withIndexCompatible({ it.onOrAfter(Version.fromString(ext.stableApiSince))
  && it != VersionProperties.elasticsearchVersion
}) { bwcVersion, baseName ->

  BwcVersions.UnreleasedVersionInfo unreleasedVersion = buildParams.bwcVersions.unreleasedInfo(bwcVersion)

  configurations {
    "oldJar${baseName}" {
      transitive = false
    }
  }

  dependencies {
    if (unreleasedVersion) {
      // For unreleased snapshot versions, build them from source
      "oldJar${baseName}"(files(project(unreleasedVersion.gradleProjectPath).tasks.named(buildBwcTaskName(project.name))))
    } else if(bwcVersion.onOrAfter('8.7.0') && project.name.endsWith("logging")==false) {
      //there was a package rename in 8.7.0, except for es-logging
      "oldJar${baseName}"("org.elasticsearch.plugin:elasticsearch-${project.name}:${bwcVersion}")
    } else {
      // For released versions, download it
      "oldJar${baseName}"("org.elasticsearch:elasticsearch-${project.name}:${bwcVersion}")
    }
  }

  def jarApiComparisonTask = tasks.register(bwcTaskName(bwcVersion), JarApiComparisonTask) {
    oldJar = configurations."oldJar${baseName}"
    newJar = configurations.newJar
  }

  jarApiComparisonTask.configure {
    onlyIf("OS != windows") {
      OS.current() != OS.WINDOWS
    }
  }
}
