#!/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 --gradle-user-home .gradlehome --stacktrace

# 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 javahelper,jh_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/tmp | xargs echo | sed -e 's^build/tmp^build^g' | xargs rm -Rf
	rm -rf .gradle .gradlehome buildSrc/.gradle buildSrc/build build
	rm -rf debian/gradle.1
	rm -rf 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

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
