#!/usr/bin/make -f
# rules to package libpqxx-object
# $Id: rules,v 1.2 2004/01/17 23:30:39 roger Exp $
#
# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# 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

# Get upstream version, and define libpqxx-object suffix
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | grep ^Version | sed "s/.* //" | sed "s/-[^-]*$$//")
LIBRARY_VERSION =  -$(UPSTREAM_VERSION)
#LIBRARY_VERSION = 1

autoconfigure: autoconfigure-stamp
autoconfigure-stamp: debian/control debian/libpqxx-object.install
	dh_testdir
# Create versioned symlinks
	cd debian; \
	for file in libpqxx-object.*; do \
	ln -sfv $$file `echo $$file | sed -e 's/\(libpqxx-object[[:alpha:]]*\)\(.*\)/\1$(LIBRARY_VERSION)\2/'`; \
	done
	rm debian/libpqxx-object$(LIBRARY_VERSION)*.in
	mkdir debian/build; \
	cd debian/build; \
	../../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --enable-static --enable-shared --disable-rpath
	touch autoconfigure-stamp


build: build-stamp debian/control
build-stamp: autoconfigure-stamp
	dh_testdir

	cd debian/build; \
	$(MAKE)

	touch build-stamp

clean: debian/control
	dh_testdir
	rm -f build-stamp autoconfigure-stamp

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

	rm -rf debian/build debian/install
	rm -rf debian/libpqxx-object.install
	rm -rf debian/libpqxx-object$(LIBRARY_VERSION).files
	rm -rf debian/libpqxx-object$(LIBRARY_VERSION).postinst
	-$(MAKE) distclean

	rm -rf $(CURDIR)/debian/libpqxx-object*$(LIBRARY_VERSION)*
	dh_clean

install: DH_OPTIONS=
install: build debian/control
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd debian/build; \
	$(MAKE) DESTDIR=`pwd`/../install install

	dh_install


# Build architecture-independent files here.
binary-indep: build install ChangeLog
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installchangelogs -i NEWS
	dh_link -i
	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 ChangeLog
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installchangelogs -a NEWS
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	find debian/libpqxx-object$(LIBRARY_VERSION)/usr/lib -type f -perm 0644 -print0 | xargs -0 chrpath --delete
	dh_makeshlibs -a -V
	dh_installdeb -a
	dh_shlibdeps -a -X -l`pwd`/debian/libpqxx-object$(LIBRARY_VERSION)/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

# Make sure ChangeLog is up-to-date.
ChangeLog: changelog-stamp
changelog-stamp: build
	$(MAKE) ChangeLog

SEDPATTERN = -e "s/\#UPSTREAM_VERSION\#/$(UPSTREAM_VERSION)/g" -e "s/\#LIBRARY_VERSION\#/$(LIBRARY_VERSION)/g"

# Make sure debian/control is up-to-date.
debian/control: debian/changelog debian/control.in
	sed $(SEDPATTERN) <debian/control.in >debian/control

# Make sure debian/libpqxx-object.install is up-to-date.
debian/libpqxx-object.install: debian/changelog debian/libpqxx-object.install.in
	sed $(SEDPATTERN) <debian/libpqxx-object.install.in >debian/libpqxx-object.install

.PHONY: build clean binary-indep binary-arch binary install autoconfigure
