#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
pkg := subread
mandir := $(CURDIR)/debian/$(pkg)/usr/share/man/man1
bindir := $(CURDIR)/bin
utildir := $(CURDIR)/bin/utilities

DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifeq ($(DEB_HOST_ARCH),$(filter $(DEB_HOST_ARCH),amd64 i386))
        CFLAGS += -mtune=generic -msse3
endif
ifeq ($(DEB_HOST_ARCH_OS), kfreebsd)
        CFLAGS += -D KFREEBSD
endif

%:
	dh $@ --parallel

override_dh_clean:
	cd src; make -f Makefile.Linux clean
	dh_clean

override_dh_auto_build:
	cd src; make -f Makefile.Linux
	dh_auto_build

override_dh_auto_test:
	debian/tests/subread-tests $(CURDIR)

HELP2MAN = help2man --no-info --help-option="''" --no-discard-stderr

override_dh_auto_install:
#fix for gzip-file-is-not-multi-arch-same-safe
	find $(CURDIR) -name "*.gz" -exec bash -c 'name="{}"; gzip -d "{}"; gzip -n "$${name%.*}"' \;

	mkdir -p $(mandir)
	$(HELP2MAN) --name='an accurate and efficient aligner for mapping both genomic \
DNA-seq reads and RNA-seq reads (for the purpose of expression analysis)' \
	$(bindir)/subread-align | debian/filter.pl > $(mandir)/subread-align.1;

	$(HELP2MAN) --name='builds an base-space or color-space index using the reference sequences' \
	$(bindir)/subread-buildindex | debian/filter.pl > $(mandir)/subread-buildindex.1;

	$(HELP2MAN) --name='an RNA-seq aligner suitable for all purposes of RNA-seq analyses' \
	$(bindir)/subjunc | debian/filter.pl > $(mandir)/subjunc.1;

	$(HELP2MAN) --name='a highly efficient and accurate read summarization program' \
	$(bindir)/featureCounts | debian/filter.pl > $(mandir)/featureCounts.1;

	$(HELP2MAN) --name='a SNP caller that discovers SNPs by testing signals against local background noises' \
	$(bindir)/exactSNP | debian/filter.pl > $(mandir)/exactSNP.1;

	$(HELP2MAN) --name='detect short and long indels' \
	$(bindir)/subindel | debian/filter.pl > $(mandir)/subindel.1;

	$(HELP2MAN) --name='scans the entire genome and reports all matches of a specified sequence' \
	$(utildir)/subread-fullscan | debian/filter.pl > $(mandir)/subread-fullscan.1;

	$(HELP2MAN) --name='counting the coverage of mapped reads at each location on the entire reference genome' \
	$(utildir)/coverageCount | debian/filter.pl > $(mandir)/coverageCount.1;

	$(HELP2MAN) --name='calculate the proportion of mapped reads/fragments' \
	$(utildir)/propmapped | debian/filter.pl > $(mandir)/propmapped.1;

	for i in removeDup repair qualityScores ; do \
	$(HELP2MAN) --name='a component of subread suite' $(utildir)/$$i | debian/filter.pl > $(mandir)/$$i.1; \
	done

	dh_auto_install

override_dh_installexamples-indep:
	tar -cJf $(CURDIR)/debian/subread-data/usr/share/doc/subread/examples/test.tar.xz  test/
