#!/usr/bin/make -f

#DH_VERBOSE = 1
export LC_ALL=C.UTF-8
include /usr/share/dpkg/default.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

DEB_VERSION_UPSTREAM := $(subst +ds,,$(DEB_VERSION_UPSTREAM))
generated_data_dir = $(CURDIR)/gendata

%:
	  dh $@ --buildsystem=meson

override_dh_auto_test: $(subst .t.in,.deb.t,$(wildcard tests/src/cram/pb*.t.in))
ifeq (testsuitedisabled,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_configure -O--buildsystem=meson
	mkdir -p $(generated_data_dir)
	python3 tests/scripts/generate_data.py $(CURDIR)/tests/data $(generated_data_dir)
	# Fix broken PATH
	synthetic_movie_all_path=`find $$PWD -name synthetic_movie_all.subreadset.xml` ; \
	sed -i -e "s?.GENERATEDDATADIR/synthetic_movie_all.subreadset.xml?$${synthetic_movie_all_path}?" tests/src/cram/pbbamify*
	BINDIR=`dirname $$(find $$PWD -name pbmerge -type f -executable)`; \
	LIBDIR=`find $$PWD -name lib -type d`; \
	PATH="$$BINDIR:$(PATH)" LD_LIBRARY_PATH="$$LIBDIR:$(LD_LIBRARY_PATH)" \
	cram3 -v --preserve-env $^
endif

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md

override_dh_auto_clean:
	dh_auto_clean
	find -name "*.deb.t" -delete
	$(RM) -r $(generated_data_dir)

%.deb.t: %.t.in
	sed \
	-e 's/$$BAM2SAM/samtools view/g' \
	-e 's/\-\-header\-only/-H/g' \
	-e 's/\-\-no\-header//g' \
	-e 's|@PacBioBAM_BinDir@/||g' \
	-e 's|$$TOOLS_BIN/||g' \
	-e 's|@PacBioBAM_TestsDir@|$(CURDIR)/tests|g' \
	-e 's|@PacBioBAM_VERSION@|$(DEB_VERSION_UPSTREAM)|g' \
	-e 's|@GeneratedTestDataDir@|$(generated_data_dir)|g' \
	-e '/@PG/s|+dfsg||g' \
	-e 's/$$SAMTOOLS/samtools/g' \
	$< > $@

override_dh_install:
	dh_install
	d-shlibmove --commit \
		    --multiarch \
		    --devunversioned \
		    --exclude-la \
		    --exclude-a \
		    --override s/libhts2-dev/libhts-dev/ \
		    --override s/libpbcopper1.3.0-dev/libpbcopper-dev/ \
		    --movedev debian/tmp/usr/include/* usr/include \
		    --movedev "debian/tmp/usr/lib/*/pkgconfig/*.pc" usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig \
		    debian/tmp/usr/lib/*/*.so
