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

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

ifneq (,$(findstring verbose,$(DEB_BUILD_OPTIONS)))
	VERBOSE="VERBOSE=1"
endif

# get upstream version for get-orig-source target
VERSION:= $(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/\-.*$$//')
# allow using whichever supported python is installed
PYTHON2:=$(shell pyversions -r debian/control|xargs which|xargs basename)
include /usr/share/python/python.mk
pyalldo=set -e; $(foreach py, $(PYTHON2) $(PYTHON3), PYTHONPATH=. $(py) $(1);)
install_dir=$(call py_libdir, $(PYTHON2))

configure: configure-stamp
configure-stamp: build-setup
	dh_testdir
	touch configure-stamp

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(call pyalldo , test/plugintest.py)
	$(call pyalldo , test/pybitclient-test.py)
	$(call pyalldo , test/webapi-test.py)
	touch $@

build-setup: setup.py
	dh_testdir
	python setup.py build
	xsltproc --nonet /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl debian/pybit-client.1.xml
	xsltproc --nonet /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl debian/pybit-watcher.1.xml
	xsltproc --nonet /usr/share/xml/docbook/stylesheet/docbook-xsl/manpages/docbook.xsl debian/pybit-web.1.xml
	mv pybit-client.1 debian/
	mv pybit-web.1 debian/
	mv pybit-watcher.1 debian/

clean:
	dh_testdir
	dh_testroot
	$(RM) build-stamp configure-stamp
	$(RM) -r build pybit_client.egg-info pybit_common.egg-info pybit_web.egg-info
	$(RM) -r build dist pybit-client.1 pybit.egg-info pybit-web.1 pybit-watcher.1
	[ ! -f Makefile ] || $(MAKE) clean
	find . -name '*.pyc' -delete
	dh_clean
	debconf-updatepo

sdist-orig:
	python setup.py sdist
	mv -v dist/pybit-$(VERSION).tar.gz ../pybit_$(VERSION).orig.tar.gz
	$(RM) -r pybit.egg-info

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	python setup.py install --root="$(CURDIR)/debian/tmp" --no-compile $(py_setup_install_args)
	mkdir -p debian/pybit-common$(install_dir)
	mkdir -p debian/pybit-client$(install_dir)
	mkdir -p debian/pybit-web$(install_dir)
	mv debian/tmp$(install_dir)/pybit/ debian/pybit-common$(install_dir)
	mv debian/tmp$(install_dir)/pybitclient/ debian/pybit-client/$(install_dir)
	mv debian/tmp$(install_dir)/pybitweb/ debian/pybit-web$(install_dir)

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installinit
	dh_lintian
	dh_installdebconf
	dh_install $(exclusions) --exclude=.git
	dh_compress
	dh_python2
	dh_link
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build install

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure sdist-orig
