#!/usr/bin/make -f
# -*- makefile -*-

DRAFTS=no
DOCS=yes

# OBS build: add
#   Macros:
#   %_with_drafts 1
# at the BOTTOM of the OBS prjconf
OBS_BUILD_CFG=/.build/build.dist
ifeq ("$(wildcard $(OBS_BUILD_CFG))","")
BUILDCONFIG=$(shell ls -1 /usr/src/packages/SOURCES/_buildconfig* | head -n 1)
endif
ifneq ("$(wildcard $(OBS_BUILD_CFG))","")
ifneq ("$(shell grep drafts $(OBS_BUILD_CFG))","")
DRAFTS=yes
endif
ifneq ("$(shell grep python_cffi $(OBS_BUILD_CFG))","")
PYTHON_CFFI=yes
endif
endif

# User build: DEB_BUILD_OPTIONS=drafts dpkg-buildpackage
ifneq (,$(findstring drafts,$(DEB_BUILD_OPTIONS)))
DRAFTS=yes
endif

ifneq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
DOCS=no
endif
ifneq (,$(findstring nodoc,$(DEB_BUILD_PROFILES)))
DOCS=no
endif

# User build: DEB_BUILD_OPTIONS=python_cffi dpkg-buildpackage
ifneq (,$(findstring python_cffi,$(DEB_BUILD_OPTIONS)))
ifeq (,$(findstring nopython,$(DEB_BUILD_OPTIONS)))
ifeq (,$(findstring nopython,$(DEB_BUILD_PROFILES)))
PYTHON_CFFI=yes
WITH_PYTHON = --with=python2,python3
endif
endif
endif

ifeq (yes,$(PYTHON_CFFI))
export PYBUILD_NAME=czmq-cffi
export PKG_CONFIG_PATH=$(CURDIR)/bindings/python_cffi:$PKG_CONFIG_PATH

override_dh_auto_build:
	dh_auto_build
	# Problem: we need pkg-config points to built and not yet installed copy of czmq
	# Solution: chicken-egg problem - let's make "fake" pkg-config file
	sed -e "s@^libdir.*@libdir=$(CURDIR)/src/.libs@" 		-e "s@^includedir.*@includedir=$(CURDIR)/include@" 		src/libczmq.pc > bindings/python_cffi/libczmq.pc
	dh_auto_build --sourcedirectory=bindings/python_cffi -O--buildsystem=pybuild

override_dh_auto_install:
	dh_auto_install
	dh_auto_install --sourcedirectory=bindings/python_cffi -O--buildsystem=pybuild

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean -O--buildsystem=pybuild
	rm -f bindings/python_cffi/*.pc
	rm -rf bindings/python_cffi/*.egg-info/
else
WITH_PYTHON =
# This tells debhelper to avoid building the python CFFI packages
DH_OPTIONS += -Npython-czmq-cffi
DH_OPTIONS += -Npython3-czmq-cffi
export DH_OPTIONS
endif

# Workaround for automake < 1.14 bug
$(shell dpkg --compare-versions `dpkg-query -W -f='$${Version}\n' automake` lt 1:1.14 && mkdir -p config)
$(shell test -f src/Makemodule-local.am || touch src/Makemodule-local.am)

override_dh_strip:
	dh_strip --dbg-package=czmq-dbg

override_dh_auto_test:
	echo "Skipped for now"

override_dh_auto_configure:
	dh_auto_configure -- \
		--with-uuid=yes \
		--with-libsystemd=yes \
		--with-liblz4=yes \
		--with-docs=$(DOCS) \
		--enable-drafts=$(DRAFTS)

%:
	dh $@ \
		$(WITH_PYTHON) \
		--with autoreconf
