#!/usr/bin/make -f

PACKAGE := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
AUTHOR  := Florent Angly <florent.angly@gmail.com>
VERSION := $(shell LC_ALL=C dpkg-parsechangelog | awk '/^Version:/{print $$2;}' | sed -e 's,-[^-]*$$,,g')
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:
	# It seems libbio-perl-perl is integrating actively code
	# from grinder and includes it into their distribution.  Finally
	# now are all modules from the Bio directory integrated into
	# BioPerl.  The single file moving was kept just for the reason
	# to have some template once a new Grinder version might come up
	# with new code
	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
	mv lib/Bio/DB/IndexedBase.pm		_disposal/Bio/DB
	mkdir -p _disposal/Bio/Seq
	mv lib/Bio/Seq/SeqFastaSpeedFactory.pm	_disposal/Bio/Seq
	mv lib/Bio/Seq/SimulatedRead.pm		_disposal/Bio/Seq
	mkdir -p _disposal/Bio/SeqFeature
	mv lib/Bio/SeqFeature/Amplicon.pm	_disposal/Bio/SeqFeature
	mv lib/Bio/SeqFeature/Primer.pm		_disposal/Bio/SeqFeature
	mv lib/Bio/SeqFeature/SubSeq.pm		_disposal/Bio/SeqFeature
	mkdir -p _disposal/Bio/Tools
	mv lib/Bio/Tools/AmpliconSearch.pm	_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
