#!/usr/bin/make -f

export DH_VERBOSE=1

pkg := subread
mandir := $(CURDIR)/debian/$(pkg)/usr/share/man/man1
bindir := $(CURDIR)/bin
utildir := $(CURDIR)/bin/utilities

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/architecture.mk
ifeq ($(origin CC),default)
    CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

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 $@

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

override_dh_auto_clean:
	dh_auto_clean
	rm -f test/subread-align/data/test-err-mut-r1.fq.gz test/subread-align/data/test-err-mut-r2.fq.gz
	rm -rf bin src/longread-one/make.version
	find . -name "*.o" -delete

override_dh_auto_build:
	cd src; make -f Makefile.Linux
	dh_auto_build -- CC=$(CC)

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	debian/tests/subread-tests $(CURDIR)
endif

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='long-read aligner that is designed based on seed-and-vote' \
	$(bindir)/sublong| debian/filter.pl > $(mandir)/sublong.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='detectionCall' \
#	$(utildir)/detectionCall | debian/filter.pl > $(mandir)/detectionCall.1;

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

	$(HELP2MAN) --name='retrieve Phred score for read bases' \
	$(utildir)/qualityScores| debian/filter.pl > $(mandir)/qualityScores.1;

	$(HELP2MAN) --name='Find reads that are from the same pair in the input and then place them next \
to each other in the output. A dummy read is added for each singleton read that does not have a pair. \
The output file is compatible with featureCounts program' \
	$(utildir)/repair| debian/filter.pl > $(mandir)/repair.1;

	$(HELP2MAN) --name='Remove duplicated reads' \
	$(utildir)/removeDup| debian/filter.pl > $(mandir)/removeDup.1;

	$(HELP2MAN) --name='Flatten features included in a GTF annotation and save the modified annotation \
to a SAF format file.' \
	$(utildir)/flattenGTF| debian/filter.pl > $(mandir)/flattenGTF.1;

	$(HELP2MAN) --name='helper tool from subread suite' \
	$(utildir)/genRandomReads| debian/filter.pl > $(mandir)/genRandomReads.1;

	dh_auto_install

override_dh_installexamples-indep:
	tar --sort=name \
            --mtime="@${SOURCE_DATE_EPOCH}" \
            --owner=root --group=root --numeric-owner \
            --mode=go=rX,u+rw,a-s \
            -cJf $(CURDIR)/debian/subread-data/usr/share/doc/subread/examples/test.tar.xz  test/
