#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

SOURCE_DIR=$(CURDIR)
BUILD_DEB_DIR=$(SOURCE_DIR)

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

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


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
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
CFLAGS += -UENABLE_NLS

##
debversion=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //')
version=$(shell echo ${debversion} | sed -e 's/-[A-Za-z0-9\.]*$$//g' -e 's/+[A-Za-z0-9\.]*$$//g')
major=$(shell echo ${version} | sed -e 's/\(^[0-9]*\)\.[0-9]*\.[0-9]*$$/\1/')
minor=$(shell echo ${version} | sed -e 's/^[0-9]*\.\([0-9]*\)\.[0-9]*$$/\1/')
rel=$(shell echo ${version} | sed -e 's/^[0-9]*\.[0-9]*\.\([0-9]*\)$$/\1/')
shortver=$(major).$(minor)
apiver=2.0
soname=0
gtkver=2.0
shver=2.10.2-1osso2
libpath=$(BUILD_DEB_DIR)/glib/.libs:$(BUILD_DEB_DIR)/gmodule/.libs:$(BUILD_DEB_DIR)/gobject/.libs:$(BUILD_DEB_DIR)/gthread/.libs

_echo_cfiles = for f in ${1}; do if [ -f $$f ]; then echo $$f; fi; done
cfiles := $(shell $(call _echo_cfiles, $(wildcard debian/libglib$(apiver)-@SONAME@.*)))


debian/control:
	dh_testdir

	sed -e 's/@SONAME@/${soname}/g' -e 's/@VERSION@/${version}/g' $@.in > $@

$(SOURCE_DIR)/configure:
	AUTOGEN_SUBDIR_MODE=1 $(SOURCE_DIR)/autogen.sh

configure-deb: $(BUILD_DEB_DIR)/config.status
$(BUILD_DEB_DIR)/config.status: $(SOURCE_DIR)/configure
	dh_testdir

	# Add here commands to configure the package.
	cd $(BUILD_DEB_DIR) && \
	env LDFLAGS="$(LDFLAGS)" CFLAGS="$(CFLAGS)" ./configure \
			--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE) \
			--prefix=/usr \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--with-html-dir=\$${prefix}/share/doc/libglib$(apiver)-doc \
			--enable-static \
			--disable-gtk-doc

	cd $(BUILD_DEB_DIR) && \
	sed < libtool > libtool-2 \
	 -e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	 -e 's/0 | \[1-9\] | \[1-9\]\[0-9\] | \[1-9\]\[0-9\]\[0-9\])/0|\[1-9\]|\[1-9\]\[0-9\]|\[1-9\]\[0-9\]\[0-9\]|\[1-9\]\[0-9\]\[0-9\]\[0-9\]|\[1-9\]\[0-9\]\[0-9\]\[0-9\]\[0-9\])/' \
	 -e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' && \
	mv libtool-2 libtool

	touch $@

configure: configure-deb

build-deb: debian/control configure-deb $(BUILD_DEB_DIR)/build-stamp
$(BUILD_DEB_DIR)/build-stamp:
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) -C $(BUILD_DEB_DIR) \
		LD_LIBRARY_PATH=$(libpath):$(LD_LIBRARY_PATH)

	touch $@

build: build-deb

clean: debian/control
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	-$(MAKE) maintainer-clean
	rm -f gobject/gmarshal.strings
	rm -f configure config.guess ltmain.sh config.sub aclocal.m4
	rm -f depcomp mkinstalldirs missing install-sh config.h.in
	find -name Makefile.in | xargs --no-run-if-empty rm

	dh_clean $(SOURCE_DIR)/install-test-stamp $(BUILD_DEB_DIR)/build-stamp debian/shlibs.local

install-test: $(SOURCE_DIR)/install-test-stamp
$(SOURCE_DIR)/install-test-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# generating debian files from .in
	for f in ${cfiles}; do \
		cp $$f `echo $$f | sed -e 's/@SONAME@/${soname}/g'`; \
	done

	for f in `find debian/ -name "[^c]*.in"`; do \
		sed -e 's/@VERSION@/${version}/g' $$f > `echo $$f | sed -e 's/\.in//'`; \
	done

	touch $@

install-deb: build-deb install-test
	# Add here commands to install the package into debian/tmp
	$(MAKE) -C $(BUILD_DEB_DIR) install \
		prefix=$(CURDIR)/debian/libglib$(apiver)-$(soname)/usr

install: install-deb
	dh_movefiles --sourcedir=debian/libglib$(apiver)-$(soname)
	-find $(CURDIR)/debian/ -type d -empty | xargs rmdir -p 2>&1 > /dev/null

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i

	dh_installdocs -i

	rm -rf $(CURDIR)/debian/libglib$(apiver)-data/usr/share/doc/libglib$(apiver)-data
	dh_link -plibglib$(apiver)-data \
		usr/share/doc/libglib$(apiver)-$(soname) \
		usr/share/doc/libglib$(apiver)-data
	dh_link -plibglib$(apiver)-doc \
		usr/share/doc/libgtk$(gtkver)-doc/gtk \
		usr/share/doc/libglib$(apiver)-doc/gtk

	# create the symlinks on /usr/share/gtk-doc/html
	for i in glib gobject; do				\
		dh_link -plibglib$(apiver)-doc			\
			usr/share/doc/libglib$(apiver)-doc/$$i	\
			usr/share/gtk-doc/html/$$i;		\
	done

	dh_installexamples -i
	dh_installinfo -i
	dh_installchangelogs -i $(SOURCE_DIR)/ChangeLog
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a

	dh_installdocs -a

	rm -rf $(CURDIR)/debian/libglib$(apiver)-$(soname)-dbg/usr/share/doc/libglib$(apiver)-$(soname)-dbg
	rm -rf $(CURDIR)/debian/libglib$(apiver)-dev/usr/share/doc/libglib$(apiver)-dev
	dh_link -plibglib$(apiver)-$(soname)-dbg \
		usr/share/doc/libglib$(apiver)-$(soname) \
		usr/share/doc/libglib$(apiver)-$(soname)-dbg
	dh_link -plibglib$(apiver)-dev \
		usr/share/doc/libglib$(apiver)-$(soname) \
		usr/share/doc/libglib$(apiver)-dev

	dh_installexamples -a
	dh_installmenu -a
	dh_installman -plibglib$(apiver)-dev
	dh_installinfo -a
	dh_installchangelogs -a $(SOURCE_DIR)/ChangeLog
	dh_strip -a --dbg-package=libglib$(apiver)-$(soname)
	dh_compress -a
	dh_fixperms -a

# shlibs
	dh_makeshlibs -a -Nlibglib$(apiver)-dbg
	dh_makeshlibs -n -X usr/lib/glib-$(apiver) -plibglib$(apiver)-$(soname)$(suffix) -V "libglib${apiver}-${soname}${suffix} (>= 2.10.2)"
	dh_installdeb -a
	dh_shlibdeps -l$(libpath) -a
	cat debian/*/DEBIAN/shlibs > debian/shlibs.local

	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build build-deb clean binary-indep binary-arch binary install install-deb install-test configure configure-deb extract debian/control
