#!/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)))
VDB_ARCH := *
LIBINSTALLDIR := debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
SCHEMADIR := debian/ncbi-vdb-data/usr/lib/$(DEB_SOURCE)
DSLMFLAGS := --commit --multiarch --devunversioned --exclude-la

# 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 $@ --buildsystem=cmake

override_dh_auto_clean:
	if [ -e ngs-sdk/Makefile.config.$(OS).$(CPU) ] ; then \
		dh_auto_clean ; \
	fi
	rm -rf $(DEB_SOURCE)/$(MULTIARCH)
	rm -rf obj-*
	rm -rf libs/cipher/cipher-1.7/cipher/__pycache__
	rm -rf test/schema/actual
	rm -rf test/vdb/db/SparseCol*
	rm -rf test/vdb/db/VCursorCommit_BufferOverflow
	rm -f libs/ngs-jni/jni.h
	rm -f libs/ngs-jni/jni_md.h
	rm -f build/ld.linux.exe_cmd.sh
	rm -f debian/common-install-stamp
	rm -f interfaces/cc/gcc/aarch64
	rm -f test/cloud/cloud-kfg/.aws/c*
	rm -f test/kdb/root/sra/SRR600096.sra.cache
	rm -f test/kdb/user-settings.mkfg
	rm -f test/vdb/kfg/*/user-settings.mkfg
	rm -f test/vfs/caching-kfg/unix/user-settings.mkfg
	rm -f test/vfs/tmp.perm-file.tmp
	rm -f test/vfs/user-settings.mkfg
	rm -f test/vxf/local_config/user-settings.mkfg

override_dh_auto_configure-indep:;

override_dh_auto_build-arch:
	ln -s arm64 interfaces/cc/gcc/aarch64
	dh_auto_build --no-parallel -- NO_VDB3=1
	# 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_build-indep:;

override_dh_auto_test-arch:
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 -- NO_VDB3=1 || true
else
	echo "Skip testing which takes a long time"
endif

override_dh_auto_test-indep:;

override_dh_prep:
	dh_prep -Xdebian/tmp

debian/common-install-stamp:
	mkdir -p debian/libncbi-vdb-dev/usr/include/$(DEB_SOURCE)
	cp -a interfaces/* debian/libncbi-vdb-dev/usr/include/$(DEB_SOURCE)
	# move schemata from development packages to library packages since these are used in executables
	mkdir -p $(SCHEMADIR)
	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
	# Move around kfg files to the place where it used to be in version 2.x
	mv debian/libncbi-vdb-dev/usr/include/ncbi-vdb/kfg/ncbi $(SCHEMADIR)/kfg
	touch $@

override_dh_auto_install-arch: debian/common-install-stamp
	dh_auto_install -- NO_VDB3=1
	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
#		    --movedev $(LIBINSTALLDIR)/libncbi-ngs-c++.a usr/lib/$(MULTIARCH) \
#		    --movedev $(LIBINSTALLDIR)/libkff.a		usr/lib/$(MULTIARCH) \
#		    --movedev $(LIBINSTALLDIR)/libkxfs.a	usr/lib/$(MULTIARCH) \
#		    --movedev $(LIBINSTALLDIR)/libkxml.a	usr/lib/$(MULTIARCH) \
#                    --movedev $(LIBINSTALLDIR)/libload.a	usr/lib/$(MULTIARCH) \
#		    --movedev $(LIBINSTALLDIR)/libtui.a		usr/lib/$(MULTIARCH) \
#                    --movedev $(LIBINSTALLDIR)/libtui_cpp.a	usr/lib/$(MULTIARCH) \

	d-shlibmove $(DSLMFLAGS) \
		    --movedev $(LIBINSTALLDIR)/libkapp.a	usr/lib/$(MULTIARCH) \
		    --movedev $(LIBINSTALLDIR)/libktst.a	usr/lib/$(MULTIARCH) \
                    --movedev obj-*/ilib/libkapp-norsrc.a	usr/lib/$(MULTIARCH) \
		    $(LIBINSTALLDIR)/libncbi-vdb.so
	# 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) \
		    $(LIBINSTALLDIR)/libncbi-wvdb.so
	d-shlibmove $(DSLMFLAGS) \
		    $(LIBINSTALLDIR)/libsam-extract.so
	find debian/lib* -name .gitignore -delete

override_dh_auto_install-indep: debian/common-install-stamp
