#!/usr/bin/make -f

DEB_BUILDDIR = debian/build
DEB_MAKE_DESTDIRSKEL = $(CURDIR)/debian/install/@FLAVOR@
DEB_MAKE_FLAVORS = shared shared_udeb
DISABLE_UPDATE_UPLOADERS = 1

include /usr/share/cdbs/1/rules/autoreconf.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/gnome.mk
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk
include /usr/share/gnome-pkg-tools/1/rules/gnome-version.mk

GNOME_MODULE = gtk+

# Enable parallel build support
DEB_BUILD_PARALLEL = 1

# Ensure the build aborts when there are still references to undefined symbols
# currently fails at least in the im-multipress im module
#LDFLAGS += -Wl,-z,defs

# Make the linker work a bit harder so dynamic loading can be done faster
LDFLAGS += -Wl,-O1

APIVER := 4
SONAME := 0

LIBDIR := usr/lib/$(DEB_HOST_MULTIARCH)

# earliest version that this release has backwards binary compatibility for
GTK_BINARY_VERSION := 4.0.0

# Gtk binary version virtual Provide
GTK_BINVER_DEP := gtk$(APIVER)-binver-$(GTK_BINARY_VERSION)

# relative base directory for configuration
CONFDIR := etc/gtk-4.0

# relative base directory for all types of modules
MODULES_BASE_PATH := $(LIBDIR)/gtk-4.0/$(GTK_BINARY_VERSION)
OLD_MODULES_BASE_PATH := usr/lib/gtk-4.0/$(GTK_BINARY_VERSION)

# package names
SHARED_PKG := libgtk-$(APIVER)-$(SONAME)
COMMON_PKG := libgtk-$(APIVER)-common
DEV_PKG := libgtk-$(APIVER)-dev
UDEB_PKG := $(SHARED_PKG)-udeb
DOC_PKG := libgtk-$(APIVER)-doc
BIN_PKG := libgtk-$(APIVER)-bin
EXAMPLES_PKG := gtk-$(APIVER)-examples

# files larger than 4k in size will be compressed by dh_compress
# so append a .gz suffix to avoid dangling symlinks
NEWS := NEWS$(shell find -maxdepth 1 -size +4k -name NEWS -exec echo ".gz" \;)
README := README$(shell find -maxdepth 1 -size +4k -name README -exec echo ".gz" \;)

FAIL_MISSING :=

# When building all packages, run dh_install with --fail-missing
binary: FAIL_MISSING=--fail-missing

DEB_DH_INSTALL_ARGS += --sourcedir=debian/install/shared $(FAIL_MISSING) -X.la \
	-Xgtk4-update-icon-cache.1 -Xgtk4-update-icon-cache -Xpkgconfig/gail \
	-Xusr/share/gettext/its -Xusr/share/glib-2.0/schemas/

DEB_DH_INSTALL_ARGS_$(UDEB_PKG) += --sourcedir=debian/install/shared_udeb

DEB_DH_AUTORECONF_ARGS = "NOCONFIGURE=true ./autogen.sh"

DEB_CONFIGURE_EXTRA_FLAGS = \
			--libdir=/$(LIBDIR) \
			--enable-test-print-backend

DEB_CONFIGURE_FLAGS_shared = \
			--enable-broadway-backend \
			--disable-mir-backend \
			--enable-x11-backend \
			--enable-gtk-doc \
			--enable-man \
			--enable-shared \
			--enable-introspection \
			--enable-cloudprint \
			--disable-static

ifeq (,$(filter stage1,$(DEB_BUILD_PROFILES)))
DEB_CONFIGURE_FLAGS_shared += --enable-colord
else
DEB_CONFIGURE_FLAGS_shared += --disable-colord
endif

ifeq (linux,$(DEB_HOST_ARCH_OS))
DEB_CONFIGURE_FLAGS_shared += --enable-wayland-backend
else
DEB_CONFIGURE_FLAGS_shared += --disable-wayland-backend
endif

DEB_CONFIGURE_FLAGS_shared_udeb = \
			--disable-broadway-backend \
			--disable-wayland-backend \
			--disable-mir-backend \
			--enable-x11-backend \
			--disable-introspection \
			--disable-cloudprint \
			--disable-colord \
			--disable-xcomposite \
			--disable-xdamage \
			--disable-xfixes \
			--disable-xrandr

# Avoid test failures on buildd environments
export HOME=$(CURDIR)/debian/build
export XDG_RUNTIME_DIR=$(HOME)
# So that gsettings can find the (uninstalled) gtk schemas
export XDG_DATA_DIRS=/usr/share:$(CURDIR)/debian/build
# Put these back to their defaults if we are not running with a clean
# environment, so that they are based on the temporary $HOME above
unexport XDG_CACHE_HOME
unexport XDG_CONFIG_DIRS
unexport XDG_CONFIG_HOME
unexport XDG_DATA_HOME
# Get failing tests' stdout/err so we have some information when a build fails
export VERBOSE=1

DEB_MAKE_CHECK_TARGET = -k check || true
DEB_INSTALL_DOCS_ALL = AUTHORS
DEB_INSTALL_DOCS_$(COMMON_PKG) = README NEWS
DEB_INSTALL_DOCS_$(DOC_PKG) += -X.in
DEB_DH_MAKESHLIBS_ARGS_ALL += -X$(MODULES_BASE_PATH)
DEB_DH_MAKESHLIBS_ARGS_$(SHARED_PKG) += -V --add-udeb=$(UDEB_PKG) -- -c4
DEB_DH_FIXPERMS_ARGS_ALL += -X$(LIBDIR)/$(SHARED_PKG)

# macro computing the list of 'debian/<pkg>.*" files which have a
# corresponding ".in" file; pass the list of packages in $(1)
dh_subst_files = $(patsubst %.in,%,$(wildcard $(addprefix debian/, $(addsuffix *.in, $(1)))))

debian/%: debian/%.in
	dh_testdir
	sed \
		-e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@VERSION@#$(DEB_UPSTREAM_VERSION)#g" \
		-e "s#@GTK_BINVER_DEP@#$(GTK_BINVER_DEP)#g" \
		-e "s#@SHARED_PKG@#$(SHARED_PKG)#g" \
		-e "s#@COMMON_PKG@#$(COMMON_PKG)#g" \
		-e "s#@DEV_PKG@#$(DEV_PKG)#g" \
		-e "s#@UDEB_PKG@#$(UDEB_PKG)#g" \
		-e "s#@DOC_PKG@#$(DOC_PKG)#g" \
		-e "s#@BIN_PKG@#$(BIN_PKG)#g" \
		-e "s#@EXAMPLES_PKG@#$(EXAMPLES_PKG)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		-e "s#@GTK_BINARY_VERSION@#$(GTK_BINARY_VERSION)#g" \
		-e "s#@LIBDIR@#$(LIBDIR)#g" \
		-e 's#@CONFDIR@#$(CONFDIR)#g' \
		-e "s#@MODULES_BASE_PATH@#$(MODULES_BASE_PATH)#g" \
		-e "s#@OLD_MODULES_BASE_PATH@#$(OLD_MODULES_BASE_PATH)#g" \
		-e "s#@NEWS@#$(NEWS)#g" \
		-e "s#@README@#$(README)#g" \
		$@.in > $@

clean:: debian/control
	# gross kludge to force control generation with the %.in target
	touch debian/control.in
	rm -f $(call dh_subst_files,$(DEB_ALL_PACKAGES))
	rm -f testsuite/reftests/*.ui.known_fail
	rm -rf debian/build debian/install
	# The build system does not automatically update the GResource files
	# when resources change. Force an update since we modify CSS files in
	# Revert-move-of-.flat-from-frame-border-to-frame.patch.
	rm -f gtk/gtkresources.c gtk/gtkresources.h

pre-build::
	# Mark reftests with known failures as non-fatal
	touch testsuite/reftests/box-shadow-changes-modify-clip.ui.known_fail
	touch testsuite/reftests/label-text-shadow-changes-modify-clip.ui.known_fail
	touch testsuite/reftests/nonresizable-size.ui.known_fail
	touch testsuite/reftests/window-show-contents-on-map.ui.known_fail
	touch testsuite/reftests/inherit-and-initial.ui.known_fail
	touch testsuite/reftests/textview-margins.ui.known_fail
	# So that gsettings can find the (uninstalled) gtk schemas
	mkdir -p debian/build/glib-2.0/schemas/
	cp gtk/org.gtk.* debian/build/glib-2.0/schemas/
	glib-compile-schemas debian/build/glib-2.0/schemas/

# Add dependencies to generate files from the debian/*.in ones
build-indep: $(call dh_subst_files,$(DEB_INDEP_PACKAGES))
build-arch: $(call dh_subst_files,$(DEB_ARCH_PACKAGES))

binary-install/$(SHARED_PKG)::
	# Install custom settings
	install -D debian/settings.ini debian/$(SHARED_PKG)/$(CONFDIR)/settings.ini
