#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all reproducible=-fixfilepath
# reproducible=-fixfilepath is needed due to how the tests get their file paths :-(

include /usr/share/dpkg/pkg-info.mk
export HOME=$(CURDIR)/fakehome

include /usr/share/dpkg/architecture.mk

ifneq (,$(filter mips mipsel mips64el,$(DEB_BUILD_ARCH)))
	DEB_CXXFLAGS_MAINT_APPEND+=-mxgot
endif
ifneq (,$(filter mips mipsel,$(DEB_BUILD_ARCH)))
	DEB_CXXFLAGS_MAINT_APPEND+=-g1
	DEB_CFLAGS_MAINT_APPEND+=-g1
endif
# As per upstream's instructions
ifndef SEQAN2_SALSA_CI
	export DEB_CFLAGS_MAINT_APPEND+=-O3
	export DEB_CXXFLAGS_MAINT_APPEND+=-DNDEBUG -O3
else
	export DEB_CFLAGS_MAINT_APPEND+=-O1
	export DEB_CXXFLAGS_MAINT_APPEND+=-DNDEBUG -O1
endif

# Disable parallel building to save memory
# MAX_PARALLEL=--max-parallel=1

pkgapps=seqan-apps
pkgdev=libseqan2-dev

%:
	dh $@

override_dh_auto_configure-arch:
ifeq ($(DEB_HOST_ARCH),armhf)
	# The following tests FAILED:
	#     46 - test_test_index_crosscompare_dna (Bus error)
	#     52 - test_test_index_fm_rank_dictionary (Bus error)
	sed -i \
	    -e '/ test_test_index_crosscompare_dna /d' \
	    -e '/ test_test_index_fm_rank_dictionary /d' \
	    tests/index/CMakeLists.txt
endif
ifeq ($(DEB_HOST_ARCH),mips)
	# The following tests FAILED:
	#      7 - test_align_parallel_data_structures (Child aborted)
	sed -i \
	    -e '/ test_align_parallel_data_structures /d' \
	    tests/index/CMakeLists.txt
endif
	export DEB_BUILD_ARCH=${DEB_BUILD_ARCH}
ifndef SEQAN2_SALSA_CI
	dh_auto_configure --arch -- \
		-DSEQAN_STATIC_APPS=False \
		-DSEQAN_DISABLE_VERSION_CHECK=YES \
		-DSEQAN_STRIP_APPS_BUILD_PATH="$(CURDIR)" \
		-DSEQAN_NO_DOX=1
else
	dh_auto_configure --arch -- \
		-DSEQAN_STATIC_APPS=False \
		-DSEQAN_DISABLE_VERSION_CHECK=YES \
		-DALIGN_SIMD_TEST=FALSE \
		-DALIGN_PARALLEL_SIMD_TEST=FALSE \
		-DSEQAN_STRIP_APPS_BUILD_PATH="$(CURDIR)" \
		-DSEQAN_NO_DOX=1
endif

override_dh_auto_configure-indep:
	dh_auto_configure --indep -B build -- \
		-DSEQAN_BUILD_SYSTEM=SEQAN_RELEASE_LIBRARY -DSEQAN_NO_DOX=1 \
		-DSEQAN_DISABLE_REVISION_INFO=1

override_dh_auto_build-arch:
	dh_auto_build --arch ${MAX_PARALLEL}
	# can't make dox, "seqansphinx" is not yet packaged

override_dh_auto_install-indep:
	dh_auto_install --indep -B build --destdir=$(CURDIR)/debian/$(pkgdev)/
	rm -Rf $(CURDIR)/debian/$(pkgdev)/usr/share/doc/seqan

override_dh_install-indep:
	dh_install --indep
	# Delete additional LICENSE and compiled Python files
	if [ -d $(CURDIR)/debian/$(pkgdev) ] ; then \
	    find $(CURDIR)/debian/$(pkgdev) -type f -name LICENSE -delete ; \
	    find $(CURDIR)/debian/$(pkgdev) -type f -name "*.pyc" -delete ; \
	fi

override_dh_install-arch:
	dh_install --arch
	# Delete additional LICENSE files
	find $(CURDIR)/debian/$(pkgapps) -type f -name LICENSE -delete || /bin/true
	find $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin -type f \
		-name "*.sh" | xargs chmod a+x
#	for app in $$(ls $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin); \
#		do $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin/$${app} \
#		--write-ctd $${app}.ctd && CTDConverter cwl -i $${app}.ctd \
#		-o $(CURDIR)/debian/$(pkgapps)/usr/share/commonwl/${app}.cwl; \
#	        done;
#	rm *.ctd
#	find $(CURDIR)/debian/$(pkgapps)/usr/share/commonwl -type f \
#		-name "*.cwl" | xargs chmod a+x

override_dh_auto_clean:
	dh_auto_clean
	find . -type f -name "*.pyc" -delete
	if [ -d orig ] ; then \
	  for header in `find orig -name "*generated_forwards.h"` ; do \
	    mv $${header} `echo $$header | sed 's?orig/??'` ; \
	  done ; \
	fi
	rm -rf orig

override_dh_installman-arch:
	dh_link --arch
	if [ -d $(CURDIR)/debian/$(pkgapps)/usr/bin ] ; then \
		find $(CURDIR)/debian/$(pkgapps)/usr/bin -type l -xtype l -delete ; \
	fi
	$(CURDIR)/debian/generate_manpages
	dh_installman --arch

override_dh_link-arch:
	dh_link --arch
	# Some binaries (eg. splazer) are not built in 32-bit architectures so
	# remove their (broken) symlinks.
	if [ -d $(CURDIR)/debian/$(pkgapps)/usr/bin ] ; then \
	    find $(CURDIR)/debian/$(pkgapps)/usr/bin -type l -xtype l -delete ; \
	fi

override_dh_fixperms-arch:
	dh_fixperms --arch
	for bin in `ls $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin/*.sh` ; do \
	    chmod +x $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin/`basename $${bin}` ; \
	done
