/*
 * 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".
 */

plugins {
  id "com.gradle.develocity" version "4.0.1"
}

// Include all subdirectories as example projects
rootDir.listFiles().findAll { it.directory && new File(it, 'build.gradle').exists() }.each { subDir ->
  include ":${subDir.name}"
}

gradle.rootProject {
  ext {
    // Fetch Elasticsearch version from outer build
    new File(rootDir.parentFile.parentFile, 'build-tools-internal/version.properties').withInputStream { is ->
      def props = new Properties()
      props.load(is)
      elasticsearchVersion = "${props.get('elasticsearch')}-SNAPSHOT"
      // for example plugins the pluginApiVersion is the same as ES version, but for real plugins it only has to match a major
      pluginApiVersion = "${props.get('elasticsearch')}-SNAPSHOT"
      log4jVersion = props.get('log4j')
      luceneVersion = props.get('lucene')
    }
  }
}

gradle.projectsEvaluated {
  if (gradle.includedBuilds) {
    gradle.allprojects {
      configurations.all {
        resolutionStrategy.dependencySubstitution {
          // When using composite builds we need to tell Gradle to use the project names since we rename the published artifacts
          substitute module('org.elasticsearch.plugin:elasticsearch-plugin-api') using module("org.elasticsearch.plugin:plugin-api:${elasticsearchVersion}")
          substitute module('org.elasticsearch.plugin:elasticsearch-plugin-analysis-api') using module("org.elasticsearch.plugin:plugin-analysis-api:${elasticsearchVersion}")
          substitute module('org.elasticsearch:elasticsearch-plugin-scanner') using module("org.elasticsearch:plugin-scanner:${elasticsearchVersion}")
          substitute module('org.elasticsearch:elasticsearch-core') using module("org.elasticsearch:core:${elasticsearchVersion}")
          substitute module('org.elasticsearch:elasticsearch') using module("org.elasticsearch:server:${elasticsearchVersion}")
          substitute module('org.elasticsearch.client:elasticsearch-rest-client') using module("org.elasticsearch.client:rest:${elasticsearchVersion}")
          substitute module('org.elasticsearch.plugin:x-pack-core') using module("org.elasticsearch.plugin:core:${elasticsearchVersion}")
          substitute module('org.elasticsearch.plugin:elasticsearch-scripting-painless-spi') using module("org.elasticsearch.plugin:spi:${elasticsearchVersion}")
          substitute module('org.elasticsearch.distribution.integ-test-zip:elasticsearch') using variant(module("org.elasticsearch.distribution.integ-test-zip:integ-test-zip:${elasticsearchVersion}")) {
            attributes {
              attribute(Attribute.of("composite", Boolean.class), true)
            }
          }
          substitute module('elasticsearch-distribution-snapshot:elasticsearch') using variant(module("org.elasticsearch.distribution.default:${getDefaultDistroProjectName()}:${elasticsearchVersion}")) {
            attributes {
              attribute(Attribute.of("composite", Boolean.class), true)
            }
          }
        }
      }
    }
  }
}

/*
 * Determine which :distribution:archives project to use when resolving as part of a composite build.
 * We have to duplicate this somewhat, since the example plugins can't use InternalDistributionDownloadPlugin (this is intentional) which is where
 * that resolution logic lives.
 */
static def getDefaultDistroProjectName() {
  String os = System.getProperty("os.name", "")
  boolean isArm = System.getProperty("os.arch", "") == 'aarch64'
  if (os.startsWith("Windows")) {
    return 'windows-zip'
  } else if (os.startsWith("Linux") || os.startsWith("LINUX")) {
    return isArm ? 'linux-aarch64-tar' : 'linux-tar'
  } else if (os.startsWith("Mac")) {
    return isArm ? 'darwin-aarch64-tar' : 'darwin-tar'
  } else {
    throw new GradleException("Unable to determine system platform type.")
  }
}
