#!/usr/bin/make -f

PACKAGE = grinder
AUTHOR  = Florent Angly <florent.angly@gmail.com>
VERSION = 0.4.2
SECTION = 1
MANDIR  = debian/man

NAME1   = $(PACKAGE)
SCRIPT1 = script/$(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:
	# some files are duplicates from BioPerl and create conflicts when trying
	# to install the package.  So the are moved out of the way in the beginning
	# and droped into place in the clean target again
	mkdir -p _disposal/Bio
	# MYMETA.yml will be changed and not reverted in clean target - use the chance to conserve it here
	cp -a MYMETA.yml _disposal
	mv lib/Bio/PrimarySeq*.pm		_disposal/Bio
	mkdir -p _disposal/Bio/DB
	mv lib/Bio/DB/Fasta.pm			_disposal/Bio/DB
	mkdir -p _disposal/Bio/Seq
	mv lib/Bio/Seq/SeqFastaSpeedFactory.pm	_disposal/Bio/Seq
	mkdir -p _disposal/Bio/SeqFeature
	mv lib/Bio/SeqFeature/Primer.pm		_disposal/Bio/SeqFeature
	mkdir -p _disposal/Bio/Tools
	mv lib/Bio/Tools/IUPAC.pm		_disposal/Bio/Tools
	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:
	$(MAKE) testdb TEST_FILE=t/00-load.t TESTDB_SW=''

# Generate manpages on the fly
override_dh_auto_build:
	mkdir -p $(MANDIR)
	echo "$$MORE" > $(MOREFILE)
	help2man --no-discard-stderr --name $(DESC1) --section $(SECTION) --version-string $(VERSION) --no-info --include $(MOREFILE) --output $(MAN1) $(SCRIPT1)
	help2man --no-discard-stderr --name $(DESC2) --section $(SECTION) --version-string $(VERSION) --no-info --include $(MOREFILE) --output $(MAN2) $(SCRIPT2)
	help2man --no-discard-stderr --name $(DESC3) --section $(SECTION) --version-string $(VERSION) --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
