#!/usr/bin/make -f

include /usr/share/quilt/quilt.make

DEB_BUILD_ARCH  ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

UPSTREAM_VERSION := $(shell dpkg-parsechangelog | sed -n 's/^Version: *\(.*\)-.*$$/\1/ p' | sed -e 's/~.*//')
MOD_MAJOR_VERSION := $(word 1, $(subst ., ,$(UPSTREAM_VERSION)))
MOD_MINOR_VERSION := $(word 2, $(subst ., ,$(UPSTREAM_VERSION)))
MOD_PATCH_VERSION := $(word 3, $(subst ., ,$(UPSTREAM_VERSION)))

export USE_64 := $(shell echo "int main(void) { int assert[(sizeof(long) == 8) ? 1 : -1]; return 0; }" | gcc -o /dev/null -x c - 2> /dev/null && echo "USE_64=1")

CFLAGS := -Wall -pipe -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2 -fno-strict-aliasing
endif

CFLAGS += $(USE_64)
# Enable compiler optimizations and disable debugging code
BUILD_OPT=1
export BUILD_OPT
export XCFLAGS="-g"
BASEDIR := $(CURDIR)
export DEBIAN_BUILD=1
export SOURCE_PREFIX=$(BASEDIR)/dist
export PKG_CONFIG_ALLOW_SYSTEM_LIBS=1
export PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1
export NSPR_INCLUDE_DIR=`/usr/bin/pkg-config --cflags-only-I nspr | sed 's/-I//'`
export NSPR_LIB_DIR=`/usr/bin/pkg-config --libs-only-L nspr | sed 's/-L//'`
export NSS_INCLUDE_DIR=`/usr/bin/pkg-config --cflags-only-I nss | sed 's/-I//'`
export NSS_LIB_DIR=`/usr/bin/pkg-config --libs-only-L nss | sed 's/-L//'`
export USE_INSTALLED_NSPR=1
export USE_INSTALLED_NSS=1

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-$(DEB_BUILD_ARCH)

# Generate symbolic info for debuggers
XCFLAGS="-g"
export XCFLAGS
export CFLAGS

override_dh_auto_build:
	make -C coreconf $(USE_64)
	make $(USE_64)

override_dh_auto_clean:

override_dh_clean:
	-[ -f $(BASEDIR)/Makefile ] || $(MAKE) -C $(BASEDIR) clean
	-[ -f $(BASEDIR)/coreconf/Makefile ] || $(MAKE) -C $(BASEDIR)/coreconf clean
	-find . -name "*.class" -type f -exec rm -f "{}" \;
	-find . -name "*.so.*" -type f -exec rm -f "{}" \;
	-find . -name ".so" -type f -exec rm -f "{}" \;
	-find . -type d -name "Linux*_glibc_PTH_*DBG.OBJ" -exec rm -rf "{}" \;
	-find . -type d -name "Linux*_glibc_PTH_*OPT.OBJ" -exec rm -rf "{}" \;
	rm -f $(BASEDIR)/org/mozilla/jss/util/Debug.java
	rm -rf $(CURDIR)/dist || true
	dh_clean

override_dh_auto_install:
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/jss
	mkdir -p $(CURDIR)/debian/tmp/usr/share/java
	install -m 644 dist/xpclass.jar $(CURDIR)/debian/tmp/usr/share/java/jss-$(MOD_MAJOR_VERSION).$(MOD_MINOR_VERSION).$(MOD_PATCH_VERSION).jar
	install -m 0755 dist/Linux*.OBJ/lib/libjss4.so $(CURDIR)/debian/tmp/usr/lib/jss

	# Required by ldapjdk
	ln -s jss-$(MOD_MAJOR_VERSION).$(MOD_MINOR_VERSION).$(MOD_PATCH_VERSION).jar \
		$(CURDIR)/debian/tmp/usr/share/java/jss.jar
	# Required by idm-console-framework
	ln -s jss-$(MOD_MAJOR_VERSION).$(MOD_MINOR_VERSION).$(MOD_PATCH_VERSION).jar \
		$(CURDIR)/debian/tmp/usr/share/java/jss4.jar

override_dh_auto_test:


# For maintainer use only, generate a tarball:
gentarball: UV=$(shell dpkg-parsechangelog|awk '/^Version:/ {print $$2}'|sed 's/-.*$$//')
gentarball:
	git archive --format=tar upstream --prefix=jss-$(UV)/ | \
		xz --best \
		> ../jss_$(UV).orig.tar.xz

%:
	dh $@ --with quilt
