#!/usr/bin/make -f

# Pregenerated with uuidgen -r
DEBIAN_GUID = 806b5c3b-0088-4732-8359-51630950d723

# +pie breaks the build
export DEB_BUILD_MAINT_OPTIONS = hardening=+all optimize=-lto

# Building against hdf5/1.14.4.3, forwarded: https://github.com/ncbi/sra-tools/issues/958
export DEB_CPPFLAGS_MAINT_APPEND = -DH5_USE_110_API

export DEB_CFLAGS_MAINT_APPEND   = -fsigned-char
export DEB_CXXFLAGS_MAINT_APPEND = -fsigned-char

export PIP_BREAK_SYSTEM_PACKAGES = 1

pkg := sra-toolkit

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

%:
	dh $@ --buildsystem=cmake \
	    --with python3,javahelper,jh_maven_repo_helper

OUTDIR=$(CURDIR)/debian/tmp

override_dh_auto_clean:
	dh_auto_clean
	rm -rf ngs/ngs-java/$(DEB_HOST_MULTIARCH)
	rm -rf ngs/ngs-bam/$(DEB_HOST_MULTIARCH)
	rm -f ngs/ngs-bam/Makefile.config
	rm -f ngs/ngs-bam/Makefile.config.install.linux.x86_64.prl
	rm -f ngs/ngs-bam/Makefile.config.linux.x86_64
	rm -f ngs/ngs-bam/reconfigure
	rm -rf ngs/ngs-sdk/ngs/unix/generic

	rm -rf ngs/ngs-sdk/$(DEB_HOST_MULTIARCH)
	rm -rf ngs/ngs-python/build ngs/ngs-python/dist
	rm -rf ngs/ngs-python/ngs.egg-info

	find test tools -name actual -type d | xargs -rt rm -rf
	rm -rf ngs/ngs-*/examples/actual/*
	rm -fv test/*/*/tmp.kfg
	rm -rf libs/general-writer/test/actual
	rm -rf libs/ngs-vdb/test/data/SparseFragmentBlobs
	rm -rf test/external/vdb-dump/db test/loaders/bam-load/out.sra
	rm -rf test/schema/test_*.db test/vdb/db
	find tools/loaders/general-loader/test -name input | xargs -rt rm -rf
	rm -f ngs/ngs-sdk/examples/actual.txt

	-rm -f debian/libngs-jni.links
	rm -rf .pybuild

override_dh_auto_configure:
	dh_auto_configure -- -DVDB_LIBDIR=/usr/lib/${DEB_HOST_MULTIARCH} \
	    -DVDB_INCDIR=/usr/include/ncbi-vdb -DBUILD_TOOLS_LOADERS=ON \
	    -DBUILD_TOOLS_INTERNAL=ON -DBUILD_TOOLS_TEST_TOOLS=ON \
	    -DCMAKE_SKIP_RPATH=ON -DTOOLS_ONLY=OFF \
	    -DCOMPILER_OPTION_SSE42_SUPPORTED=FALSE

KFG_INC  = /usr/include/ncbi-vdb/kfg/ncbi
KFG_DEST = debian/sra-toolkit/etc/ncbi
override_dh_auto_install:
	dh_auto_install
	# build/install.sh $(KFG_INC) \
	# 		 $(CURDIR)/tools/internal/vdb-copy \
	# 		 $(KFG_DEST) \
	# 		 $(KFG_DEST) \
	# 		 debian/sra-toolkit/usr/bin \
	# 		 debian/sra-toolkit/usr/lib/$(DEB_HOST_MULTIARCH) \
	# 		 $(CURDIR)/shared/kfgsums
	build/install-kfg.sh default.kfg $(KFG_INC) $(KFG_DEST) \
			     $(KFG_INC)/kfgsums
	build/install-kfg.sh certs.kfg $(KFG_INC) $(KFG_DEST) \
			     $(KFG_INC)/kfgsums
	build/install-kfg.sh vdb-copy.kfg $(CURDIR)/tools/internal/vdb-copy \
			     $(KFG_DEST) $(CURDIR)/shared/kfgsums
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-a \
		    --exclude-la \
		    --movedev "debian/*/usr/lib/$(DEB_HOST_MULTIARCH)/libncbi-ngs.a*" \
			usr/lib/$(DEB_HOST_MULTIARCH) \
		    --override "s/libncbi-vdb3-dev/libncbi-vdb-dev/" \
		    debian/*/usr/lib/$(DEB_HOST_MULTIARCH)/libncbi-ngs.so
# Ignore spurious failures from pluses; --ignorelibdep only partially helps.
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-a \
		    --exclude-la \
		    --movedev "debian/*/usr/lib/$(DEB_HOST_MULTIARCH)/lib*c++.a*" \
			usr/lib/$(DEB_HOST_MULTIARCH) \
		    --movedev "debian/tmp/usr/include/*" usr/include \
		    --ignorelibdep \
		    debian/*/usr/lib/$(DEB_HOST_MULTIARCH)/libngs-c++.so | \
		tee /dev/stderr | LC_ALL=C grep '^[a-z]' | sh

override_dh_install:
	# upstream installs versioned *.a libs - we only want a single version
	for d in debian/lib*-dev/usr/lib/$(DEB_HOST_MULTIARCH) ; do \
	    for slib in $$d/lib*.a ; do \
		mv $$d/`readlink $$slib` $$slib ; \
		mv $$d/`readlink $$slib` $$slib ; \
	    done ; \
	done
	ls -l debian/tmp/usr/bin
	cd debian/tmp/usr/bin && \
	for bin in *-orig.* ; do \
	    base=$${bin%-orig.*} && \
	    vbase=$$(readlink "$$(readlink $$base)") && \
	    vdriver=$$(readlink "$$vbase") && \
	    mv -v $$bin $$base-orig && \
	    ln -snfv $${vdriver%%.[0-9]*} $$vbase || exit $$? ; \
	done && \
	for bin in *[a-z+] ; do \
	    bin2=$$(readlink $$bin) ; \
	    bin3=$$(readlink "$$bin2") ; \
	    case $$bin2 in \
	        $$bin.* ) rm -v $$bin2 && mv -v $$bin3 $$bin || exit $$? ;; \
	    esac ; \
	done
	mkdir debian/tmp/usr/lib/sra-toolkit
	mv debian/tmp/usr/bin/sratools debian/tmp/usr/lib/sra-toolkit
	for x in debian/tmp/usr/bin/*-orig; do \
	    mv -v $$x debian/tmp/usr/lib/sra-toolkit ; \
	    ln -snfv ../lib/sra-toolkit/sratools $${x%-orig} ; \
	done
	dh_install
	# remove susipcious file which is not executable and conflicts with package magic
	rm -f debian/*/usr/bin/magic
	pybuild --install --name ngs -d ngs/ngs-python -s distutils

override_dh_installexamples:
	dh_installexamples
	find debian/*/ -name .gitignore -delete

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd obj-$(DEB_HOST_MULTIARCH)  && \
	    ctest --force-new-ctest-process --verbose -j1 -LE online
endif

override_dh_prep:
	dh_prep -X debian/tmp

execute_before_dh_link:
	# Putting the upstream version number (without the dfsg part) at the end of
	# symlink source in the -jni package.
	sed -e 's/\(#STRIPPED_UPSTREAM_VERSION#\)/\1$(DEB_VERSION_UPSTREAM)/' \
	    -e 's/#STRIPPED_UPSTREAM_VERSION#\(.*\)+dfsg[0-9]*/\1/' \
	    -e 's/\$$[({]DEB_HOST_MULTIARCH[)}]/$(DEB_HOST_MULTIARCH)/g' \
	    debian/libngs-jni.links.in > debian/libngs-jni.links

# require network, not automatically run
# use it when the pom file must be re-downloaded from maven repo
BASE_URL:=https://repo.maven.apache.org/maven2/gov/nih/nlm/ncbi/ngs-java
WGET=wget --no-clobber
get-poms:
	$(RM) debian/ngs-java.pom debian/ngs-java.pom.asc
	# extract upstream version
	$(WGET) -O debian/ngs-java.pom $(BASE_URL)/$(DEB_VERSION_UPSTREAM)/ngs-java-$(DEB_VERSION_UPSTREAM).pom && \
	$(WGET) -O debian/ngs-java.pom.asc $(BASE_URL)/$(DEB_VERSION_UPSTREAM)/ngs-java-$(DEB_VERSION_UPSTREAM).pom.asc
	gpg --trust-model=always --status-fd 1 --no-default-keyring --keyring debian/sra-tools.gpg --verify debian/ngs-java.pom.asc
