#!/usr/bin/make -f

export LC_ALL=C.UTF-8
export JAVA_HOME=/usr/lib/jvm/default-java

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

PYTHON3S:=$(shell py3versions -vr)

%:
	dh $@ --with javahelper --with python3

# The configure step is handled through Maven for -arch and -indep builds.
# It has to be run only once.
override_dh_auto_configure-arch override_dh_auto_configure-indep:
	if [ ! -f debian/mavenConfigureDone ]; then \
	        /usr/share/maven-debian-helper/copy-repo.sh $$(readlink -f debian) && \
	        mh_patchpoms -plibzookeeper-java --debian-build --keep-pom-version --maven-repo=$$(readlink -f debian/maven-repo) && \
	        touch debian/mavenConfigureDone; \
	fi

override_dh_auto_build-indep:
	dh_auto_build -- package javadoc:jar javadoc:aggregate -Pfull-build

# Running the Java build first, as it also builds the C++ part.
override_dh_auto_build-arch: override_dh_auto_build-indep
	# Build Python bindings in addition
	cd zookeeper-contrib/zookeeper-contrib-zkpython && \
		set -e ; set -x ; for i in $(PYTHON3S) ; do \
			python$$i src/python/setup.py build --build-base=$(CURDIR)/build ; \
		done

# Empty override: no need to call the Maven stuff for the arch-dependent packages
override_dh_auto_install-arch:

override_dh_auto_install-indep:
	dh_auto_install -i -- -Pfull-build

override_dh_install-arch:
	# Installing the Python artifacts in debian/tmp before calling dh_install
	cd zookeeper-contrib/zookeeper-contrib-zkpython && \
		set -e ; set -x ; for i in $(PYTHON3S) ; do \
			python$$i src/python/setup.py build --build-base=$(CURDIR)/build \
				install --root=$(CURDIR)/debian/tmp --install-layout=deb ;\
		done

	dh_install -a

override_dh_install-indep:
	dh_install -i
	# Also installing the configuration.xsl and zoo.cfg which are in conf/.
	cp conf/configuration.xsl debian/zookeeper/etc/zookeeper/conf_example/
	cp conf/zoo_sample.cfg debian/zookeeper/etc/zookeeper/conf_example/zoo.cfg

# Empty override for the -arch part, as we currently run no tests othen than
# the Java ones. See the commented block below for some perspective.
override_dh_auto_test-arch:

# In former versions a test could be run for the -arch part. To be reworked.
#ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
## Limit architectures which execute tests as some
## non x86 archs can be a bit racey.
#TEST_ARCH=i386 amd64
#ifneq (,$(findstring $(DEB_BUILD_ARCH), $(TEST_ARCH)))
#TEST_JARS=jline log4j-1.2 xercesImpl xmlParserAPIs netty slf4j-api slf4j-log4j12
#override_dh_auto_test-arch:
#	# C testing starts/stops zookeeper
#	# this ensures that all the right classes are found
#	for jar in $(TEST_JARS); do \
#		ln -sf /usr/share/java/$$jar.jar build/lib/$$jar.jar; \
#	done;
#	# Execute multi-threaded test suite
#	# Disable on Ubuntu and Debian due to glibc 2.17 incompatibility
#	# https://issues.apache.org/jira/browse/ZOOKEEPER-1646
#	if ! dpkg-vendor --derives-from debian; then \
#		$(MAKE) -C src/c zktest-mt; \
#		cd src/c && ./zktest-mt; \
#	fi;
#endif
#endif

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build
	find . -name "*.jar" -delete
	-rm zookeeper-client/zookeeper-client-c/generated/*
	[ ! -f zookeeper-client/zookeeper-client-c/Makefile ] || $(MAKE) -C zookeeper-client/zookeeper-client-c distclean
	-rm -rf zookeeper-client/zookeeper-client-c/Makefile.in
	-rm -rf zookeeper-client/zookeeper-client-c/aclocal.m4
	-rm -rf zookeeper-client/zookeeper-client-c/autom4te.cache
	-rm -rf zookeeper-client/zookeeper-client-c/compile
	-rm -rf zookeeper-client/zookeeper-client-c/config.guess
	-rm -rf zookeeper-client/zookeeper-client-c/config.sub
	-rm -rf zookeeper-client/zookeeper-client-c/configure
	-rm -rf zookeeper-client/zookeeper-client-c/ltmain.sh
	-rm -rf zookeeper-client/zookeeper-client-c/TEST-*
	-rm debian/mavenConfigureDone

override_dh_installinit:
	dh_installinit --name=zookeeper
