#!/usr/bin/make -f

export DH_VERBOSE = 1
export DH_OPTIONS = -v
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

export PYBUILD_NAME=keyman-config
export PYBUILD_INSTALL_ARGS=--install-scripts=/usr/share/keyman-config/

export KEYMAN_PKG_BUILD=1

# xenial needs this to be explicit
export LC_ALL=C.UTF-8

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

# Workaround for https://bugs.debian.org/1041499 and
# https://github.com/mesonbuild/meson/issues/12017
# --native-file got added in Meson 0.49, so we check for that version here
MESON_049_OR_HIGHER := $(shell expr `meson --version | sed -e 's/\.\([0-9][0-9]\)/\1/g' -e 's/\.\([0-9]\)/0\1/g' -e 's/^[0-9]\{3,4\}$$/&00/'` \>= 04900)

configure_options :=

ifeq ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))
ifeq ($(MESON_049_OR_HIGHER), 1)
configure_options += --native-file=$(CURDIR)/debian/meson/no-exe-wrapper.ini
endif
endif

# Unfortunately dh-python 3.20180325 (bionic) doesn't provide the virtual dh-sequence-python3
# package, so we'll have to pass --with-python3 here
%:
	dh $@ --with-python3

override_dh_auto_configure:
	# keymankeyboardprocessor
	cd core && \
		./build.sh configure -- --wrap-mode=nodownload --prefix=/usr --sysconfdir=/etc \
			--localstatedir=/var --libdir=lib/$(DEB_TARGET_MULTIARCH) \
			--libexecdir=lib/$(DEB_TARGET_MULTIARCH) $(configure_options)
	# ibus-keyman
	cd linux/ibus-keyman && \
		cp -f ../../VERSION.md VERSION && \
		export KEYMAN_PROC_CFLAGS="-I$(CURDIR)/core/build/arch/release/include -I$(CURDIR)/common/include -I$(CURDIR)/core/include" && \
		export KEYMAN_PROC_LIBS="-L$(CURDIR)/core/build/arch/release/src -lkmnkbp0" && \
		export PKG_CONFIG_PATH=$(CURDIR)/core/build/arch/release/meson-private && \
		./autogen.sh && \
		./configure --prefix=/usr
	# keyman-config

override_dh_auto_build:
	cp linux/keyman-config/resources/keyman.sharedmimeinfo debian/
	# keymankeyboardprocessor
	cd core && ./build.sh build
	# ibus-keyman
	cd linux/ibus-keyman && make
	# keyman-config
	cd linux/keyman-config && \
		make man && \
		sed -i -e "s/^__pkgversion__ = \"[^\"]*\"/__pkgversion__ = \"$(DEB_VERSION)\"/g" keyman_config/version.py && \
		make compile-po
	dh_auto_build --sourcedir=linux/keyman-config --buildsystem=pybuild $@

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# keymankeyboardprocessor
	cd core && ./build.sh tests
	# ibus-keyman
	cd linux/ibus-keyman && make check VERBOSE=1
	# keyman-config
	dh_auto_test --sourcedir=linux/keyman-config --buildsystem=pybuild $@
endif

override_dh_auto_install:
	# keymankeyboardprocessor
	install -d $(CURDIR)/debian/tmp
	cd core && DESTDIR=$(CURDIR)/debian/tmp ./build.sh install
	# ibus-keyman
	cd linux/ibus-keyman && make install DESTDIR=$(CURDIR)/debian/tmp
	# keyman-config
	install -d $(CURDIR)/debian/keyman/usr/share/
	cp -r linux/keyman-config/locale/ $(CURDIR)/debian/keyman/usr/share/
	rm $(CURDIR)/debian/keyman/usr/share/locale/*.po*
	dh_auto_install --sourcedir=linux/keyman-config --buildsystem=pybuild $@
	# Unfortunately bash-completion 2.10 (focal) doesn't yet provide dh-sequence-bash-completion,
	# which we could add as build-dependency, so we'll have to explicitly call
	# dh_bash_completion here...
	dh_bash-completion -O--buildsystem=pybuild
	dh_python3 -O--buildsystem=pybuild

override_dh_missing:
	dh_missing --fail-missing
