#!/usr/bin/make -f
# -*- makefile -*-
#
# This rules file builds packages in two different threads.
# The -arch thread is initiated via the binary-arch target
# and similarly, the -indep thread is initiated via the binary-indep
# target. This enables dpkg-buildpackage to build either -arch only,
# -indep only or both, depending on the setting of the -A, -B or -b 
# flags.

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

# These files are overwritten by autoreconf which we need to run because
# one of the Makefile.am's is patched. They are deleted in the clean
# target as not to interfere with the building of the source package.

OVERWRITTEN_FILES = configure aclocal.m4 Makefile.in examples/Makefile.in \
config/config.guess config/ltmain.sh config/Makefile.in dox/Makefile.in	  \
clipper/Makefile.in clipper/minimol/Makefile.in clipper/cif/Makefile.in	  \
clipper/mmdb/Makefile.in clipper/cns/Makefile.in clipper/phs/Makefile.in  \
clipper/contrib/Makefile.in clipper/ccp4/Makefile.in			  \
clipper/core/Makefile.in

NOT_IN_TARBALL = config/lt~obsolete.m4 config/ltoptions.m4	\
config/ltversion.m4 config/ltsugar.m4 config/libtool.m4

include /usr/share/quilt/quilt.make


# The first target to be called. First we patch, then
# we run pre-configure:
configure-stamp: config.status pre-configure-stamp
	./configure $(CROSS) \
	--prefix=/usr \
	--with-fftw=/usr \
	--enable-mmdb \
	--with-mmdb=/usr \
	--enable-gpp4 \
	--with-gpp4=/usr \
	--enable-cif \
	--enable-minimol \
	--enable-cns \
	CFLAGS="$(CFLAGS)" \
	CXXFLAGS="$(CXXFLAGS)"

	touch $@

config.status:
	dh_testdir

	# Save some upstream files that get overwritten 
	# when we run autoreconf... this is mainly useful
	# when building the source package, where those files
	# otherwise end up in .diff.gz.
	tar -czf ow.tgz $(OVERWRITTEN_FILES)

	# Here we configure the package.
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config/config.guess
endif

pre-configure-stamp: patch
	autoreconf --force --install
	touch $@

#Architecture
build:

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp

	$(MAKE)
	touch $@

build-indep: build-indep-stamp
build-indep-stamp:
	cp debian/Doxyfile .
	doxygen
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f pre-configure-stamp configure-stamp 
	rm -f build-arch-stamp build-indep-stamp
	rm -f Doxyfile	
	rm -rf doc/

	# clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f config.sub config.guess

	# Re-establish original files
	if test -f ow.tgz; then tar -xzf ow.tgz; rm -f ow.tgz; fi
	rm -f $(NOT_IN_TARBALL)
	rm -f Doxygen doxy.core.tag

	dh_clean

install: install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs -i
	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s
	dh_installdirs -s

	# install the arch part of the package into debian/tmp.
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	install -D -m644 debian/libclipper2.lintian debian/tmp/usr/share/lintian/overrides/libclipper2

	dh_install -s

# This target is called by binary-arch/binary-indep in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress --exclude=examples
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independent packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary: binary-arch binary-indep

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

####
