#! /usr/bin/make -f

include /usr/share/dpatch/dpatch.make

# 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

PYVERS := $(shell pyversions -vs)
CURLVERS := $(strip $(shell /usr/bin/curl-config --version | awk '{print $$2}'))

unexport LDFLAGS
export FFLAGS="-fPIC"


build: build-stamp
build-stamp: DH_OPTIONS=
build-stamp: patch-stamp $(PYVERS:%=build-python%)
	touch build-stamp

build-python%:
	dh_testdir
	python$* setup.py build
	python$*-dbg setup.py build

	set -e ;\
	for test in "tests/test_internals.py" "tests/test_memleak.py" ; do \
	    PYTHONPATH=`ls -d $(CURDIR)/build/lib.*-*-$*` \
	        python$* $$test; \
	done
	touch $@

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-*

	for py in $(PYVERS); do \
	    python$$py setup.py clean; \
	    python$$py-dbg setup.py clean; \
	done

	rm -rf build
	-find -name '*.py[co]' | xargs rm -f
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	for py in $(PYVERS); do \
  	    python$$py setup.py install --root=$(CURDIR)/debian/python-pycurl-openssl --install-layout=deb; \
 	    python$$py-dbg setup.py install --root=$(CURDIR)/debian/python-pycurl-openssl-dbg --install-layout=deb; \
	done

	rm -rf $(CURDIR)/debian/python-pycurl-openssl-dbg/usr/lib/python*/*-packages/curl
	rm -f $(CURDIR)/debian/python-pycurl-openssl-dbg/usr/lib/python*/*-packages/pycurl-*.egg-info

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

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	cp -a doc/*.html debian/python-pycurl-openssl/usr/share/doc/python-pycurl-openssl/html
	dh_installdocs -a -A TODO
	dh_installexamples -a -A examples/* 
	dh_installchangelogs -a ChangeLog
	dh_pysupport -a
	dh_strip -ppython-pycurl-openssl --dbg-package=python-pycurl-openssl-dbg
	rm -rf debian/python-pycurl-openssl-dbg/usr/share/doc/python-pycurl-openssl-dbg
	ln -s python-pycurl-openssl debian/python-pycurl-openssl-dbg/usr/share/doc/python-pycurl-openssl-dbg
	dh_compress -a -X.py
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	sed -i '/shlibs:Depends/s,\(libcurl[0-9]*-openssl (>= \)[^)]*\().*\),\1$(CURLVERS)\2,' debian/python-pycurl*.substvars
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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