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

include debian/scripts/vars
BUILD_DIR=$(SOURCE_DIR)/$(TAR_DIR)

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=3

# 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 debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

##
version=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' -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)
soname=0
libpath=$(CURDIR)/$(BUILD_DIR)/glib/.libs:$(CURDIR)/$(BUILD_DIR)/gmodule/.libs:$(CURDIR)/$(BUILD_DIR)/gobject/.libs:$(CURDIR)/$(BUILD_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$(shortver)-@SONAME@.*)))


debian/control:
	dh_testdir

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

configure: $(STAMP_DIR)/configure-stamp
$(STAMP_DIR)/configure-stamp:
	dh_testdir

	$(MAKE) -f debian/sys-build.mk source.make

	-test -r /usr/share/misc/config.sub && \
	   cp -f /usr/share/misc/config.sub $(BUILD_DIR)/config.sub
	-test -r /usr/share/misc/config.guess && \
	   cp -f /usr/share/misc/config.guess $(BUILD_DIR)/config.guess

	# When install the shared libraries, don't relink it.
	cp debian/fixed-ltmain.sh $(BUILD_DIR)/ltmain.sh

	# Add here commands to configure the package.
	cd $(BUILD_DIR) && \
	./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$(shortver)-doc \
			--enable-static

	cd $(BUILD_DIR) && \
	sed < libtool > libtool-2 \
	 -e 's/^hardcode_libdir_flag_spec.*$$/hardcode_libdir_flag_spec=" -D__LIBTOOL_IS_A_FOOL__ "/' \
	 -e '/^archive_cmds="/s/"$$/ \\$$deplibs"/' && \
	mv libtool-2 libtool

	touch $@

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

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

	touch $@

clean: debian/control
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	$(MAKE) -f debian/sys-build.mk source.clean
	-rm -rf $(STAMP_DIR)

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) -C $(BUILD_DIR) install \
		prefix=$(CURDIR)/debian/libglib$(shortver)-$(soname)/usr

	# 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

	# for -dbg
	mkdir -p $(CURDIR)/debian/libglib$(shortver)-$(soname)/usr/lib/debug
	for l in `find $(CURDIR)/debian/libglib$(shortver)-$(soname)/usr/lib -name '*.so*'`; do \
		cp -vdf $$l \
			$(CURDIR)/debian/libglib$(shortver)-$(soname)/usr/lib/debug/; \
	done

	dh_movefiles --sourcedir=debian/libglib$(shortver)-$(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$(shortver)-data/usr/share/doc/libglib$(shortver)-data
	dh_link -plibglib$(shortver)-data \
		usr/share/doc/libglib$(shortver)-$(soname) \
		usr/share/doc/libglib$(shortver)-data

	dh_installexamples -i
	dh_installinfo -i
	dh_installchangelogs -i $(BUILD_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$(shortver)-dbg/usr/share/doc/libglib$(shortver)-dbg
	rm -rf $(CURDIR)/debian/libglib$(shortver)-dev/usr/share/doc/libglib$(shortver)-dev
	dh_link -plibglib$(shortver)-dbg \
		usr/share/doc/libglib$(shortver)-$(soname) \
		usr/share/doc/libglib$(shortver)-dbg
	dh_link -plibglib$(shortver)-dev \
		usr/share/doc/libglib$(shortver)-$(soname) \
		usr/share/doc/libglib$(shortver)-dev

	dh_installexamples -a
	dh_installmenu -a
	dh_installman -plibglib$(shortver)-dev
	dh_installinfo -a
	dh_undocumented -a
	dh_installchangelogs -a $(BUILD_DIR)/ChangeLog
	dh_strip -a -Nlibglib$(shortver)-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -Nlibglib$(shortver)-dbg
	dh_makeshlibs	-plibglib$(shortver)-$(soname) \
			-V "libglib${shortver}-${soname} (>= ${version})"
	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 clean binary-indep binary-arch binary install configure debian/control
