#!/usr/bin/make -f

DEB_BUILDDIR = debian/build
DEB_MAKE_DESTDIRSKEL = $(CURDIR)/debian/install/@FLAVOR@
DEB_MAKE_FLAVORS = shared shared_udeb static
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

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
SHVER := 3.3.18

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)

# 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
PIXBUF_PKG := gtk3-engines-pixbuf


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

DEB_CONFIGURE_FLAGS_shared = \
			--enable-gtk-doc \
			--enable-shared \
			--enable-introspection=yes \
			--disable-static

DEB_CONFIGURE_FLAGS_shared_udeb = \
			--enable-introspection=no \
			--disable-xcomposite \
			--disable-xdamage \
			--disable-xfixes \
			--disable-xrandr

DEB_CONFIGURE_FLAGS_static = \
			--with-included-loaders=yes \
			--enable-introspection=no \
			--disable-modules \
			--disable-shared \
			--enable-static

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_MAKESHLIBS_ARGS_libgail-3-0 += -V"libgail-3-0 (>= $(SHVER)), libgail-3-common (>= $(SHVER))" -- -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) =

# 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#@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#@OPTLIBDIR@#$(OPTLIBDIR)#g' \
		-e 's#@CONFDIR@#$(CONFDIR)#g' \
		-e "s#@MODULES_BASE_PATH@#$(MODULES_BASE_PATH)#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))

# 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

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

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
