#!/usr/bin/make -f

# This package is maintained by the Debian Med packaging team. Please refer to
# our group policy if you would like to commit to our Git repository. All
# Debian developpers have write acces to it.
#
# https://med-team.pages.debian.net/policy/

include /usr/share/dpkg/default.mk

prefix=$(CURDIR)/debian/$(DEB_SOURCE)/usr
mandir=$(prefix)/share/man/man1
libexecdir=$(prefix)/lib/$(DEB_SOURCE)

# Copy and extend upstream CXXFLAGS here because makefile enables only overriding them
export DEB_CXXFLAGS_MAINT_APPEND += -Wall -Wextra -Wcast-qual -Wswitch-enum -Wundef -Wcast-align -Wno-long-long -ansi -pedantic -std=c++11 -fopenmp-simd -O3
export DEB_CPPFLAGS_MAINT_APPEND += -g -DHAS_CXX_THREADS -DSIMDE_ENABLE_OPENMP
# -Wconversion
# -fomit-frame-pointer ?

export DEB_LDFLAGS_MAINT_APPEND += -pthread

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Ensure the COMMAND-plain variants is used to generate the manpages
# for reproducible builds
export SIMD_LIST = fallback-to-plain-simd

export LC_ALL=C.UTF-8
export LANG=C.UTF-8

BUILT_USING=$(shell dpkg-query -f '$${source:Package} (= $${source:Version}), ' -W "libsimde-dev")

%:
	dh  $@

override_dh_auto_build:
ifeq (amd64,$(DEB_HOST_ARCH))
	mkdir -p $(prefix)
	mkdir -p $(libexecdir)
	for SIMD in avx2 avx sse4.1 ssse3 sse3 sse2 ; do \
		dh_auto_build -- all SFX=-$${SIMD} CFLAGS="$(CFLAGS) -m$${SIMD}" CXXFLAGS="$(CXXFLAGS) -m$${SIMD}" ; \
		find . -name '*.o' -delete ; \
		find . -name '*.o5' -delete ; \
	done
	dh_auto_build -- all SFX=-plain CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)"
else ifeq (i386,$(DEB_HOST_ARCH))
	mkdir -p $(prefix)
	mkdir -p $(libexecdir)
	for SIMD in avx2 avx sse4.1 ssse3 sse3 sse2 sse; do \
		dh_auto_build -- all SFX=-$${SIMD} CFLAGS="$(CFLAGS) -m$${SIMD}" CXXFLAGS="$(CXXFLAGS) -m$${SIMD}" ; \
		find . -name '*.o' -delete ; \
		find . -name '*.o5' -delete ; \
	done
	dh_auto_build -- all SFX=-plain CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)"
else
	dh_auto_build
endif

HELP2MAN = help2man --no-info --version-string="$(DEB_VERSION_UPSTREAM)"

override_dh_auto_install:
ifeq (amd64,$(DEB_HOST_ARCH))
	dh_install debian/bin/simd-dispatch /usr/lib/$(DEB_SOURCE)/
	dh_install bin/lastdb-* bin/lastal-* bin/last-split-* bin/last-merge-batches-* bin/last-pair-probs-* bin/lastdb5-* bin/lastal5-* bin/last-split5-* usr/lib/last-align
	dh_install bin/fastq-* bin/fasta-* bin/last-dotplot bin/last-map-probs bin/last-postmask bin/last-train bin/maf-* bin/parallel-fast* usr/bin
	mkdir -p $(prefix)/bin
	cd $(prefix)/bin \
		&& for prog in lastdb lastal last-split last-merge-batches last-pair-probs lastdb5 lastal5 last-split5 ; do \
		ln -s ../lib/$(DEB_SOURCE)/simd-dispatch $${prog} ; done
else ifeq (i386,$(DEB_HOST_ARCH))
	dh_install debian/bin/simd-dispatch /usr/lib/$(DEB_SOURCE)/
	dh_install bin/lastdb-* bin/lastal-* bin/last-split-* bin/last-merge-batches-* bin/last-pair-probs-* bin/lastdb5-* bin/lastal5-* bin/last-split5-* usr/lib/last-align
	dh_install bin/fastq-* bin/fasta-* bin/last-dotplot bin/last-map-probs bin/last-postmask bin/last-train bin/maf-* bin/parallel-fast* usr/bin
	mkdir -p $(prefix)/bin
	cd $(prefix)/bin \
		&& for prog in lastdb lastal last-split last-merge-batches last-pair-probs lastdb5 lastal5 last-split5 ; do \
		ln -s ../lib/$(DEB_SOURCE)/simd-dispatch $${prog} ; done
else
	$(MAKE) -j$(NUMJOBS) install prefix=$(prefix)
endif

override_dh_auto_clean:
ifeq (amd64,$(DEB_HOST_ARCH))
	for SIMD in avx2 avx sse4.1 ssse3 sse3 sse2 ; do \
		dh_auto_clean -- SFX=-$${SIMD} ; done
else ifeq (i386,$(DEB_HOST_ARCH))
	for SIMD in avx2 avx sse4.1 ssse3 sse3 sse2 sse plain; do \
		dh_auto_clean -- SFX=-$${SIMD} ; done
else
	dh_auto_clean
endif
	for i in last-merge-batches last-pair-probs last-split last-split5 lastal lastal5 lastdb lastdb5; \
	do \
	   for j in avx avx2 sse2 sse3 sse4.1 ssse3 sse3 sse2 sse ; \
	   do \
	      rm -f bin/$$i-$$j ; \
	   done ; \
	done

override_dh_installman:
	dh_installman
	$(HELP2MAN) \
		 --name='make a dotplot, a.k.a. Oxford Grid, of alignments in LAST tabular format' \
	         $(CURDIR)/bin/last-dotplot > $(mandir)/last-dotplot.1
	$(HELP2MAN) \
	         --name="genome-scale comparison of biological sequences" \
	         --help-option="-h" \
                 $(prefix)/bin/lastal > $(mandir)/lastal.1
	$(HELP2MAN) \
	         --name="genome-scale comparison of biological sequences" \
	         --help-option="-h" \
                 $(prefix)/bin/lastal5 > $(mandir)/lastal5.1
	# help2man output needs manual postprocessing - see debian/mans
	#$(HELP2MAN) \
	#         --name="genome-scale comparison of biological sequences" \
	#         --help-option="-h" \
        #         $(prefix)/bin/lastdb > $(mandir)/lastdb.1
	$(HELP2MAN) \
                 --name="Sort MAF-format alignments by sequence name" \
                 $(CURDIR)/bin/maf-sort > $(mandir)/maf-sort.1
	$(HELP2MAN) \
                 --name="changes the order of the sequences in MAF-format alignments" \
                 $(CURDIR)/bin/maf-swap > $(mandir)/maf-swap.1
	# help2man output needs manual postprocessing - see debian/mans
	#$(HELP2MAN) \
        #         --name="Add extra column with mapping probabilities" \
        #         $(CURDIR)/bin/last-map-probs > $(mandir)/last-map-probs.1
	# This script has not enough to put in a manual page.
	#$(HELP2MAN) \
        #         --name="Read files of lastal output, merge corresponding batches, and write them." \
        #         $(CURDIR)/bin/last-merge-batches > $(mandir)/last-merge-batches.1
	#         # These bin were droped
	#$(HELP2MAN) \
        #         --name="Clean up MAF-format alignments" \
        #         $(CURDIR)/bin/last-reduce-alignments > $(mandir)/last-reduce-alignments.1
	#$(HELP2MAN) \
        #         --name='Remove MAF-format alignments which are "dominated" by others' \
        #         $(CURDIR)/bin/last-remove-dominated > $(mandir)/last-remove-dominated.1
	$(HELP2MAN) \
                 --name='Estimates "split" or "spliced" alignments.' \
                 $(prefix)/bin/last-split > $(mandir)/last-split.1
	$(HELP2MAN) \
                 --name='Estimates "split" or "spliced" alignments.' \
                 $(prefix)/bin/last-split5 > $(mandir)/last-split5.1
	$(HELP2MAN) \
                 --name="Join two or more sets of MAF-format multiple alignments" \
                 $(CURDIR)/bin/maf-join > $(mandir)/maf-join.1
	$(HELP2MAN) \
		 --help-option="-h" \
                 $(CURDIR)/bin/maf-cut > $(mandir)/maf-cut.1
	$(HELP2MAN) \
                 --name="Convert MAF-format alignments to tabular format" \
                 $(CURDIR)/bin/maf-convert > $(mandir)/maf-convert.1
	$(HELP2MAN) \
                 --name="Read 2 fastq files, and write them interleaved" \
                 $(CURDIR)/bin/fastq-interleave > $(mandir)/fastq-interleave.1
	$(HELP2MAN) \
                 --name="Read files of lastal output, merge corresponding batches, and write them" \
                 $(prefix)/bin/last-merge-batches > $(mandir)/last-merge-batches.1
	# help2man output needs manual postprocessing - see debian/mans
	#$(HELP2MAN) \
        #         --name="Read alignments of paired DNA reads to a genome, and do estimations" \
        #         $(prefix)/bin/last-pair-probs > $(mandir)/last-pair-probs.1
	$(HELP2MAN) \
                 --name="Get alignments that have a segment with score >= threshold, with gentle masking of lowercase letters" \
                 $(CURDIR)/bin/last-postmask > $(mandir)/last-postmask.1
	$(HELP2MAN) \
                 --name="Try to find suitable score parameters for aligning the given sequences" \
                 $(CURDIR)/bin/last-train > $(mandir)/last-train.1
	$(HELP2MAN) \
	         --name="Keep the first of identical sequences" \
	         --help-option="-h" \
                 $(prefix)/bin/fasta-nr > $(mandir)/fasta-nr.1
override_dh_gencontrol:
	dh_gencontrol -- -VBuilt-Using="$(BUILT_USING)"
