#!/usr/bin/make -f

# Debian GNOME Maintainers
DISABLE_UPDATE_UPLOADERS := 1
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
include /usr/share/dpkg/pkg-info.mk

built_binaries := $(shell dh_listpackages)

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

STAMP_DIR := debian/stampdir

# rules in this debian/rules Makefile can be built concurrently as well as
# upstream rules in Makefile; all $(MAKE) invocations will inherit this flag,
# if you recurse into debian/rules ($(MAKE)
# -f debian/rules in rules), you need to pass a flag to avoid adding "-jX" when
# the childs encounters this line
DEB_BUILD_OPTIONS_PARALLEL ?= $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
PARALLEL_FLAGS += $(if $(DEB_BUILD_OPTIONS_PARALLEL),-j$(DEB_BUILD_OPTIONS_PARALLEL))

LIBDIR := usr/lib/$(DEB_HOST_MULTIARCH)

# ppc64 support; see #386815
ifeq ($(DEB_HOST_GNU_CPU),powerpc64)
	CFLAGS += -mminimal-toc
endif

# default CFLAGS; these can be expanded with $(call flavor_get, )
CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

# default LDFLAGS; these can be expanded with $(call flavor_get, )
# currently fails in the cups gtkprint backend
#LDFLAGS += -Wl,-z,defs

APIVER := 2.0
SONAME := 0
SHVER := 2.24.0

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

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

# relative base directory for all types of modules
MODULES_BASE_PATH := $(LIBDIR)/gtk-$(APIVER)/$(GTK_BINARY_VERSION)
OLD_MODULES_BASE_PATH := usr/lib/gtk-$(APIVER)/$(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
PIXBUF_PKG := gtk2-engines-pixbuf

# Avoid test failures on buildd environments
export HOME=$(CURDIR)/debian/build
export XDG_RUNTIME_DIR=$(HOME)
# 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
unexport XDG_DATA_DIRS

# package groups, used in rule dependencies and in cleanup
BINARY_INDEP_PKGS := $(COMMON_PKG) $(DOC_PKG) libgail-doc
BINARY_ARCH_PKGS := $(SHARED_PKG) $(BIN_PKG) $(UDEB_PKG) $(DEV_PKG) $(EXAMPLES_PKG) $(PIXBUF_PKG) gir1.2-gtk-2.0 libgail18 libgail18-udeb libgail-dbg libgail-dev libgail-common
ALL_PKGS := $(BINARY_INDEP_PKGS) $(BINARY_ARCH_PKGS)

# list of flavors we build; each gets a builddir, a configure pass (configure
# args are defined below), a build pass, and an install pass (in two steps)
# Note: the shared flavor is required
FLAVORS := shared

ifneq ($(filter %-udeb,$(built_binaries)),)
FLAVORS += shared_udeb
endif

# current flavor we're building; this is only expanded in flavor specific
# targets
current_flavor = $*

# macro to get a value for the current flavor we're building; for example
# when building the shared flavor, $(call flavor_get,CFLAGS) will expand to
# shared_CFLAGS if it's set or to CFLAGS otherwise; pay attention to not adding
# superflous spaces when for the arguments of $(call ); only some vars can
# be expanded in this way though
flavor_get = $(or $($(current_flavor)_$(1)),$($(1)))

# build dir for the current flavor; this is only expanded in flavor specific
# targets
builddir = $(buildbasedir)/$(current_flavor)
buildbasedir = $(CURDIR)/debian/build

# install dir for the current flavor; this is only expanded in flavor specific
# targets
installdir = $(installbasedir)/$(current_flavor)
installbasedir = $(CURDIR)/debian/install

# configure flags
configure_flags := \
			--enable-test-print-backend \
			--enable-introspection=no \
			--enable-shared \
			--disable-static \
			--with-xinput=yes

shared_configure_flags := $(configure_flags) \
			--enable-introspection \
			--enable-man
shared_udeb_configure_flags := $(configure_flags) \
			--disable-introspection \
			--disable-xcomposite \
			--disable-xdamage \
			--disable-xfixes \
			--disable-xrandr

DH_INSTALLDOCS_FILES := NEWS README AUTHORS

# 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)))))

# Gruik gruik generation of .devhelp files for the FAQ and tutorial
debian/gtk-%.devhelp: docs/%/html/index.html
	gawk -f debian/gruik2devhelp.awk -v name=gtk-$* $< > $@

debian/dh_gtkmodules.1: debian/dh_gtkmodules
	cd debian && pod2man -c "Gtk" -r "$(DEB_VERSION)" dh_gtkmodules dh_gtkmodules.1

%:
	dh $@ --with=autoreconf --with=gir --with=python3

override_dh_autoreconf:
	mkdir -p m4
	cp /usr/share/gtk-doc/data/gtk-doc.make .
	dh_autoreconf

override_dh_auto_configure: $(addprefix $(STAMP_DIR)/configure-stamp-, $(FLAVORS))
	:

$(STAMP_DIR)/configure-stamp-%:
	mkdir -p $(STAMP_DIR)
	mkdir -p $(builddir)
	dh_auto_configure \
		--builddirectory=$(builddir) \
		-- \
		$(call flavor_get,configure_flags)
	touch $@

.NOTPARALLEL: $(addprefix $(STAMP_DIR)/build-stamp-, $(FLAVORS))
$(STAMP_DIR)/build-stamp-%: $(STAMP_DIR)/configure-stamp-%
	LD_LIBRARY_PATH=$(builddir)/gtk/.libs:$(builddir)/gdk/.libs:$(LD_LIBRARY_PATH) \
		dh_auto_build --builddirectory=$(builddir)
	touch $@

override_dh_auto_build-arch: $(addprefix $(STAMP_DIR)/build-stamp-, $(FLAVORS))
	:

override_dh_auto_build-indep: $(addprefix $(STAMP_DIR)/build-stamp-, shared)
	:

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	mkdir -p debian/build/.local/share
	# setting LD_LIBRARY_PATH for "make check" might be needed in the
	# future, but doesn't seem necessary right now
	#LD_LIBRARY_PATH=$(builddir)/gtk/.libs:$(builddir)/gdk/.libs:$(LD_LIBRARY_PATH)
	# testsuite failures are ignored
	-dh_auto_test --builddirectory=$(CURDIR)/debian/build/shared -- -k
endif

$(STAMP_DIR)/install-stamp-%: $(STAMP_DIR)/build-stamp-%
	mkdir -p $(installdir)
	dh_auto_install --builddirectory=$(builddir) --destdir=$(installdir)
	sed -i -e '1s,^#!.*python.*,#!/usr/bin/python3,' debian/install/shared/usr/bin/gtk-builder-convert
	touch $@

override_dh_auto_install-arch: $(addprefix $(STAMP_DIR)/install-stamp-, $(FLAVORS))
	:

override_dh_auto_install-indep: $(addprefix $(STAMP_DIR)/install-stamp-, shared)
	:

debian/%: debian/%.in
	sed \
		-e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@VERSION@#$(DEB_VERSION_UPSTREAM)#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#@PIXBUF_PKG@#$(PIXBUF_PKG)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		-e "s#@GTK_BINARY_VERSION@#$(GTK_BINARY_VERSION)#g" \
		-e "s#@LIBDIR@#$(LIBDIR)#g" \
		-e "s#@MODULES_BASE_PATH@#$(MODULES_BASE_PATH)#g" \
		-e "s#@OLD_MODULES_BASE_PATH@#$(OLD_MODULES_BASE_PATH)#g" \
		$@.in \
		> $@

clean:: debian/control
	dh clean

override_dh_clean: debian/control
	# gross kludge to force control generation with the %.in target
	touch debian/control.in
	# remove install and build dirs
	rm -rf $(installbasedir)
	rm -rf $(buildbasedir)
	rm -f gtk-doc.make
	rm -f debian/*.devhelp
	-rm -rf $(STAMP_DIR)
	-rm -f $(call dh_subst_files,$(ALL_PKGS))
	-rm -f debian/dh_gtkmodules debian/dh_gtkmodules.1
	dh_clean

override_dh_installchangelogs:
	# changelog.gz symlinks will be overwritten by dh_link
	dh_installchangelogs -XChangeLog

override_dh_install-indep: $(call dh_subst_files,$(BINARY_INDEP_PKGS)) debian/gtk-faq.devhelp debian/gtk-tutorial.devhelp
	dh_install -i

override_dh_installdocs-indep:
	# do not merge the calls: dh_installdocs will only install some files
	# in the first package
	dh_installdocs -p$(COMMON_PKG) $(DH_INSTALLDOCS_FILES)
	# We need to install AUTHORS, NEWS.gz and README.gz in /u/s/d/l-doc,
	# not -dev according to debhelper 11's defaults, otherwise it would
	# have file conflicts
	dh_installdocs -p$(DOC_PKG) --doc-main-package=$(DOC_PKG) -X.in $(DH_INSTALLDOCS_FILES)
	# install copyright in all packages; the -p is for the special behavior
	# of dh_installdocs (ship README.Debian etc. in the first package)
	dh_installdocs -p$(COMMON_PKG) -X.in -i

override_dh_install-arch: $(call dh_subst_files,$(BINARY_ARCH_PKGS)) debian/dh_gtkmodules debian/dh_gtkmodules.1
	dh_install -a

override_dh_installdocs-arch:
	# install copyright in all packages; the -p is for the special behavior
	# of dh_installdocs (ship README.Debian etc. in the first package)
	dh_installdocs -p$(PIXBUF_PKG) -a

override_dh_dwz:
	# Skip udebs to work around #933212
	dh_dwz -N$(UDEB_PKG) -Nlibgail18-udeb

override_dh_strip:
	dh_strip --dbgsym-migration="libgtk2.0-0-dbg (<< 2.24.31-3~)" -N$(UDEB_PKG) -Nlibgail18 -Nlibgail18-udeb -Nlibgail-common
	dh_strip -plibgail18 -plibgail-common --dbgsym-migration="libgail-dbg (<< 2.24.31-3~)"
	dh_strip -p$(UDEB_PKG) -plibgail18-udeb

override_dh_compress:
	dh_compress -X.sgml -X.devhelp

override_dh_fixperms-arch:
	dh_fixperms -a -X$(LIBDIR)/$(SHARED_PKG)

override_dh_makeshlibs:
	dh_makeshlibs -p$(SHARED_PKG) \
		-X$(MODULES_BASE_PATH) \
		-V"$(SHARED_PKG) (>= $(SHVER))" \
		--add-udeb=$(UDEB_PKG) \
		-- -c4
	dh_makeshlibs -plibgail18 \
		-X$(MODULES_BASE_PATH) \
		-V"libgail18 (>= $(SHVER))" \
		--add-udeb=libgail18-udeb \
		-- -c4

override_dh_shlibdeps:
	dh_shlibdeps -p$(PIXBUF_PKG) -- -xlibgtk2.0-0
	# --remaining-packages didn't work right so let's at least make sure
	# it doesn't run a second time for the pixbuf .so
	dh_shlibdeps --remaining-packages -Xlibpixmap.so

override_dh_missing:
	# We use the GTK 3 version of gtk-update-icon-cache
	rm -f debian/install/*/usr/bin/gtk-update-icon-cache
	rm -f debian/install/*/usr/share/man/man1/gtk-update-icon-cache.1
	# Failing to install anything else is an error
	dh_missing --fail-missing
