#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for cd-hit
#- Andreas Tille <tille@debian.org>
#- Steffen Moeller <moeller@debian.org>
# GPL

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

pkg := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
ver := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')

mandir := $(CURDIR)/debian

%:
	dh $@

override_dh_auto_build:
	dh_auto_build -- openmp=yes

override_dh_installman:
	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='quickly group sequences' \
	   $(CURDIR)/cd-hit > $(mandir)/cd-hit.1
	(cd $(mandir) && ln -s cd-hit.1 cdhit.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='quickly group sequences, optimised for 454 data' \
	   $(CURDIR)/cd-hit-454 > $(mandir)/cd-hit-454.1
	(cd $(mandir) && ln -s cd-hit-454.1 cdhit-454.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='quickly group sequences in db1 or db2 format' \
	   $(CURDIR)/cd-hit-2d > $(mandir)/cd-hit-2d.1
	(cd $(mandir) && ln -s cd-hit-2d.1 cdhit-2d.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='run CD-HIT algorithm on RNA/DNA sequences' \
	   $(CURDIR)/cd-hit-est > $(mandir)/cd-hit-est.1
	(cd $(mandir) && ln -s cd-hit-est.1 cdhit-est.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='run CD-HIT algorithm on RNA/DNA sequences in db1 or db2 format' \
	   $(CURDIR)/cd-hit-est-2d > $(mandir)/cd-hit-est-2d.1
	(cd $(mandir) && ln -s cd-hit-est-2d.1 cdhit-est-2d.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='divide a big clustering job into pieces to run cd-hit-2d or cd-hit-est-2d jobs' \
	   $(CURDIR)/cd-hit-2d-para.pl	> $(mandir)/cd-hit-2d-para.1
	(cd $(mandir) && ln -s cd-hit-2d-para.1 cdhit-2d-para.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='divide a big clustering job into pieces to run cd-hit or cd-hit-est jobs' \
	    $(CURDIR)/cd-hit-para.pl	> $(mandir)/cd-hit-para.1
	(cd $(mandir) && ln -s cd-hit-para.1 cdhit-para.1)

	help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	   --name='divide a big clustering job into pieces to run cd-hit or cd-hit-est jobs' \
	    $(CURDIR)/cd-hit-div.pl	> $(mandir)/cd-hit-div.1
	(cd $(mandir) && ln -s cd-hit-div.1 cdhit-div.1)

	dh_installman

	# psi-cd-hit.pl is throwing some errors which are fixed using sed
	# ... at least in versions <= 4.6.4 - now help2man does not work at all
	#help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	#   --name='runs similar algorithm like CD-HIT but using BLAST to calculate similarities' \
	#   $(CURDIR)/psi-cd-hit.pl	| \
	#   sed -e '/^Name "main::.*" used only once:/d' \
	#	> $(mandir)/psi-cd-hit.1

	# help2man does not work at all since version 4.6.4
	#help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	#   --name='runs similar algorithm like CD-HIT but using BLAST to calculate similarities in db1 or db2 format' \
	#      $(CURDIR)/psi-cd-hit-2d.pl	> $(mandir)/psi-cd-hit-2d.1

	# FIXME: what is the difference between psi-cd-hit-2d.pl and psi-cd-hit-2d-g1.pl ?
	# help2man does not work at all since version 4.6.4
	#help2man --no-info --no-discard-stderr --version-string='$(ver)' \
	#   --name='runs similar algorithm like CD-HIT but using BLAST to calculate similarities in db1 or db2 format' \
	#      $(CURDIR)/psi-cd-hit-2d-g1.pl	> $(mandir)/psi-cd-hit-2d-g1.1

	# No help output from
	#   cd-hit-div.pl
	#   clstr2tree.pl
	#   clstr_merge.pl
	#   clstr_merge_noorder.pl
	#   clstr_reduce.pl
	#   clstr_renumber.pl
	#   clstr_rev.pl
	#   clstr_sort_by.pl
	#   clstr_sort_prot_by
	#   make_multi_seq
	#   psi-cd-hit-local.pl    -> even throws several "used only once: possible typo" errors

override_dh_auto_install:
	dh_auto_install -- PREFIX=debian/$(pkg)/usr/lib/cd-hit

override_dh_compress:
	dh_compress --exclude=.pdf

override_dh_auto_clean:
	# removing autogenerated man pages
	rm -f debian/*.1
	# continue with regular auto-clean
	dh_auto_clean
