#!/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
DEB_BUILD_PARALLEL = yes

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

# Make a writable HOME for the testsuite
export HOME=$(CURDIR)/debian/home

GNOME_MODULE = gtk+

# 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 := 3
SONAME := 0

LIBDIR := usr/lib/$(DEB_HOST_MULTIARCH)

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

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

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

# relative base directory for all types of modules
MODULES_BASE_PATH := $(LIBDIR)/gtk-3.0/$(GTK_BINARY_VERSION)
OLD_MODULES_BASE_PATH := usr/lib/gtk-3.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
DEBUG_PKG := $(SHARED_PKG)-dbg
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" \;)

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

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

DEB_CONFIGURE_FLAGS_shared = \
			--enable-gtk-doc \
			--enable-shared \
			--enable-introspection \
			--enable-colord \
			--enable-wayland-backend \
			--disable-static

DEB_CONFIGURE_FLAGS_shared_udeb = \
			--disable-introspection \
			--disable-colord \
			--disable-wayland-backend \
			--disable-xcomposite \
			--disable-xdamage \
			--disable-xfixes \
			--disable-xrandr

DEB_MAKE_CHECK_TARGET = -k check
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_MAKESHLIBS_ARGS_libgail-3-0 += -V -- -c4
DEB_DH_FIXPERMS_ARGS_ALL += -X$(LIBDIR)/$(SHARED_PKG)
DEB_DH_STRIP_ARGS = --dbg-package=$(if $(findstring libgail,$(cdbs_curpkg)),libgail-3-0-dbg,$(DEBUG_PKG))
DEB_DH_STRIP_ARGS_$(UDEB_PKG) =

pre-build::
	mkdir -p $(CURDIR)/debian/home

# 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#@DEBUG_PKG@#$(DEBUG_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))
	# work around tarball generated with too new Wayland
	rm -f gdk/wayland/gtk-shell-client-protocol.h
	rm -f gdk/wayland/gtk-shell-protocol.c
	rm -rf $(CURDIR)/debian/home

# 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 the binaries with a -3.0 suffix
	mv debian/$(SHARED_PKG)/$(LIBDIR)/$(SHARED_PKG)/gtk-update-icon-cache \
	    debian/$(SHARED_PKG)/$(LIBDIR)/$(SHARED_PKG)/gtk-update-icon-cache-3.0
	# Install custom settings
	install -D debian/settings.ini debian/$(SHARED_PKG)/$(CONFDIR)/settings.ini

binary-install/$(BIN_PKG)::
	# Install the binaries with a -3.0 suffix
	mv debian/$(BIN_PKG)/usr/share/man/man1/gtk-update-icon-cache.1 \
	    debian/$(BIN_PKG)/usr/share/man/man1/gtk-update-icon-cache-3.0.1
	install -m 0755 -D debian/update-icon-caches \
	    debian/$(BIN_PKG)/usr/sbin/update-icon-caches

list-missing:
	# FIXME: remove this when CDBS' list-missing works with multiple flavors
	perl -w debian/dh_listmissing.pl $(foreach f,$(DEB_MAKE_FLAVORS),debian/install/$(f)) 2>&1 | \
	    egrep -v '(\.la|$(LIBDIR)/gtk-3.0/$(GTK_BINARY_VERSION)/[^/]+/[^/]+\.a) has been installed' >&2
