/*
 * Copyright (c) 2011-2026 VMware Inc. or its affiliates, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.temporal.ChronoField

if (project.hasProperty("releaserDryRun") && project.findProperty("releaserDryRun") != "false") {
	println "Adding MavenLocal() for benefit of releaser dry run"
	project.repositories {
		mavenLocal()
	}
}

//NOTE: this task is intended for single project builds with no submodules
task groupId(group: "releaser helpers", description: "output the group id of the root project") {
	doLast {
		println rootProject.group
	}
}

task copyReadme(type: Copy, group: "releaser helpers", description: "copies the README in preparation for search and replace") {
	from(rootProject.rootDir) {
		include "README.md"
	}
	into rootProject.layout.buildDirectory
}

task bumpVersionsInReadme(type: Copy, group: "releaser helpers", description: "replaces versions in README") {
	def oldVersion = rootProject.findProperty("oldVersion")
	def currentVersion = rootProject.findProperty("currentVersion")

	onlyIf { oldVersion != null && currentVersion != null }
	dependsOn copyReadme

	doLast {
		println "Will replace $oldVersion with $currentVersion"
	}
	from(rootProject.layout.buildDirectory) {
		include 'README.md'
	}
	into rootProject.rootDir
	filter { line -> line
			.replace(oldVersion, currentVersion)
	}
}

String getOrGenerateBuildNumber() {
	if (project.hasProperty("buildNumber")) {
		return project.findProperty("buildNumber")
	}
	def ciNumber = System.getenv("GITHUB_RUN_NUMBER")
	if (ciNumber != null) {
		return ciNumber
	}
	ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC)
	long secondsInDay = now.toEpochSecond() - ZonedDateTime.now(ZoneOffset.UTC).withSecond(0).withHour(0).withMinute(0).toEpochSecond()
	String buildNumber = "$version-${now.get(ChronoField.YEAR)}${now.get(ChronoField.MONTH_OF_YEAR).toString().padLeft(2,'0')}${now.get(ChronoField.DAY_OF_MONTH).toString().padLeft(2,'0')}@${secondsInDay}"
	println "No buildNumber set, generated: $buildNumber"
	return buildNumber
}

task printBuildNumber() {
	doLast {
		println getOrGenerateBuildNumber()
	}
}

if (project.hasProperty("artifactory_publish_password")) {
	configure(rootProject) { p ->
		apply plugin: "com.jfrog.artifactory"
		def buildNumber = getOrGenerateBuildNumber()
		artifactory {
			publish {
				contextUrl = "${artifactory_publish_contextUrl}"
				repository {
					repoKey = "${artifactory_publish_repoKey}"
					username = "${artifactory_publish_username}"
					password = "${artifactory_publish_password}"
				}
			}
			clientConfig.includeEnvVars = false
			buildInfo {
				setBuildName('Reactor - BOM')
				setBuildNumber(buildNumber)
				if (System.getenv("GITHUB_ACTIONS") == "true") {
					setBuildUrl(System.getenv("GITHUB_SERVER_URL") + "/" + System.getenv("GITHUB_REPOSITORY") + "/actions/runs/" + System.getenv("GITHUB_RUN_ID"))
					setVcsRevision(System.getenv("GITHUB_SHA"))
				}
			}
		}
	}
}
