#!/usr/bin/make -f

DISABLE_UPDATE_UPLOADERS := 1
include /usr/share/gnome-pkg-tools/1/rules/uploaders.mk
-include /usr/share/gnome-pkg-tools/1/rules/gnome-get-source.mk

GNOME_MODULE := pango

STAMP_DIR := debian/stampdir

PATCH_DIR := debian/patches

# 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 "-j2" when
# the childs encounters this line
MAKEFLAGS += $(if $(DEB_BUILD_OPTIONS_PARALLEL),-j2 $(DEB_BUILD_OPTIONS_PARALLEL))

CFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

# Ensure the build aborts when there are still references to undefined
# symbols
LDFLAGS += -Wl,-z,defs

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

DEBVERSION := $(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')
VERSION := $(shell echo $(DEBVERSION) | sed -e 's/-[^-]*$$//')
APIVER := 1.0
SONAME := 0
MODVER := 1.6.0

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# relative libdir
ifneq (,$(findstring multiarch,$(DEB_BUILD_OPTIONS)))
LIBDIR := usr/lib/$(DEB_HOST_GNU_TYPE)
else
LIBDIR := usr/lib
endif

# relative Pango Modules Path (separated by ":")
MODULES_PATH := $(LIBDIR)/pango/$(MODVER)/modules

# relative directory to store the generated Pango Module File
MODULE_FILES_D := $(LIBDIR)/pango/$(MODVER)/module-files.d

# Pango Module API version virtual Provide
ifneq (,$(findstring multiarch,$(DEB_BUILD_OPTIONS)))
PANGO_MODVER_DEP := pango$(APIVER)-multiarch-modver-$(MODVER)
else
PANGO_MODVER_DEP := pango$(APIVER)-modver-$(MODVER)
endif

# package names
SHARED_PKG := libpango$(APIVER)-$(SONAME)
UDEB_PKG := libpango$(APIVER)-udeb
COMMON_PKG := libpango$(APIVER)-common
DEV_PKG := libpango$(APIVER)-dev
DEBUG_PKG := libpango$(APIVER)-$(SONAME)-dbg
DOC_PKG := libpango$(APIVER)-doc

# package groups, used in rule dependencies and in cleanup
BINARY_INDEP_PKGS := $(DOC_PKG) $(COMMON_PKG)
BINARY_ARCH_PKGS := $(SHARED_PKG) $(UDEB_PKG) $(DEV_PKG) $(DEBUG_PKG)
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 static udeb

# list of flavors to run the test suite on
CHECK_FLAVORS := $(filter shared, $(FLAVORS))

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

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

# configure flags
common_configure_flags := \
			--prefix=/usr \
			--libdir=/$(LIBDIR) \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--with-html-dir=\$${prefix}/share/doc/$(DOC_PKG) \
			--sysconfdir=/etc \
			--build=$(DEB_BUILD_GNU_TYPE)

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	common_configure_flags += --host=$(DEB_HOST_GNU_TYPE)
endif

shared_configure_flags := $(common_configure_flags) \
			--enable-shared \
			--disable-static
static_configure_flags := $(common_configure_flags) \
			--disable-shared \
			--with-included-modules \
			--enable-static
udeb_configure_flags := $(common_configure_flags) \
			--without-x \
			--enable-shared \
			--disable-debug

# 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/dh_pangomodules.1: debian/dh_pangomodules
	cd debian && pod2man -c "Pango" -r "$(DEBVERSION)" dh_pangomodules dh_pangomodules.1

patch: $(STAMP_DIR)/patch-stamp

$(STAMP_DIR)/patch-stamp:
	dh_testdir
	# apply patches
	QUILT_PATCHES=$(PATCH_DIR) quilt --quiltrc /dev/null push -a || test $$? = 2
	# backup the original (potentially patched) files to restore them in
	# the clean target
	-test -r config.sub && cp config.sub config.sub.orig
	-test -r config.guess && cp config.guess config.guess.orig
	-test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess config.guess
	-mkdir -p $(STAMP_DIR)
	touch $@

$(STAMP_DIR)/configure-stamp-%: $(STAMP_DIR)/patch-stamp
	dh_testdir
	mkdir -p $(builddir)
	cd $(builddir) && \
		CFLAGS="$(CFLAGS)" LDFLAGS="$(LDFLAGS)" \
			$(CURDIR)/configure $($*_configure_flags)
	sed -i \
		-e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
		-e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' \
		$(builddir)/libtool
	touch $@

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

$(STAMP_DIR)/build-stamp-%: $(STAMP_DIR)/configure-stamp-%
	dh_testdir
	LD_LIBRARY_PATH=$(builddir)/pango/.libs:$(LD_LIBRARY_PATH) \
		$(MAKE) -C $(builddir)
	touch $@

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

$(STAMP_DIR)/check-stamp-%: $(STAMP_DIR)/build-stamp-%
	dh_testdir
	# setting LD_LIBRARY_PATH for "make check" might be needed in the
	# future, but doesn't seem necessary right now
	#LD_LIBRARY_PATH=$(builddir)/pango/.libs:$(LD_LIBRARY_PATH)
	# testsuite failures are ignored
	-$(MAKE) -C $(builddir) check
	touch $@

check: $(addprefix $(STAMP_DIR)/check-stamp-, $(CHECK_FLAVORS))

$(STAMP_DIR)/install-stamp-%: $(STAMP_DIR)/build-stamp-%
	mkdir -p $(installdir)
	$(MAKE) -C $(builddir) install \
		DESTDIR=$(installdir)
	touch $@

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

# gross kludge to force control generation with the %.in target
clean::
	touch debian/control.in

# always update debian/foo (do not rely on the timestamp of debian/control
# versus debian/control.in which might break with svn, svn-buildpackage, or
# dpkg-source)
debian/%: debian/%.in
	dh_testdir
	sed -e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@VERSION@#$(VERSION)#g" \
		-e "s#@PANGO_MODVER_DEP@#$(PANGO_MODVER_DEP)#g" \
		-e "s#@LIBDIR@#$(LIBDIR)#g" \
		-e "s#@MODULES_PATH@#$(MODULES_PATH)#g" \
		-e "s#@MODULE_FILES_D@#$(MODULE_FILES_D)#g" \
		-e "s#@GNOME_TEAM@#$(UPLOADERS)#g" \
		$@.in > $@

clean:: debian/control
	dh_testdir
	dh_testroot
	# remove install and build dirs
	rm -rf $(installbasedir)
	rm -rf $(buildbasedir)
	# restore files from backup (before unpatching)
	-test -r config.sub.orig && mv -f config.sub.orig config.sub
	-test -r config.guess.orig && mv -f config.guess.orig config.guess
	# unapply patches, if any
	QUILT_PATCHES=$(PATCH_DIR) quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	-rm -rf .pc
	-rm -rf $(STAMP_DIR)
	-rm -f $(call dh_subst_files,$(ALL_PKGS))
	-rm -f debian/dh_pangomodules debian/dh_pangomodules.1
	dh_clean

maybe_check = $(if $(findstring nocheck,$(DEB_BUILD_OPTIONS)),,check)

binary-indep: build $(maybe_check) install $(call dh_subst_files,$(BINARY_INDEP_PKGS))
	dh_testdir
	dh_testroot
	dh_installdirs -i
	dh_install -i
	dh_installchangelogs -i -N$(COMMON_PKG) ChangeLog ChangeLog.*
	dh_installdocs -i -N$(COMMON_PKG) NEWS README
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build $(maybe_check) install $(call dh_subst_files,$(BINARY_ARCH_PKGS)) debian/dh_pangomodules debian/dh_pangomodules.1
	dh_testdir
	dh_testroot
	dh_install -a
	# empty the dependency_libs in the *.la files
	sed -i "/dependency_libs/ s/'.*'/''/" \
		debian/$(DEV_PKG)/$(LIBDIR)/*.la
	dh_installchangelogs -a -N$(DEV_PKG) ChangeLog ChangeLog.*
	dh_installdocs -a -N$(DEV_PKG) NEWS README
	dh_installdefoma -a
	# fix the permissions of the development package because it ships
	# dh_pangomodules which we need to invoke in this build
	dh_fixperms -p$(DEV_PKG)
	# generate $(SHARED_PKG).modules for the shared lib
	LD_LIBRARY_PATH=$(CURDIR)/debian/$(SHARED_PKG)/$(LIBDIR):$(LD_LIBRARY_PATH) \
	PANGO_QUERYMODULES=$(buildbasedir)/shared/pango/pango-querymodules \
		debian/$(DEV_PKG)/usr/bin/dh_pangomodules \
			-p$(SHARED_PKG)
	# generate $(UDEB_PKG).modules for the udeb
	LD_LIBRARY_PATH=$(CURDIR)/debian/$(UDEB_PKG)/$(LIBDIR):$(LD_LIBRARY_PATH) \
	PANGO_QUERYMODULES=$(buildbasedir)/udeb/pango/pango-querymodules \
		debian/$(DEV_PKG)/usr/bin/dh_pangomodules \
			-p$(UDEB_PKG)
	dh_installinfo -a
	dh_installman -a
	dh_link -a
	dh_strip -a --dbg-package=$(DEBUG_PKG)
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -p$(SHARED_PKG) -X$(MODULES_PATH) -V "$(SHARED_PKG) (>= $(VERSION))" --add-udeb=$(UDEB_PKG)
	dh_installdeb -a
	# override shlibs for libraries from this source before computing
	# dependencies of packages generated from this source; we already have
	# inter-dependencies expressed manually in the control file, we do not
	# need the shlibs to add duplicates
	cat debian/*/DEBIAN/shlibs | \
		sed -n -r -e 's/(([^ ]+: )?([^ ]+) ([^ ]+)) .*/\1/p' \
			> debian/shlibs.local
	dh_shlibdeps -a
	-rm -f debian/shlibs.local
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: patch configure build check install clean binary-indep binary-arch binary
