#!/usr/bin/make -f

UPSTREAM_GIT := https://github.com/svinota/pyroute2
include /usr/share/openstack-pkg-tools/pkgos.make

#export PYBUILD_NAME=pyroute2
#export PYBUILD_BEFORE_TEST=cp -r {dir}/tests {build_dir}/
#export PYBUILD_AFTER_TEST=rm -rf {build_dir}/tests
#export PYBUILD_TEST_ARGS=tests/unit
#export PYBUILD_TEST_NOSE=1

export python=python3

%:
	dh $@ --with python3,sphinxdoc

override_dh_auto_clean:
	rm -rf build
	find . -type f -name "*.pyc" | xargs rm -fr
	$(MAKE) clean

override_dh_auto_test:
	echo "Doing nothing..."

override_dh_auto_install:
	echo "Do nothing..."

override_dh_install:
	$(MAKE) VERSION
	$(MAKE) setup
	mkdir debian/test-installs

	# Install in a temp folder for tests
	MYDIR=$(CURDIR) ; set -e ; set -x ; for PACKAGE in pyroute2 pyroute2.core pyroute2.ethtool pyroute2.ipdb pyroute2.ipset pyroute2.ndb pyroute2.nftables pyroute2.nslink pyroute2.protocols ; do \
		cd $${MYDIR}/$$PACKAGE ; \
		python3 setup.py install --install-layout=deb --root $${MYDIR}/debian/test-installs ; \
	done ; cd $${MYDIR}

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	echo "Do not run tests for now..."
	#set -e ; set -x ; cd tests ; PYTHONPATH=.:$(CURDIR)/debian/test-installs/usr/lib/python3/dist-packages python3 -m pytest
endif

	# Install in the packaging folders
	MYDIR=$(CURDIR) ; set -e ; set -x ; for PACKAGE in pyroute2 pyroute2.minimal pyroute2.core pyroute2.ethtool pyroute2.ipdb pyroute2.ipset pyroute2.ndb pyroute2.nftables pyroute2.nslink pyroute2.protocols ; do \
		cd $${MYDIR}/$$PACKAGE ; \
		if [ $$PACKAGE = "pyroute2.minimal" ] ; then \
			DESTPKG=pyroute2-minimal ; \
		else \
			DESTPKG=$$PACKAGE ; \
		fi ; \
		python3 setup.py install --install-layout=deb --root $${MYDIR}/debian/python3-$$DESTPKG ; \
	done ; cd $${MYDIR}

	dh_install

override_dh_installchangelogs:
	dh_installchangelogs CHANGELOG.md

override_dh_sphinxdoc:
	PYTHONPATH=$(CURDIR)/debian/test-installs/usr/lib/python3/dist-packages python3 -m sphinx -b html docs $(CURDIR)/debian/python-pyroute2-doc/usr/share/doc/python-pyroute2-doc/html
	dh_sphinxdoc
