#!/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

DEB_CONFIGURE_SCRIPT_ENV += LDFLAGS="-Wl,-O1 -Wl,--as-needed" CSC=/usr/bin/mono-csc

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

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

# Need this export to prevent mono trying (and failing, on buildds) to create ~/.wapi
export MONO_DISABLE_SHM = 1

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 $(DEB_CONFIGURE_SCRIPT_ENV) ../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 $(DEB_CONFIGURE_SCRIPT_ENV) 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

binary-install/liblaunchpad-integration1.0-cil::
	dh_clifixperms -pliblaunchpad-integration1.0-cil
	dh_makeclilibs -m 0.1.33
	dh_installcligac

binary-predeb/liblaunchpad-integration1.0-cil::
	dh_clideps -pliblaunchpad-integration1.0-cil

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