#!/usr/bin/make -f

export JAVA_HOME=/usr/lib/jvm/default-java/
export CLASSPATH=lib/*:build/classes:debian/_jh_build.zookeeper

PYVERS := $(shell pyversions -vr)

%:
	dh --with javahelper $@

override_dh_auto_build:
	javacc -OUTPUT_DIRECTORY=src/java/main/org/apache/jute/compiler/generated src/java/main/org/apache/jute/compiler/generated/rcc.jj
	# the upstream build file includes .java files in the jars. Let's create the jars ourselves.
	# see: https://issues.apache.org/jira/browse/ZOOKEEPER-537
	ant -propertyfile debian/ant.properties -buildfile debian/build-override.xml build-generated
	mkdir -p debian/_jh_build.zookeeper
	mv build/classes/org debian/_jh_build.zookeeper

	cd src/c && ./configure --prefix=/usr
	$(MAKE) -C src/c
	cd src/contrib/zkpython && \
	  for pv in $(PYVERS); do \
	    python$$pv src/python/setup.py build --build-base=$(CURDIR)/build ;\
	  done
	# the rest gets compiled by jh_build
	    # ant compile -Dinstall.dir=$(CURDIR)/debian/tmp -Dpython.exe=python$$pv; \

override_dh_install:
	$(MAKE) -C src/c install DESTDIR=$(CURDIR)/debian/tmp
	cd src/contrib/zkpython && \
	  for pv in $(PYVERS); do \
	    python$$pv src/python/setup.py build --build-base=$(CURDIR)/build \
	               install --root=$(CURDIR)/debian/tmp --install-layout=deb ;\
	  done
	dh_install

override_jh_build:
	jh_build --no-javadoc
	javadoc -link /usr/share/doc/default-jdk-doc/api/ -classpath ${CLASSPATH}:debian/lib -d debian/_jh_build.javadoc/api -quiet -sourcepath src/java/main:src/contrib/bookkeeper/src/java -subpackages org.apache.jute:org.apache.zookeeper:org.apache.bookkeeper

override_dh_auto_test:
	# src/c/tests/TestClient.cc:298: Assertion: equality assertion failed [Expected: -101, Actual  : -4]
	#ant -propertyfile debian/ant.properties -buildfile debian/build-override.xml test-core
	#Testsuite runs ~15 min on my machine. Seems too much for the Debian build daemons
	#ant -propertyfile debian/ant.properties -buildfile debian/build-override.xml test-core-java

override_dh_clean:
	dh_clean --exclude=src
	jh_clean

# the original build.xml deletes too much
override_dh_auto_clean:
	dh_testdir
	rm -rf .revision
	rm -rf build
	rm -rf src/c/generated
	rm -rf src/java/generated
	rm -rf src/java/main/org/apache/jute/compiler/generated/*.java
	rm -f zookeeper*.jar
	#The following line caused lintian tag debian-rules-ignores-make-clean-error
	#-$(MAKE) -C src/c distclean
	#replaced by
	[ ! -f src/c/Makefile ] || $(MAKE) -C src/c distclean
	rm -f src/c/config.log src/c/config.status

override_dh_installdocs:
	dh_installdocs --link-doc=libzookeeper-java \
	  -plibzookeeper-java -pzookeeper -pzookeeperd -plibzookeeper-java-doc
	dh_installdocs --link-doc=libzookeeper2 \
	  -plibzookeeper2 -plibzookeeper-dev -pzookeeper-bin -ppython-zookeeper

override_dh_installinit:
	dh_installinit -d

# jh_depends fails for me:
# - I can not specify, that I need a minimal version of log4j
# - It unnecesseraly pulls libjaxp1.3-java in Depends, although this is only a
#   transitiv dependency of libxerces2-java
override_jh_depends:
	true
