#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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


PYVERS=$(shell pyversions -vs)
PY3VERS=$(shell py3versions -vs)

%:
	dh $@ --with=python2 --with=python3 --with=autoreconf

override_dh_build:: build-python-all build-python3-all

build-python-all: build-swig $(PYVERS:%=build-python%)
	touch $@

build-python3-all: build-swig $(PY3VERS:%=build-python3%)
	touch $@

build-swig:
	dh_build
	touch $@

build-python%:
	cd python && python$* setup.py build
	touch $@

build-python3%:
	cd python3 && python$* setup.py build
	touch $@

override_dh_install:: build $(PYVERS:%=install-python2%) $(PY3VERS:%=install-python3%)
install-python2%:
	cd python && python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python-gearman.libgearman
	find $(CURDIR)/debian/python-gearman.libgearman -name '__init__.py' -delete

install-python3%:
	cd python3 && python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python3-gearman.libgearman

override_dh_auto_configure::
	dh_auto_configure -- --without-ruby --without-lua
	# These get rm'd by make clean, but its better this way as
	# There won't be any complaints about ignoring the deletion since we'll rename these back in clean
	[ -f python/libgearman.c.orig ] || [ -f python/libgearman.c ] && mv -f python/libgearman.c python/libgearman.c.orig || true
	[ -f python3/libgearman.c.orig ] || [ -f python3/libgearman.c ] && mv -f python3/libgearman.c python3/libgearman.c.orig || true
	rm -f python/libgearman.c
	rm -f python3/libgearman.c

override_dh_clean::
	dh_clean
	rm -f build-python*
	rm -f build-swig
	rm -rf .deps/
	rm -f python/debian
	rm -rf python/ez_setup.pyc
	rm -rf python/build/
	rm -rf python3/gearman/_libgearman*so
	rm -rf python3/gearman/__pycache__
	rm -rf python/python_libgearman.egg-info/
	rm -rf python3/build/
	[ ! -f python/libgearman.c.orig ] || mv -f python/libgearman.c.orig python/libgearman.c
	[ ! -f python3/libgearman.c.orig ] || mv -f python3/libgearman.c.orig python3/libgearman.c
