plugins {
	id "java"
}

ext {
	junitVersion = "5.11.4"
	testcontainersVersion = "1.21.3"
	assertjCoreVersion = "3.27.6"
}

repositories {
	mavenCentral()
}

group "org.adoptium"
version "1.0.0-SNAPSHOT"

/*
 * The versions list defines the Debian and Ubuntu versions that this package is uploaded to.
 *
 * If a version like sid is missing here, the package won't be included in the sid repository and therefore not
 * installable via apt. Never use suite names like testing or unstable. This list is usually bigger than the list
 * of versions we test with as defined by the class DebianFlavours in the source set packageTest.
 *
 * **Attention**: If you alter the list, check if the class DebianFlavours needs to be updated, too.
 */
def deb_versions = [
		"trixie", // Debian/13
		"bookworm", // Debian/12
		"bullseye", // Debian/11
		"noble",    // Ubuntu/24.04 (LTS)
		"jammy",    // Ubuntu/22.04 (LTS)
		"focal",    // Ubuntu/20.04 (LTS)
		"bionic",   // Ubuntu/18.04 (LTS)
]

java {
	sourceCompatibility = JavaVersion.VERSION_1_8
	targetCompatibility = JavaVersion.VERSION_1_8
}

sourceSets {
	packageTest {
		compileClasspath += sourceSets.main.output
		runtimeClasspath += sourceSets.main.output
	}
}

configurations {
	packageTestImplementation.extendsFrom implementation
	packageTestRuntimeOnly.extendsFrom runtimeOnly
}

dependencies {
	packageTestImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
	packageTestImplementation "org.testcontainers:testcontainers:$testcontainersVersion"
	packageTestImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
	packageTestImplementation "org.assertj:assertj-core:$assertjCoreVersion"
}

test {
	useJUnitPlatform()
	testLogging {
		events "passed", "skipped", "failed"
	}
}

task packageCaCertificatesDebian {
	dependsOn "assemble"

	group = "packaging"
	description = "Creates ca-certificate deb package for Debian flavours."

	def outputDir = new File(project.buildDir.absolutePath, "ospackage")
	outputs.dir(outputDir)

	// Get container registry property. If not set, return empty string that is equivalent to dockerhub
	def containerRegistry = getContainerRegistry()

	doLast {
		project.copy {
			from("src/main/packaging/")
			into("${buildDir}/generated/packaging")
		}
		project.exec {
			workingDir "src/main/packaging"
			commandLine "docker", "build", "--no-cache", "--pull",
				"-t", "adoptium-packages-linux-cacerts-debian",
				"--build-arg=CONTAINER_REGISTRY=${containerRegistry}",
				"-f", "Dockerfile",
				getProjectDir().absolutePath + "/src/main/packaging"
		}

		project.exec {
			workingDir getRootDir()
			commandLine "docker", "run", "--rm",
				"--mount", "type=bind,source=${buildDir},target=/home/builder/build",
				"--mount", "type=bind,source=${outputDir.absolutePath},target=/home/builder/out",
				"-e", "VERSIONS=${String.join(" ", deb_versions)}",
				"adoptium-packages-linux-cacerts-debian:latest"
		}
	}
}

task checkCaCertificatesDebian(type: Test) {
	dependsOn packageCaCertificatesDebian

	description = 'Tests the generated ca-certificate Debian packages.'
	group = 'verification'

	testClassesDirs = sourceSets.packageTest.output.classesDirs
	classpath = sourceSets.packageTest.runtimeClasspath

	def containerRegistry = getContainerRegistry()

	environment "PACKAGE", "adoptium-ca-certificates"

	if (containerRegistry != null && containerRegistry != "") {
		environment "containerRegistry", "$containerRegistry"
		println "Container registry set to $containerRegistry"
	}

	useJUnitPlatform()
	testLogging {
		events "passed", "skipped", "failed"
	}
}

parent.packageCaCertificates.dependsOn(packageCaCertificatesDebian)
parent.checkCaCertificatesPackage.dependsOn(checkCaCertificatesDebian)
