#!/usr/bin/make -f

PY2VERS := $(shell pyversions -s)
PY3VERS := $(shell py3versions -s)

export CFLAGS = $(shell dpkg-buildflags --get CFLAGS)
export CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

%:
	dh $@ --with python2,python3

override_dh_auto_install:
	set -e ; \
	for python in $(PY2VERS); do \
		$$python setup.py install --install-layout=deb --root $(CURDIR)/debian/python-psutil; \
	done
	set -e ; \
	for python in $(PY3VERS); do \
		$$python setup.py install --install-layout=deb --root $(CURDIR)/debian/python3-psutil; \
	done

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_OPTIONS)),)
	# run tests
	-for test in test_memory_leaks.py test_psutil.py ;\
	do \
	    for python in $(PY2VERS) ; do \
  	        echo "running "test/$$test" on "$$python ; \
		LIB=$$($$python -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print b.build_platlib") ; \
	        PYTHONPATH=$$LIB $$python test/$$test ; \
	    done ; \
	    for python in $(PY3VERS) ; do \
  	        echo "running "test/$$test" on "$$python ; \
		LIB=$$($$python -c "from distutils.command.build import build ; from distutils.core import Distribution ; b = build(Distribution()) ; b.finalize_options() ; print (b.build_platlib)") ; \
	        PYTHONPATH=$$LIB $$python test/$$test ; \
	    done ; \
	done
endif

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

override_dh_installdocs:
	dh_installdocs -X.DS_Store

override_dh_compress:
	dh_compress -X.py

PACKAGE = python-psutil
SRC_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | sed 's/^Version: \(.*\)-.*/\1/')
SVN_REVISION := $(shell echo $(SRC_VERSION) | awk -F"+" '{ print $$2 }' | sed 's/svn//' )
TARBALL = $(PACKAGE)_$(SRC_VERSION).orig.tar.gz

.PHONY: get-orig-source
get-orig-source:
	rm -rf get-orig-source $(TARBALL)
	mkdir get-orig-source
	svn export -r $(SVN_REVISION) http://psutil.googlecode.com/svn/trunk get-orig-source/$(PACKAGE)-$(SRC_VERSION)
	tar czf $(TARBALL) -C get-orig-source $(PACKAGE)-$(SRC_VERSION)
	rm -rf get-orig-source
	echo "  "$(TARBALL)" created; move it to the right destination to build the package"
