#!/usr/bin/make -f

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export JAVA_HOME=/usr/lib/jvm/default-java
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
export CCACHE_DIR=$(CURDIR)/ccache

%:
	dh $@ --buildsystem=maven --with javahelper

override_dh_auto_build:
	# maven builds a jar that is missing Version.class
	# so use the one from the ant build
	ant -Duse.system.libffi=1 jar
	mkdir target
	mv dist/jffi.jar target/jffi-$(DEB_VERSION_UPSTREAM:+ds=).jar

override_dh_install-arch:
	# install -jni library in multi-arch path
	dh_install build/jni/*.so usr/lib/$(DEB_HOST_MULTIARCH)/jni

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	ant -Duse.system.libffi=1 archive-platform-jar test
endif

JAR=debian/libjffi-java/usr/share/java/jffi.jar
override_jh_manifest-indep:
	jh_manifest -plibjffi-java
	# generate OSGi metadata
	cat debian/jffi.bnd | sed s/\%VERSION\%/$(DEB_VERSION_UPSTREAM:+ds=)/ > debian/jffi.versioned.bnd
	-test -f $(JAR) && \
	bnd wrap --properties debian/jffi.versioned.bnd --output $(JAR).tmp $(JAR)
	mv $(JAR).tmp $(JAR)
	rm debian/jffi.versioned.bnd

override_dh_clean:
	ant clean
	dh_clean
