#!/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 := glib

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS += -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# 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

APIVER := 2.0
SONAME := 0
SHVER := 2.12.9

# package names
SHARED_PKG := libglib$(APIVER)-$(SONAME)
DATA_PKG := libglib$(APIVER)-data
DEV_PKG := libglib$(APIVER)-dev
UDEB_PKG := libglib$(APIVER)-udeb
DOC_PKG := libglib$(APIVER)-doc
DEBUG_PKG := $(SHARED_PKG)-dbg

# 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 := deb udeb

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

# build dir for the current flavor; this is only expanded in flavor specific
# targets
# Note: dh_clean will rm -rf debian/tmp, hence all builds
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 \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--with-html-dir=\$${prefix}/share/doc/$(DOC_PKG) \
			--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	common_configure_flags += --build=$(DEB_BUILD_GNU_TYPE)
else
	common_configure_flags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif
deb_configure_flags := $(common_configure_flags) \
			--enable-static
udeb_configure_flags := $(common_configure_flags)

patch: $(STAMP_DIR)/patch-stamp

$(STAMP_DIR)/patch-stamp:
	dh_testdir
	# backup the original 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
	# apply patches
	QUILT_PATCHES=$(PATCH_DIR) quilt --quiltrc /dev/null push -a || test $$? = 2
	-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)/glib/.libs:$(builddir)/gmodule/.libs:$(builddir)/gobject/.libs:$(builddir)/gthread/.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
	# testsuite failures are ignored
	-LD_LIBRARY_PATH=$(builddir)/glib/.libs:$(builddir)/gmodule/.libs:$(builddir)/gobject/.libs:$(builddir)/gthread/.libs:$(LD_LIBRARY_PATH) \
		$(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))

debian/control:
	dh_testdir
	sed \
		-e "s#@SONAME@#$(SONAME)#g" \
		-e "s#@APIVER@#$(APIVER)#g" \
		-e "s#@VERSION@#$(VERSION)#g" \
		-e "s#@SHARED_PKG@#$(SHARED_PKG)#g" \
		-e "s#@DATA_PKG@#$(DATA_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#@DEBUG_PKG@#$(DEBUG_PKG)#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)
	dh_clean

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

binary-indep: build $(maybe_check) install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installchangelogs -i -N$(DATA_PKG) ChangeLog ChangeLog.*
	dh_installdocs -N$(DATA_PKG) -i NEWS NEWS.* README
	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
	dh_testdir
	dh_testroot
	dh_install -a
	# empty the dependency_libs in the *.la files
	sed -i "/dependency_libs/ s/'.*'/''/" \
		debian/$(DEV_PKG)/usr/lib/*.la
	dh_installchangelogs -a -N$(DEV_PKG) -N$(DEBUG_PKG) ChangeLog ChangeLog.*
	dh_installdocs -a -N$(DEV_PKG) -N$(DEBUG_PKG) NEWS NEWS.* README
	dh_link -a
	dh_strip -a --dbg-package=$(DEBUG_PKG)
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs	-p$(SHARED_PKG) \
			-V "$(SHARED_PKG) (>= $(SHVER))" \
			--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 -ldebian/$(SHARED_PKG)/usr/lib -L$(SHARED_PKG)
	-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 debian/control
