#!/usr/bin/make -f

# based upon sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

pkg=mcl

# allow bashism's in commandlines
SHELL = /bin/bash

# Debian Policy 10.1
CFLAGS = -g -Wall
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
INSTALL = install
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

# variables to be used in configure invocation. see
# autotools-dev/README.Debian.gz
export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# use autoconf 2.52 or newer
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

## Build BLAST parsing script mcxdeblast and the pipeline script mclblastline
## (sources in src/alien/oxygen).  It encapsulates all the stages of parsing,
## matrix creation, running mcl, and formatting the result
## into a single application.
confflags += --enable-blast

## add --enable-tribe to confflags to add the upstreams supplied tribe tools
## (protein clustering stuff).  This builds and installs the tools
##
##  tribe-families  tribe-matrix  tribemcl  tribe-parse
##
## We're not doing it since tribe-matrix segfaults, and the tools lackproper
## manpages.  Furthermore, the BLAST parsing scripts can be used in place of
## the tribe module.

# confflags += --enable-tribe

## If you really want to suppress the citation reference printed by MCL (the
## one which looks like
##
##  Please cite:
##    Stijn van Dongen, Graph Clustering by Flow Simulation.  PhD thesis,
##    University of Utrecht, May 2000.
##       (  http://www.library.uu.nl/digiarchief/dip/diss/1895620/full.pdf
##       or  http://micans.org/mcl/lit/svdthesis.pdf.gz)
##  OR
##    Stijn van Dongen, A cluster algorithm for graphs. Technical
##    Report INS-R0010, National Research Institute for Mathematics
##    and Computer Science in the Netherlands, Amsterdam, May 2000.
##       (  http://www.cwi.nl/ftp/CWIreports/INS/INS-R0010.ps.Z
##       or  http://micans.org/mcl/lit/INS-R0010.ps.Z)
##
## ), then this is the place to do it.  Of course we honor the choice of
## upstream here, and build as suggested by upsteam.

# confflags += --disable-helpful-reminder

config: config-stamp
config-stamp:
	dh_testdir

#	# Make sure we have fresh config.{sub,guess}, in order to make life
#	# more easy for porters to new architectures. See
#	# autotools-dev/README.Debian.gz
	if [ -r /usr/share/misc/config.sub ] && [ ! -f autofoo/config.sub.backup ]; then \
		mv -v autofoo/config.sub autofoo/config.sub.backup; \
		cp -f /usr/share/misc/config.sub autofoo/config.sub; \
	fi

	if [ -r /usr/share/misc/config.guess ] && [ ! -f autofoo/config.guess.backup ]; then \
		mv -v autofoo/config.guess autofoo/config.guess.backup; \
		cp -f /usr/share/misc/config.guess autofoo/config.guess; \
	fi

	touch $@

config-clean:
	dh_testdir
	dh_testroot

	if [ -f autofoo/config.sub.backup ]; then \
		mv -fv autofoo/config.sub.backup autofoo/config.sub; \
	fi

	if [ -f autofoo/config.guess.backup ]; then \
		mv -fv autofoo/config.guess.backup autofoo/config.guess; \
	fi

	$(RM) config-stamp


configure: config configure-stamp
configure-stamp:
	dh_testdir

	./configure \
           $(confflags) \
           --prefix=/usr \
           --mandir=\$${prefix}/share/man \
           --infodir=\$${prefix}/share/info

	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure $(QUILT_STAMPFN)
	dh_testdir

	$(MAKE)

	touch build-stamp

clean: config-clean
	dh_testdir
	dh_testroot
	$(RM) -f build-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/mcl
#	# install some extra stuff not installed by upstream
	install -c -m 644 $(CURDIR)/{AUTHORS,README,THANKS} $(CURDIR)/debian/mcl/usr/share/doc/mcl/
	mkdir $(CURDIR)/debian/mcl/usr/share/doc/mcl/scripts
	install -c -m 644 $(CURDIR)/scripts/clx* $(CURDIR)/debian/mcl/usr/share/doc/mcl/scripts/
#	# clean up unneeded docs
	$(RM) $(CURDIR)/debian/mcl/usr/share/doc/mcl/*txt
#	# process mcl-doc.install
	dh_install --sourcedir=debian/mcl
	dh_installdocs
#	# clean up files just copied to mcl-doc
	$(RM) $(CURDIR)/debian/mcl/usr/share/doc/mcl/{AUTHORS,README,THANKS}
	$(RM) $(CURDIR)/debian/mcl/usr/share/doc/mcl/*{html,ps,minimcl}
	$(RM) -r $(CURDIR)/debian/mcl/usr/share/doc/mcl/examples
	$(RM) -r $(CURDIR)/debian/mcl/usr/share/man/man[5,7]

binary-indep: build install
	dh_installman
	dh_installchangelogs ChangeLog
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb -pmcl-doc


binary-arch: build install
#	no need to call dh_installexamples: upstream takes care of
#        this in 'make install'
	dh_installman
	dh_installchangelogs ChangeLog
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb -pmcl

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure config config-clean
