#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

AUTHOR  := Florent Angly <florent.angly@gmail.com>
SECTION := 1
MANDIR  := debian/man

NAME1   = $(DEB_SOURCE)
SCRIPT1 = bin/$(NAME1)
DESC1   = 'Versatile omics shotgun and amplicon sequencing read simulator'
MAN1    = $(MANDIR)/$(NAME1).$(SECTION)

NAME2   = average_genome_size
SCRIPT2 = utils/$(NAME2)
DESC2   = 'Calculate the average genome size in Grinder libraries'
MAN2    = $(MANDIR)/$(NAME2).$(SECTION)

NAME3   = change_paired_read_orientation
SCRIPT3 = utils/$(NAME3)
DESC3   = 'Reverses the orientation of each paired\-end FASTA sequence'
MAN3    = $(MANDIR)/$(NAME3).$(SECTION)

define MORE
[see also]
.BR grinder (7),
.BR $(NAME1) ($(SECTION)),
.BR $(NAME2) ($(SECTION))
and
.BR $(NAME3) ($(SECTION)).
endef
export MORE
MOREFILE = $(MANDIR)/more.$(SECTION)


%:
	dh $@

override_dh_auto_configure:
	# MYMETA.yml will be changed and not reverted in clean target - use the chance to conserve it here
	mkdir _disposal
	cp -a MYMETA.yml _disposal
	dh_auto_configure

# Because of Grinder's random output, some tests are brittle and may fail...
# Only test that the program loads successfully
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) testdb TEST_FILE=t/00-load.t TESTDB_SW=''
endif

# Generate manpages on the fly
override_dh_auto_build:
	mkdir -p $(MANDIR)
	echo "$$MORE" > $(MOREFILE)
	PERL5LIB=lib help2man --no-discard-stderr --name $(DESC1) --section $(SECTION) --version-string $(DEB_VERSION_UPSTREAM) --no-info --include $(MOREFILE) --output $(MAN1) $(SCRIPT1)
	help2man --no-discard-stderr --name $(DESC2) --section $(SECTION) --version-string $(DEB_VERSION_UPSTREAM) --no-info --include $(MOREFILE) --output $(MAN2) $(SCRIPT2)
	help2man --no-discard-stderr --name $(DESC3) --section $(SECTION) --version-string $(DEB_VERSION_UPSTREAM) --no-info --include $(MOREFILE) --output $(MAN3) $(SCRIPT3)
	rm $(MOREFILE)
	dh_auto_build

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(MANDIR)
	if [ -d _disposal ] ; then \
	    if [ -e _disposal/MYMETA.yml ] ; then mv _disposal/MYMETA.yml . ; fi ; \
	    for pm in `cd _disposal ; find Bio -type f ; cd ..` ; do \
		mv _disposal/$${pm} lib/$${pm} ; \
	    done ; \
	fi
	rm -rf _disposal

override_dh_install:
	dh_install
	for pl in `grep -Rl '#!/usr/bin/env[[:space:]]\+perl' debian/*/usr/*` ; do \
	    sed -i '1s?^#!/usr/bin/env[[:space:]]\+perl?#!/usr/bin/perl?' $${pl} ; \
	done
