#!/usr/bin/make -f

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

export DH_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/pkg-info.mk
export HOME=$(shell echo $$PWD"/fakehome")

# import hardening, adding CPPFLAGS to CFLAGS, in case dh doesn't
# See https://wiki.debian.org/Hardening#Notes_for_packages_using_CMake
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Droping -fstack-protector flag helps **drastically** reducing memory consumption when building the package!!!!
# export CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS | sed 's/-fstack-protector *//')  ## don't do this, it simply triggers #759978
# export DEB_BUILD_MAINT_OPTIONS = hardening=-stackprotector
# alternatively: export DEB_CXXFLAGS_STRIP='-fstack-protector'

DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

DEB_BUILD_ARCH          ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifneq (,$(filter mips mipsel kfreebsd% hurd%,$(DEB_BUILD_ARCH)))
    CXXFLAGS+=-O1
    CFLAGS+=-01
else
    CXXFLAGS+=-O3
    CFLAGS+=-O3
endif

ifneq (,$(filter mips mipsel mips64el,$(DEB_BUILD_ARCH)))
    export CXXFLAGS+=-mxgot
endif
# Disable parallel building on some build archs to save memory
ifneq (,$(filter amd64 arm64 armhf mips kfreebsd% ppc64el sh4,$(DEB_BUILD_ARCH)))
    $(info $$DEB_BUILD_OPTIONS was [${DEB_BUILD_OPTIONS}])
    TDBO := $(DEB_BUILD_OPTIONS)
    export DEB_BUILD_OPTIONS=parallel=1,$(filter $(TDBO), parallel)
endif
$(info $$DEB_BUILD_OPTIONS is [${DEB_BUILD_OPTIONS}])

ifneq (,$(filter mips64el ppc64el s390x alpha hppa powerpc ppc64 sparc64,$(DEB_HOST_ARCH)))
# Disable async io on certain target archs, as per upstream
# https://github.com/seqan/seqan/issues/1861#issuecomment-336484553
CXXFLAGS+="-DSEQAN_ASYNC_IO=0"
endif

pkgapps=seqan-apps
pkgdev=libseqan2-dev

%:
	dh $@

override_dh_auto_configure-arch:
	#	dh_auto_configure -- -DSEQAN_BUILD_SYSTEM=SEQAN_RELEASE_APPS -DSEQAN_STATIC_APPS=False  # skip the tests for now
	dh_auto_configure -- -DSEQAN_BUILD_SYSTEM=DEVELOP \
		-DSEQAN_STATIC_APPS=False -DSEQAN_NO_DOX=1 \
		-DSEQAN_DISABLE_VERSION_CHECK=YES

override_dh_auto_configure-indep: ;

override_dh_auto_build-arch:
	dh_auto_build --arch
	#cd obj-$(DEB_BUILD_GNU_TYPE); make dox # This does nothing and no *.html files will be created at all
	# This ends up in:
	# Could not import extension sphinxcontrib.bibtex (exception: No module named sphinxcontrib.bibtex)
	# Seems there is no sphinx module bibtex - thus deactivating creation of html manual
	# cd manual && make html

override_dh_auto_build-indep:
	sed -e "s#@CMAKE_INSTALL_PREFIX@#/usr#g" \
	    -e "s/@SEQAN_VERSION_STRING@/$(DEB_VERSION_UPSTREAM_REVISION)/g" \
	    -e "s/@CMAKE_PROJECT_NAME@/seqan/g" \
	    util/pkgconfig/seqan.pc.in > seqan-2.pc

override_dh_install:
	dh_install
	# Delete additional LICENSE files
	find $(CURDIR)/debian/$(pkgapps) -type f -name LICENSE -delete || /bin/true
	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_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 seqan-2.pc

override_dh_installman-arch:
	dh_link -a
	$(CURDIR)/debian/generate_manpages
	#for bin in `ls $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin` ; do \
	#    if [ -x $(CURDIR)/debian/$(pkgapps)/usr/lib/seqan/bin/`basename $${bin}` ] ; then \
	#        ln -s ../lib/seqan/bin/`basename $${bin}` $(CURDIR)/debian/$(pkgapps)/usr/bin/`basename $${bin}` ; \
	#    fi ; \
	#    if echo $${bin} | grep -q '\.sh$$' ; then \
	#        ln -s ../lib/seqan/bin/$${bin} $(CURDIR)/debian/$(pkgapps)/usr/bin/`basename $${bin} .sh` ; \
	#    fi ; \
	#done
	# we generate only those manpages where binaries are linked to /usr/bin
	dh_installman -a

override_dh_link-arch:
	dh_link
	# 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-indep:
	dh_fixperms
	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
	find $(CURDIR)/debian/$(pkgdev)/usr/share/cmake -type f -exec chmod -x {} \;

override_dh_auto_test:
# enforce tests on some architectures
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 arm64 i386 kfreebsd-amd64 m68k x32))
	dh_auto_test
else
	dh_auto_test || /bin/true
endif
