#!/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 python3_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=python3
endif
endif
endif

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

override_dh_auto_build:
	dh_auto_build -O--buildsystem=autoconf
	# Problem: we need pkg-config points to built and not yet installed copy of malamute
	# 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/libmlm.pc > bindings/python_cffi/libmlm.pc
	dh_auto_build --sourcedirectory=bindings/python_cffi -O--buildsystem=pybuild

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

override_dh_auto_clean:
	dh_auto_clean -O--buildsystem=autoconf
	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 += -Npython3-malamute-cffi
export DH_OPTIONS

override_dh_auto_build:
	dh_auto_build -O--buildsystem=autoconf

override_dh_auto_install:
	dh_auto_install -O--buildsystem=autoconf

override_dh_auto_clean:
	dh_auto_clean -O--buildsystem=autoconf
endif

$(shell test -f src/Makemodule-local.am || touch src/Makemodule-local.am)

override_dh_strip:
	dh_strip --dbg-package=malamute-dbg

override_dh_auto_test:
	echo "Skipped for now"

override_dh_auto_configure:
	dh_auto_configure -- \
		--with-systemd-units \
		--with-systemdsystemunitdir=/lib/systemd/system \
		--with-docs=$(DOCS) \
		--enable-drafts=$(DRAFTS)

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