#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

DH_BUILD_MAINT_OPTIONS := nocheck

# DH_VERBOSE := 1

include /usr/share/dpkg/default.mk

OS := $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
CPU := $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
# since build-results are not ending up in the proper dir its saver to use "*" instead of "$(CPU)"
SODIR := $(DEB_SOURCE)/$(DEB_SOURCE)/$(OS)/gcc
VDB_ARCH := $(word 1, $(subst -, ,$(DEB_HOST_MULTIARCH)))
LIBINSTALLDIR := debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/$(VDB_ARCH)/dbg/lib
LIBPKG := libncbi-vdb2
SCHEMADIR := debian/$(LIBPKG)/usr/lib/$(DEB_SOURCE)
DSLMFLAGS := --commit --multiarch --devunversioned --exclude-la \
             --override s/libmbedx509-0-dev/libmbedtls-dev/

# See debian/patches/fix-linking
# export DEB_LDFLAGS_MAINT_APPEND := -Wl,-z,defs -Wl,--as-needed
export DEB_LDFLAGS_MAINT_APPEND := -Wl,--as-needed

%:
	dh $@

override_dh_auto_clean:
	if [ -e ngs-sdk/Makefile.config.$(OS).$(CPU) ] ; then \
		dh_auto_clean ; \
	fi
	rm -rf $(DEB_SOURCE)/$(MULTIARCH)
	rm -f libs/ngs-jni/jni.h
	rm -f libs/ngs-jni/jni_md.h
	rm -f build/ld.linux.exe_cmd.sh

override_dh_auto_configure:
	# that's no standard configure script lacking support of default options
	# The build system shamelessly ignores those options like
	#    --with-ngs-java-prefix=/usr/share/java
	# so we need to go with patches.  The options are specified anyway.
	ln -s /usr/lib/jvm/default-java/include/jni.h		libs/ngs-jni
	ln -s /usr/lib/jvm/default-java/include/linux/jni_md.h	libs/ngs-jni
	./configure --build=$(MULTIARCH) --prefix=/usr --build-prefix=$(CURDIR)/debian/tmp/usr --with-ngs-sdk-prefix=/usr --with-debug --with-ngs-java-prefix=/usr/share/java
	# no idea how to convince configure script to use the correct include dir so patching result afterwards
	sed -i 's?HDF5_INCDIR = /usr/include?&/hdf5/serial?' build/Makefile.config*

override_dh_auto_build:
	dh_auto_build --no-parallel
	# we need to make really sure that no 'vdb_'-prefixed functions will remain ...
	if find . \( -name "*.so" -o -name "*.a" \) -exec strings \{\} \; | tee | grep -q vdb_mbedtls ; then \
	    find . \( -name "*.so" -o -name "*.a" \) -exec strings \{\} \; | tee | grep vdb_mbedtls ; \
	    grep -Rl vdb_mbedtls  debian/tmp/* ; \
	    exit 1 ; \
	    false ; \
	fi

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# FIXME: some tests keep on failing despite debian/patches/skip_failing_test.patch - needs to be sorted out later
	dh_auto_test || true
else
	echo "Skip testing which takes a long time"
endif

override_dh_prep:
	dh_prep -Xdebian/tmp

override_dh_auto_install:
	dh_auto_install
	find $(LIBINSTALLDIR) -name "*-static.a" -delete
	# upstream installs versioned *.a libs - we only want a single version
	for slib in $(LIBINSTALLDIR)/*.a ; do \
	    mv $(LIBINSTALLDIR)/`readlink $$slib` $$slib ; \
	    mv $(LIBINSTALLDIR)/`readlink $$slib` $$slib ; \
	done
	d-shlibmove $(DSLMFLAGS) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/lib/libncbi-ngs-c++.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libkapp.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libkapp-norsrc.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libkff.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libktst.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libkxfs.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libkxml.a usr/lib/$(MULTIARCH) \
                    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libload.a usr/lib/$(MULTIARCH) \
		    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libtui.a usr/lib/$(MULTIARCH) \
                    --movedev debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/ilib/libtui_cpp.a usr/lib/$(MULTIARCH) \
		    debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/lib/libncbi-vdb.so
	install -d debian/libncbi-vdb-dev/usr/include
	cp -a interfaces debian/libncbi-vdb-dev/usr/include/$(DEB_SOURCE)
	# json-response.h and services-priv.h are needed by sra-sdk, but
	# the latter shouldn't replace interfaces/vfs/services-priv.h.
	dh_install -plibncbi-vdb-dev libs/vfs/json-response.h usr/include/ncbi-vdb/vfs
	cp libs/vfs/services-priv.h debian/libncbi-vdb-dev/usr/include/ncbi-vdb/vfs/services-priv-internal.h
	d-shlibmove $(DSLMFLAGS) \
		    --override s/libhdf5_serial[0-9]*-dev/libhdf5-dev/ \
		    debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/lib/libkdf5.so
	d-shlibmove $(DSLMFLAGS) \
		    debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/lib/libncbi-wvdb.so
	d-shlibmove $(DSLMFLAGS) \
		    debian/tmp/usr/$(DEB_SOURCE)/$(OS)/gcc/*/dbg/lib/libvdb-sqlite.so
	find debian/lib* -name .gitignore -delete
	# move schemata from development packages to library packages since these are used in executables
	mkdir -p $(SCHEMADIR)
	set -x ; \
	for schema in `find debian/libncbi-vdb-dev/usr/include/$(DEB_SOURCE) -name "*.vschema"` ; do \
	    schemafile=`basename $${schema}` ; \
	    schemadir=`basename $$(dirname $${schema})` ; \
	    mkdir -p $(SCHEMADIR)/$${schemadir} ; \
	    sed -i "s:^include ':&/usr/lib/ncbi-vdb/:" $${schema} ; \
	    mv $${schema} $(SCHEMADIR)/$${schemadir} ; \
	done

override_dh_missing:
	dh_missing --fail-missing
