#!/usr/bin/make -f
#DH_VERBOSE = 1

# See dpkg-buildflags(1)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND =
# See dpkg-gensymbols(1)
export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

# Do not build ibus-tests on (compatibility-only) Ubuntu i386, as some of
# ibus-tests depends are not in the compatibility layer
ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes) $(DEB_HOST_ARCH),yes i386)
   skip_packages = -Nibus-tests
endif

DH_FLAGS = --with gir

# Ubuntu language pack support (dh_translations)
ifeq ($(shell dpkg-vendor --is Ubuntu && echo yes),yes)
DH_FLAGS += --with translations
endif


%:
	dh $@ $(DH_FLAGS) --buildsystem=meson

CONFIGURE_FLAGS = \
		--default-library=shared \
		-D appindicator=true \
		-D emoji-dict=true \
		-D gtk2=enabled \
		-D gtk3=true \
		-D gtk4=true \
		-D introspection=true \
		-D vapi=enabled \
		-D xim=enabled \
		-D no-snooper-apps='gnome-do,Do.*,firefox.*,.*chrome.*,.*chromium.*,terminator' \
		-D ucd-dir=/usr/share/unicode \
		$(NULL)

ifeq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
CONFIGURE_FLAGS += -D gtk-doc=true
else
CONFIGURE_FLAGS += -D gtk-doc=false
endif

# Disable distro-tests, which needs to be run in a GNOME session
ifeq (,$(filter noinsttest,$(DEB_BUILD_PROFILES)))
CONFIGURE_FLAGS += -D install-tests=true -D distro-tests=false
else
CONFIGURE_FLAGS += -D install-tests=false -D distro-tests=false
endif

# memconf is not needed on nocheck or noinsttest build profile. But these
# build profiles should not change package contents (See
# https://wiki.debian.org/BuildProfileSpec). So always use this flag.
CONFIGURE_FLAGS += -D memconf=true

ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFIGURE_FLAGS += -D wayland=enabled -D systemd-services=enabled
else
CONFIGURE_FLAGS += -D wayland=disabled -D systemd-services=disabled
endif

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE_FLAGS)

override_dh_auto_install:
	dh_auto_install
	desktop-file-install --delete-original \
		--dir debian/tmp/usr/share/applications \
		debian/tmp/usr/share/applications/*

override_dh_autoreconf:
	# dh_autoreconf seems to be triggered by autotools configure.ac.
	# Just ignore this.
	test -f configure.ac

override_dh_install:
	dh_install -X.la -X.pyc -X.pyo -X__pycache__

override_dh_missing:
	dh_missing --fail-missing -X.la -X.pyc -X.pyo

override_dh_auto_test:
	# NOTE: Do not use debhelper 13+ provided $HOME and XDG dirs. The
	# provided $XDG_CACHE_HOME is too long so the ibus-daemon socket name
	# length easily exceeds the OS limit (107 bytes in Linux).
	mkdir -p -m700 $(CURDIR)/debian/tmp-home/run
	env \
	  GVFS_DISABLE_FUSE=1 \
	  HOME=$(CURDIR)/debian/tmp-home \
	  XDG_CACHE_HOME= \
	  XDG_CONFIG_HOME= \
	  XDG_DATA_HOME= \
	  XDG_DATA_DIRS= \
	  XDG_RUNTIME_DIR=$(CURDIR)/debian/tmp-home/run \
	  DISABLE_DAEMONIZE_IN_TESTS=1 \
	  dh_auto_test -- --no-suite ibus:display
	rm -rf $(CURDIR)/debian/tmp-home

override_dh_builddeb:
	dh_builddeb $(skip_packages)

override_dh_gencontrol:
	dh_gencontrol $(skip_packages)

override_dh_shlibdeps:
	dh_shlibdeps -pibus-gtk  -- -xlibgtk2.0-0t64
	dh_shlibdeps -pibus-gtk3 -- -xlibgtk-3-0t64
	dh_shlibdeps -pibus-gtk4 -- -xlibgtk-4-1
	dh_shlibdeps --remaining-packages
