#!/usr/bin/make -f

PYVERS	:= $(shell pyversions -s)
PYDEF	:= $(shell pyversions -d)
OTHER_VERS = $(filter-out $(PYDEF), $(PYVERS))

DEB_SRCDIR = .
DEB_BUILDDIR = $(DEB_SRCDIR)/build-$(PYDEF)

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/simple-patchsys.mk
include /usr/share/cdbs/1/class/gnome.mk

# some contansts for arch-build
PKG=launchpad-integration
DEBVER=$(shell dpkg-parsechangelog |sed -n -e '/^Version:/s/^Version: //p')
DEB_BUILD_PROG:=debuild --preserve-envvar PATH --preserve-envvar CCACHE_DIR -us -uc $(DEB_BUILD_PROG_OPTS)

DEB_CONFIGURE_SCRIPT_ENV += LDFLAGS="-Wl,-O1 -Wl,--as-needed"

DEB_DH_MAKESHLIBS_ARGS_liblaunchpad-integration1 := -V 'liblaunchpad-integration1 (>= 0.1.17)'

DEB_DBG_PACKAGE_python-launchpad-integration = python-launchpad-integration-dbg

configure/launchpad-integration:: stamp-configure-pyversions
stamp-configure-pyversions:
ifneq (,$(strip $(OTHER_VERS)))
	: # configure for other python versions
	set -e; \
	for v in $(OTHER_VERS); do \
	  rm -rf build-$$v; \
	  mkdir build-$$v; \
	  cd build-$$v; \
	  PYTHON=/usr/bin/$$v ../configure $(filter-out --srcdir=%, $(DEB_CONFIGURE_NORMAL_ARGS) $(DEB_CONFIGURE_EXTRA_FLAGS)); \
	  cd ..; \
	done
endif
	: # configure for python debug versions
	set -e; \
	for v in $(PYVERS); do \
	  rm -rf dbg-build-$$v; \
	  mkdir dbg-build-$$v; \
	  cd dbg-build-$$v; \
	  PYTHON=/usr/bin/$$v-dbg CFLAGS="$(CFLAGS) -O0" ../configure $(filter-out --srcdir=%, $(DEB_CONFIGURE_NORMAL_ARGS) $(DEB_CONFIGURE_EXTRA_FLAGS)); \
	  cd ..; \
	done
	touch stamp-configure-pyversions

build/launchpad-integration:: stamp-build-pyversions
stamp-build-pyversions: stamp-configure-pyversions
ifneq (,$(strip $(OTHER_VERS)))
	: # build for other python versions
	set -e; \
	for v in $(OTHER_VERS); do \
	  $(MAKE) -C dbg-build-$$v; \
	done
endif
	: # build for python debug versions
	set -e; \
	for v in $(PYVERS); do \
	  $(MAKE) -C dbg-build-$$v; \
	done
	touch stamp-build-pyversions

binary-install/launchpad-integration:: stamp-build-pyversions
ifneq (,$(strip $(OTHER_VERS)))
	: # install for other python versions
	set -e; \
	for v in $(OTHER_VERS); do \
	  $(MAKE) -C build-$$v DESTDIR=$(CURDIR)/debian/tmp install; \
	done
endif
	: # install for python debug versions
	set -e; \
	for v in $(PYVERS); do \
	  $(MAKE) -C dbg-build-$$v DESTDIR=$(CURDIR)/debian/python-launchpad-integration-dbg install; \
	done
	find debian/python-*-dbg ! -type d ! -name '*.so' | xargs rm -f
	find debian/python-*-dbg -depth -empty -exec rmdir {} \;
	for i in $$(find debian/python-*-dbg -name '*.so'); do \
	  b=$$(basename $$i .so); \
	  mv $$i $$(dirname $$i)/$${b}_d.so; \
	done

	dh_pycentral -plaunchpad-integration

binary-install/python-launchpad-integration:: binary-install/launchpad-integration
	DH_PYCENTRAL=nomove dh_pycentral -ppython-launchpad-integration

binary-install/python-launchpad-integration-dbg:: binary-install/launchpad-integration
	: # XXXX

binary-predeb/python-launchpad-integration-dbg::
	rm -rf debian/python-launchpad-integration-dbg/usr/share/doc/python-launchpad-integration-dbg
	ln -s python-launchpad-integration debian/python-launchpad-integration-dbg/usr/share/doc/python-launchpad-integration-dbg

arch-build:
	rm -rf debian/arch-build
	mkdir -p debian/arch-build/$(PKG)-$(DEBVER)
	tar -c --no-recursion --exclude=arch-build -f - `bzr inventory` | (cd debian/arch-build/$(PKG)-$(DEBVER);tar xf -)
	(cd debian/arch-build/$(PKG)-$(DEBVER) && ./autogen.sh && make distclean && $(DEB_BUILD_PROG))

clean::
	rm -f stamp-*-pyversions
	rm -rf build-python* dbg-build-python*
