#!/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

# 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

##
version=$(shell dpkg-parsechangelog | grep '^Version: ' | sed -e 's/^Version: //' | sed -e 's/-[A-Za-z0-9\.]*$$//g' -e 's/+[A-Za-z0-9\.]*$$//g')
major=$(shell echo ${version} | sed -e 's/\.[0-9]*$$//')
minor=$(shell echo ${version} | sed -e 's/^[0-9]*\.\([0-9]*\)\.*/\1/')
rel=$(shell echo ${version} | sed -e 's/^[0-9]*\.[0-9]*\.//')
soname=17
libpath=$(CURDIR)/$(BUILD_DIR)/util/.libs:$(CURDIR)/$(BUILD_DIR)/gail/.libs

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


debian/control:
	dh_testdir

	sed -e "s/@SONAME@/${soname}/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

	cp debian/fixed-Makefile.in $(BUILD_DIR)/docs/reference/libgail-util/Makefile.in
	(cd $(BUILD_DIR) && libtoolize --force --copy)

	# 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

	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/libgail$(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" -e "s/@SONAME@/${soname}/g" $$f > `echo $$f | sed -e "s/\.in//"`; \
	done

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

	# for -doc
	mv debian/libgail$(soname)/usr/share/gtk-doc/html/ \
		debian/libgail-doc/usr/share/doc/libgail-doc/
	
	dh_movefiles --sourcedir=debian/libgail$(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
	dh_installexamples -i
	dh_installmenu -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_installdebconf -a
	dh_installdocs -a

	rm -rf $(CURDIR)/debian/libgail-dev/usr/share/doc/libgail-dev
	rm -rf $(CURDIR)/debian/libgail-dbg/usr/share/doc/libgail-dbg
	rm -rf $(CURDIR)/debian/libgail-common/usr/share/doc/libgail-common
	dh_link -plibgail-dev usr/share/doc/libgail$(soname) usr/share/doc/libgail-dev
	dh_link -plibgail-dbg usr/share/doc/libgail$(soname) usr/share/doc/libgail-dbg
	dh_link -plibgail-common usr/share/doc/libgail$(soname) usr/share/doc/libgail-common

	dh_installexamples -a
	dh_installmenu -a
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_installinit -a
#	dh_installcron -a
	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs $(BUILD_DIR)/ChangeLog
	dh_strip -a -Nlibgail-dbg
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -Nlibgail-dbg
	dh_makeshlibs -plibgail$(soname) -l$(libpath) -V "libgail${soname} (>= ${version})"
	dh_installdeb -a
#	dh_perl -a
	dh_shlibdeps -l$(libpath) -a
	#cat debian/*/DEBIAN/shlibs > debian/shlibs.local
	dh_makeshlibs -plibgail$(soname) -l$(libpath) -V "libgail${soname} (>= ${version}), libgail-common (>= ${version})"
	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
