#!/usr/bin/make -f

GRADLE_TASKS := assemble startScripts javadocAll groovydocAll samplesDocs
export GRADLE_VERSION = $(shell cat version.txt)

# Only for debug purpose
# export JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005

# Under fakeroot Gradle will show error while creating /root/.gradle, hence
# specifies the Gradle home location
GRADLE_FLAGS = --project-prop finalRelease=true --offline --stacktrace \
               --gradle-user-home debian/.gradlehome

# Support for DEB_BUILD_OPTIONS parallel=n
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMTHREADS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	GRADLE_FLAGS += --parallel --max-workers=$(NUMTHREADS)
endif

%:
	dh $@ --with maven_repo_helper

override_dh_auto_build:
	gradle $(GRADLE_TASKS) $(GRADLE_FLAGS)
	pandoc -s -o debian/gradle.1 debian/gradle.1.md

override_dh_auto_clean:
	dh_auto_clean
	find . -wholename .*build/generated-resources | xargs echo | sed -e 's^build/generated-resources^build^g' | xargs rm -Rf
	rm -r -f .gradle debian/.gradlehome buildSrc/.gradle buildSrc/build build
	rm -r -f debian/gradle.1 debian/*.pom

override_dh_install:
	dh_install
	# Remove lintian warning zero-byte-file-in-doc-directory
	rm debian/gradle-doc/usr/share/doc/gradle/groovydoc/org/gradle/api/distribution/Distribution.html
	rm debian/gradle-doc/usr/share/doc/gradle/groovydoc/org/gradle/api/initialization/Settings.html
	rm debian/gradle-doc/usr/share/doc/gradle/dsl/dsl-standalone.html
	rm debian/gradle-doc/usr/share/doc/gradle/dsl/sidebar.js

override_mh_install:
	mh_install
	# I have no idea why this exist, maven-repo-helper has so many bugs
	rm -r debian/libgradle-core-java/subprojects

get-orig-source:
	uscan --download-current-version --force-download --repack --compression xz

# since gradle build-depends on itself, a way to generate a deb from upstream
# binary distribution is provided with this target. With the resulting .deb,
# gradle can be rebuilt from source.
bootstrap:
	cd $(dir $(firstword $(MAKEFILE_LIST)))../ && \
	./debian/bootstrap.sh
