#!/usr/bin/make -f

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


# 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 (,$(findstring arm,$(DEB_HOST_GNU_TYPE)))
	BUILD_FIXED := --enable-fixed
endif

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

PYVERS=$(shell pyversions -r)

config.status: configure
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --without-lapack $(BUILD_FIXED) CFLAGS="$(CFLAGS)"


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:  config.status
	dh_testdir
	$(MAKE)
	$(RM) -r python/build
	set -e; \
	for PYTHON in $(PYVERS); do \
		(cd python && $$PYTHON setup.py build); \
	done
	set -e; \
	for PYTHON in $(PYVERS); do \
		(cd python && $$PYTHON-dbg setup.py build); \
	done
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean 
	$(RM) -r debian/python-sphinxbase debian/python-sphinxbase-dbg
	$(RM) -r python/build

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install
	set -e; \
	for PYTHON in $(PYVERS); do \
	    (cd python && $$PYTHON setup.py install --root=$(CURDIR)/debian/python-sphinxbase --install-layout=deb) ; \
	done
	set -e; \
	for PYTHON in $(PYVERS); do \
	    (cd python && $$PYTHON-dbg setup.py install --root=$(CURDIR)/debian/python-sphinxbase-dbg --install-layout=deb) ; \
	done
	find debian/python-*-dbg ! -type d ! -name '*.so' | xargs rm -f
	find debian/python-*-dbg -depth -empty -exec rmdir {} \;
	-find debian -name '*.py[co]' | xargs rm -f

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_install --sourcedir=debian/tmp
	dh_installman
	dh_link
	dh_strip --exclude=_d.so
	rm -rf debian/python-sphinxbase-dbg/usr/share/doc/python-sphinxbase-dbg
	ln -s python-sphinxbase debian/python-sphinxbase-dbg/usr/share/doc/python-sphinxbase-dbg
	dh_compress -X.py
	dh_fixperms
	dh_pysupport
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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