#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

KOTLIN_VERSION_TO_BUILD := $(firstword $(subst +, ,$(DEB_VERSION_UPSTREAM)))
KOTLIN_VERSION_INSTALLED := $(shell dpkg-query --showformat='$${Version}\n' -W kotlin)
KOTLIN_VERSION_TO_USE := $(firstword $(subst +, ,$(subst -, ,$(KOTLIN_VERSION_INSTALLED))))
COMMON_GRADLE_OPTIONS := -Dkotlin.compiler.execution.strategy=in-process

# The Gradle plugin markers (plugin id:artifact), upstream generates them in
# libraries/tools/gradle-tools/pluginMarkers.gradle from the pluginBundle block
# of the plugin-publish plugin, which isn't packaged
PLUGIN_MARKERS := $(addsuffix :kotlin-gradle-plugin,$(addprefix org.jetbrains.kotlin.,\
	android android.extensions jvm kapt multiplatform native.cocoapods \
	platform.android platform.common platform.js platform.jvm plugin.scripting)) \
	org.jetbrains.kotlin.plugin.sam.with.receiver:kotlin-sam-with-receiver
#COMMON_GRADLE_OPTIONS := -Dkotlin.compiler.execution.strategy=in-process \
#	"-Dorg.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 \
#	-Xbootclasspath/a:/usr/share/java/gradle-helper-hook.jar:/usr/share/java/maven-repo-helper.jar"

export JDK_16=/usr/lib/jvm/default-java

INTELLIJ_SDK_VERSION := $(shell sed -n 's/^versions\.intellijSdk=//p' gradle/versions.properties)
INTELLIJ_REPO  := dependencies/repo/kotlin.build
IDEA_LIB       := $(INTELLIJ_REPO)/ideaIC/$(INTELLIJ_SDK_VERSION)/artifacts/lib
INTELLIJ_CORE  := $(INTELLIJ_REPO)/intellij-core/$(INTELLIJ_SDK_VERSION)/artifacts
JPS_STANDALONE := $(INTELLIJ_REPO)/jps-standalone/$(INTELLIJ_SDK_VERSION)/artifacts

%:
	dh $(@:_build=build) --buildsystem=gradle --with-maven-repo-helper

# Allow retrying `debian/rules build` with an existing `build` subdir
.PHONY: build
build: _build

debian/kotlin.poms: debian/kotlin.poms.in debian/plugin-marker.pom.in
	sed -e "s|__VERSION__|$(KOTLIN_VERSION_TO_BUILD)|g" < $< > $@
	mkdir -p debian/markers
	for marker in $(PLUGIN_MARKERS); do \
	    sed -e "s|__ID__|$${marker%%:*}|g" -e "s|__ARTIFACT__|$${marker#*:}|g" -e "s|__VERSION__|$(KOTLIN_VERSION_TO_BUILD)|g" \
	        < debian/plugin-marker.pom.in > debian/markers/$${marker%%:*}.gradle.plugin.pom; \
	done

override_dh_auto_build: debian/kotlin.poms
	# Build Kotlin
	mkdir --parents $(IDEA_LIB) $(INTELLIJ_CORE) $(JPS_STANDALONE)
	cp -u /usr/share/java/jdom2-intellij.jar		$(IDEA_LIB)/jdom.jar
	cp -u /usr/share/java/jna.jar				$(IDEA_LIB)/jna.jar
	cp -u /usr/share/java/jna-platform.jar			$(IDEA_LIB)/jna-platform.jar
	cp -u /usr/share/java/log4j-1.2.jar			$(IDEA_LIB)/log4j.jar
	cp -u /usr/share/java/oro.jar				$(IDEA_LIB)/oro-2.0.8.jar
	cp -u /usr/share/java/picocontainer-1.3.jar		$(IDEA_LIB)/picocontainer-1.2.jar
	cp -u /usr/share/java/libtrove-intellij.jar		$(IDEA_LIB)/trove4j.jar
	cp -u /usr/share/java/intellij-java-compatibility.jar	$(INTELLIJ_CORE)/java-compatibility-1.0.1.jar
	cp -u /usr/share/java/streamex.jar			$(IDEA_LIB)/streamex-0.6.7.jar
	cp -u /usr/share/java/lz4-java.jar			$(IDEA_LIB)/lz4-1.3.0.jar
	cp -u /usr/share/java/guava.jar				$(IDEA_LIB)/guava-25.1-jre.jar
	cp -u /usr/share/java/intellij-util-all.jar		$(IDEA_LIB)/util.jar
	cp -u /usr/share/java/intellij-jps-model-all.jar	$(JPS_STANDALONE)/jps-model.jar
	cp -u /usr/share/java/intellij-extensions.jar		$(IDEA_LIB)/extensions.jar
	cp -u /usr/share/java/intellij-platform-api.jar		$(IDEA_LIB)/platform-api.jar
	cp -u /usr/share/java/intellij-platform-impl.jar	$(IDEA_LIB)/platform-impl.jar
	cp -u /usr/share/java/intellij-core.jar			$(INTELLIJ_CORE)/intellij-core.jar

	ant -f ./debian/buildprotobufkotlin.xml -Dintellij.sdk.version=$(INTELLIJ_SDK_VERSION)
	cp ./debian/protobuf-debian.pom ./debian/maven-repo-cache/org/jetbrains/kotlin/protobuf/protobuf/debian/protobuf-debian.pom
	# compileKotlin is skipped, but Gradle 5 requires its output directory for compileJava
	mkdir -p plugins/android-extensions/android-extensions-compiler/build/classes/kotlin/main
	dh_auto_build -- $(COMMON_GRADLE_OPTIONS) \
	    -PkotlinVersionToBuild=$(KOTLIN_VERSION_TO_BUILD) \
	    -PbuildSrc.kotlin.version=$(KOTLIN_VERSION_TO_USE) \
	    -Pbootstrap.kotlin.version=$(KOTLIN_VERSION_TO_USE) \
	    -PkotlinxMetadataDeployVersion=0.0.4 \
	    dist install \
	    -x :kotlin-android-extensions-runtime:compileKotlin \
	    -x :plugins:android-extensions-compiler:compileKotlin

	ln -sfT $$(echo .gradle/daemon/*/.m2) .m2

	dh_auto_configure --buildsystem=maven
	# The Gradle build installs its artifacts into ./.m2, add them to the local
	# repository of the Maven build, in place of the links to the installed
	# package when it has the version being built
	rm -rf debian/maven-repo/org/jetbrains/kotlin/*/$(KOTLIN_VERSION_TO_BUILD)
	cp -rsf $(CURDIR)/.m2/repository/org/jetbrains/kotlin debian/maven-repo/org/jetbrains/
	# The Maven plugins of the compiler are compiled with kotlin-maven-plugin,
	# and Maven resolves a plugin from the repository and not from the reactor
	dh_auto_build --buildsystem=maven -- --file=libraries/pom.xml --projects=tools/kotlin-maven-plugin install
	dh_auto_build --buildsystem=maven -- --file=libraries/pom.xml package

override_dh_auto_clean:

execute_before_dh_clean: debian/kotlin.poms
	# Compensate for not running clean on potential left outs.
	# .gitignore on upstream project has 'build/' as a pattern, so this should be safe.
	find buildSrc -name 'build' -and -type d | xargs rm -rf
	# Compensate for not being able to run maven with 'clean' as goal.
	find libraries -name target -and -type d | xargs rm -rf
	# Undo a file edit done by the Kotlin build system but not cleaned
	sed --in-place --regexp-extended --expression='s|(val CURRENT: KotlinVersion = KotlinVersion)\(([[:digit:]]+), ([[:digit:]]+), ([[:digit:]]+)\)|\1(\2, \3, 0)|' libraries/stdlib/src/kotlin/util/KotlinVersion.kt
	mh_unpatchpoms --package=kotlin
	rm -rf ./debian/kotlin.poms

execute_before_dh_install:
	mh_install
	mh_install --rules=debian/maven.rules.debian

override_dh_auto_test:
