import java.time.Duration

apply plugin: 'maven-publish'
apply plugin: 'signing'

apply plugin: 'de.marcphilipp.nexus-publish'

nexusPublishing {
  repositories {
    sonatype()
  }

  connectTimeout = Duration.ofMinutes(5)
  clientTimeout = Duration.ofMinutes(5)
}

publishing {
  publications {
    maven(MavenPublication) {
      if (project.tasks.findByName("shadowJar") != null && !findProperty('noShadowPublish')) {
        project.shadow.component(it)
        //These two are here just to satisfy Maven Central
        artifact sourcesJar
        artifact javadocJar
      } else {
        project.plugins.withId("java-platform") {
          from(components["javaPlatform"])
        }
        project.plugins.withId("java-library") {
          from components.java
        }
      }

      def stable = findProperty("otel.stable") ?: false
      if (!stable) {
        def versionParts = version.split('-')
        versionParts[0] += '-alpha'
        version = versionParts.join('-')
      }

      afterEvaluate {
        def mavenGroupId = project.findProperty('mavenGroupId')
        if (mavenGroupId) {
          groupId = mavenGroupId
        }
        artifactId = artifactPrefix(project, archivesBaseName) + archivesBaseName

        if (!groupId.startsWith("io.opentelemetry.")) {
          throw new GradleException("groupId is not set for this project or its parent $project.parent")
        }
      }

      pom {
        name = 'OpenTelemetry Instrumentation for Java'
        url = 'https://github.com/open-telemetry/opentelemetry-java-instrumentation'

        licenses {
          license {
            name = 'The Apache License, Version 2.0'
            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
          }
        }

        developers {
          developer {
            id = 'opentelemetry'
            name = 'OpenTelemetry'
            url = 'https://github.com/open-telemetry/opentelemetry-java-instrumentation/discussions'
          }
        }

        scm {
          connection = 'scm:git:git@github.com:open-telemetry/opentelemetry-java-instrumentation.git'
          developerConnection = 'scm:git:git@github.com:open-telemetry/opentelemetry-java-instrumentation.git'
          url = 'git@github.com:open-telemetry/opentelemetry-java-instrumentation.git'
        }

        afterEvaluate {
          // description is not available until evaluated.
          description = project.description ?: 'Instrumentation of Java libraries using OpenTelemetry.'
        }
      }

    }
  }
}

private String artifactPrefix(Project p, String archivesBaseName) {
  if (archivesBaseName.startsWith("opentelemetry")) {
    return ''
  }
  if (p.name.startsWith("opentelemetry")) {
    return ''
  }
  if (p.name.startsWith("javaagent")) {
    return 'opentelemetry-'
  }
  if (p.group == 'io.opentelemetry.javaagent.instrumentation') {
    return 'opentelemetry-javaagent-'
  }
  return 'opentelemetry-'
}

rootProject.tasks.release.finalizedBy tasks.publishToSonatype
rootProject.tasks.closeAndReleaseRepository.mustRunAfter tasks.publishToSonatype

tasks.withType(Sign).configureEach {
  onlyIf { System.getenv("CI") != null }
}

signing {
  useInMemoryPgpKeys(System.getenv("GPG_PRIVATE_KEY"), System.getenv("GPG_PASSWORD"))
  sign publishing.publications.maven
}
