import org.elasticsearch.gradle.plugin.PluginBuildPlugin
import org.elasticsearch.gradle.internal.conventions.precommit.LicenseHeadersTask

Project xpackRootProject = project

subprojects {

  // We define a specific repository for opensaml since the shibboleth project doesn't publish to maven central and the
  // artifacts that are located there are not curated/updated by the project
  // see: https://wiki.shibboleth.net/confluence/display/DEV/Use+of+Maven+Central
  repositories {
    maven {
      name = "opensaml"
      url = "https://artifactory.elstc.co/artifactory/shibboleth-releases/"
      content {
        includeGroup "org.opensaml"
        includeGroup "net.shibboleth.utilities"
        includeGroup "net.shibboleth"
      }
    }
  }

  group = xpackProjectGroup(path)

  // helper method to find the path to a module
  ext.xpackModule = { String moduleName -> ":x-pack:plugin:${moduleName}" }

  plugins.withType(PluginBuildPlugin).whenPluginAdded {
    project.esplugin.licenseFile = layout.settingsDirectory.file('licenses/ELASTIC-LICENSE-2.0.txt').asFile
    project.esplugin.noticeFile = xpackRootProject.file('NOTICE.txt')
  }

  tasks.withType(LicenseHeadersTask.class).configureEach {
    approvedLicenses = ['Elastic License 2.0', 'Generated', 'Vendored']
    additionalLicense 'ELAST', 'Elastic License 2.0', '2.0; you may not use this file except in compliance with the Elastic License'
  }

  project.pluginManager.withPlugin("elasticsearch.licensing") {
    ext.projectLicenses.set(['Elastic License 2.0': ext.elasticLicenseUrl])
  }

  project.pluginManager.withPlugin("elasticsearch.build") {
    project.ext.licenseFile.set(layout.settingsDirectory.file('licenses/ELASTIC-LICENSE-2.0.txt').asFile)
    project.ext.noticeFile.set(xpackRootProject.file('NOTICE.txt'))
  }
}

// helper method to calculate unique group id for qa projects and
// use org.elasticsearch.plugin for all productive projects
// generates e.g. for path `x-pack:plugin:ccr:qa:rest` the project group id `org.elasticsearch.plugin.ccr.qa`
def xpackProjectGroup(String path) {
  return path.contains(":qa:") ?
    'org.elasticsearch' + path.substring(":x-pack".length(), path.lastIndexOf(":")).replace(":", ".") :
    'org.elasticsearch.plugin'
}

