#!/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

pkg := sra-toolkit

export DEB_CFLAGS_MAINT_APPEND   = -I/usr/include/ncbi-vdb
export DEB_CXXFLAGS_MAINT_APPEND = -I/usr/include/ncbi-vdb
include /usr/share/dpkg/buildflags.mk

%:
	dh $@ --no-parallel

OUTDIR=$(CURDIR)/debian/tmp
MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

override_dh_auto_clean:
	-dh_auto_clean

override_dh_clean:
	dh_clean
	rm -f reconfigure build/ld.linux.exe_cmd.sh build/Makefile.config build/Makefile.config.linux.x86_64
	rm -rf test/*/actual test/*/tmp.kfg tools/kqsh/kqsh-sra.[ch]

override_dh_update_autotools_config:
	echo "do nothing"

override_dh_auto_configure:
	# output dir will be checked so create it before
	mkdir -p debian/tmp/usr/lib
	ln -s /usr/lib/$(MULTIARCH) debian/tmp/usr/lib
	#
        # that's no standard configure script lacking support of default options
	#
	# --with-debug sets some options that require header files like compiler.h which
	# is internal to ncbi-vcb and not installed into the package - so this option
	# should not be used.
	./configure --build=$(MULTIARCH) --prefix=$(CURDIR)/debian/tmp/usr --build-prefix=$(CURDIR)/debian/tmp/usr --with-ngs-sdk-prefix=/usr

override_dh_auto_build:
	export LD_LIBRARY_PATH="$$LD_LIBRARY_PATH:$(OUTDIR)/lib"; export OUTDIR=$(OUTDIR) ; make DEB_CFLAGS="$(CFLAGS)" DEB_CXXFLAGS="$(CXXFLAGS)" DEB_LDFLAGS="$(LDFLAGS)"

override_dh_auto_install:
	dh_auto_install -- KONFIG_DIR=$(CURDIR)/debian/sra-toolkit/etc/ncbi

override_dh_install:
	# upstream installs versioned *.a libs - we only want a single version
	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
	dh_install
	# remove susipcious file which is not executable and conflicts with package magic
	rm -f debian/*/usr/bin/magic
	# Formally supply a standard installation ID to satisfy sratools
	echo '/LIBS/GUID = "$(DEBIAN_GUID)"' >debian/$(pkg)/etc/ncbi/debian.kfg

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test --no-parallel -- -k
endif

override_dh_prep:
	dh_prep -X debian/tmp
