#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_REVISION ?= "+deb$(shell echo $(DEB_VERSION) | cut -d- -f2)"

znc_backlog_pth = $(CURDIR)/debian/contrib/znc-backlog
znc_backlog_ver = 0.20180824+$(DEB_VERSION)
znc_push_pth = $(CURDIR)/debian/contrib/znc-push
znc_push_ver = 1.0.0+git20190521.78d0385+$(DEB_VERSION)

DEB_CONFIGURE_EXTRA_FLAGS := \
	-DCMAKE_INSTALL_LIBDIR=lib \
	-DVERSION_EXTRA=$(DEB_REVISION) \
	-DGTEST_ROOT="/usr/src/googletest/googletest" \
	-DGMOCK_ROOT="/usr/src/googletest/googlemock" \
	-DWANT_SYSTEMD=false \
	-DWANT_TCL=true \
	-DWANT_CYRUS=true \
	-DWANT_PYTHON=true \
	-DWANT_PERL=true \
	-DWANT_I18N=true

export VERBOSE=1

%:
	dh $@ --with python3 --buildsystem=cmake

override_dh_auto_clean:
	dh_auto_clean
	dh_auto_clean --buildsystem=makefile -D$(znc_backlog_pth)
	dh_auto_clean --buildsystem=makefile -D$(znc_push_pth)

override_dh_auto_configure:
	dh_auto_configure -- $(DEB_CONFIGURE_EXTRA_FLAGS)

build_contrib_plugins: export PATH := $(CURDIR)/debian/tmp/usr/bin:$(PATH)
build_contrib_plugins: export CXXFLAGS += -I$(CURDIR)/debian/tmp/usr/include
build_contrib_plugins: export CMAKE_PREFIX_PATH := $(CURDIR)/debian/tmp/usr/share/znc/cmake
build_contrib_plugins:
	dh_auto_build --buildsystem=makefile -D$(znc_backlog_pth)
	dh_auto_build --buildsystem=makefile -D$(znc_push_pth) -- \
		version="$(znc_push_ver)" curl=yes

override_dh_installsystemd:
	dh_installsystemd --no-start --no-enable --restart-after-upgrade

override_dh_python3:
	dh_python3 \
		-p znc-python \
		--exclude=znc.py \
		--exclude=znc_core.py

override_dh_install: build_contrib_plugins
	dh_install
	dh_install -p znc-dev \
		usr/lib/pkgconfig/* usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/
	# Fix pkg-config file.
	sed '/^Cflags:/d' -i $(CURDIR)/debian/znc-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/znc.pc
	sed '/Libs:/d' -i $(CURDIR)/debian/znc-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/znc.pc

override_dh_missing:
	dh_missing --fail-missing

override_dh_installchangelogs:
	dh_installchangelogs -pznc-backlog -XChangeLog.md
	dh_installchangelogs -pznc-push -XChangeLog.md
	dh_installchangelogs --remaining-packages

override_dh_gencontrol:
	dh_gencontrol -pznc-backlog -- -v$(znc_backlog_ver) -VbinVer=$(DEB_VERSION)
	dh_gencontrol -pznc-push -- -v$(znc_push_ver) -VbinVer=$(DEB_VERSION)
	dh_gencontrol --remaining-packages