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

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

# The C++ compiler to be used - this has only been made a variable to make
# it easier to test the code with a different C++ compiler; when building
# Debian packages, always use plain 'g++'.
CXX=g++

CFLAGS += -g
CXXFLAGS += -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	CXXFLAGS += -O0
else    
	CFLAGS += -O2
	CXXFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

confflags = -v --prefix=/usr --enable-maintainer-mode

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build $(DEB_HOST_GNU_TYPE)
else    
	confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# shared library versions, option 1
#version=2.0.5
#major=2
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
version=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
major=`ls src/.libs/lib*.so.* | \
 awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

upstreamversion=$(shell dpkg-parsechangelog | grep Version | head -1 | sed -e 's/Version: //g' -e 's/-[A-Za-z0-9\.]*$$//g')


sources-update:
	env NOCONFIGURE=1 sh ./autogen.sh -v

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	env CXX=$(CXX) ./configure $(confflags)

	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp 
	dh_testdir

	$(MAKE)
	doxygen

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp config.log config.status config.h \
		stamp-h

	# Add here commands to clean up after the build process.
	-$(MAKE) clean

	rm -rf src/.libs src/*.la src/*.o src/*.lo \
		include/pqxx/config.h include/pqxx/stamp-h

	# Update config.{sub,guess} from autotools-dev, if possible.
	-for f in config/config.sub config/config.guess ; do \
		if test -r /usr/share/misc/`basename $$f` ; then \
			OLDDATE=`./$$f -t | tr -d -` ; \
			NEWDATE=$$(/usr/share/misc/`basename $$f` -t | tr -d -) ; \
			if [ $$OLDDATE -lt $$NEWDATE ] ; then \
				echo "GNU config automated update of $$f (replacing $$OLDDATE with $$NEWDATE)" 1>&2 ; \
				cp -f /usr/share/misc/`basename $$f` $$f ; \
			fi; \
		fi; \
	done

	-find doc/html/Reference -type f -name '*.html' | xargs rm -f

	dh_clean

maintainer-clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-$(MAKE) distclean
	-debian/rules clean

	rm -f aclocal.m4 \
		configure libtool \
		config/depcomp config/config.guess config/config.sub \
		config/install-sh config/ltmain.sh config/missing \
		config/mkinstalldirs \
		include/pqxx/config.h.in include/pqxx/stamp.h.in 

	-find -type f -name 'Makefile.am' | sed -e 's/\.am$$/.in/' | xargs rm -f
	-find -type f -name 'Makefile.am' | sed -e 's/\.am$$//' | xargs rm -f

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

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

	# Ensure there are no leftover files
	find `pwd`/debian/tmp -type d -depth | xargs rmdir 2>&1

#	dh_installdebconf	
	dh_installdocs

	install -p doc/html/Reference/*html \
		$(CURDIR)/debian/libpqxx-dev/usr/share/doc/libpqxx-dev/html/

	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -plibpqxx0 -V 'libpqxx0 (>= $(upstreamversion))'
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

