#!/usr/bin/make -f
# -*- makefile -*-

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

TCL_VER := 8.5

# support both hardening-wrapper (for backports) and dpkg-buildflags
export DEB_BUILD_HARDENING = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie
DPKG_EXPORT_BUILDFLAGS = 1
-include /usr/share/dpkg/buildflags.mk
LDFLAGS+= -Wl,--as-needed -Wl,-z,now
CFLAGS+= -fPIC -pie

# When protecting the postmaster with oom_adj=-17, allow the OOM killer to slay
# the backends (http://archives.postgresql.org/pgsql-hackers/2010-01/msg00170.php)
ifeq ($(shell uname -s),Linux)
CFLAGS+= -DLINUX_OOM_ADJ=0
endif

ifneq ($(findstring $(DEB_BUILD_ARCH), sparc alpha),)
# sparc and alpha's gcc currently miscompiles; see
# http://lists.debian.org/debian-alpha/2007/11/msg00025.html
    CFLAGS+=-O1
endif

export DPKG_GENSYMBOLS_CHECK_LEVEL=4

# build should fail on test suite failures on all arches
TESTSUITE_FAIL_CMD=exit 1

override_dh_auto_clean:
	[ ! -f GNUmakefile ] || make distclean
	-rm -fr build
	-find doc-xc/src/sgml -name "*.sgml" -exec rm -f {} \;
	(cd src/test/regress/sql; \
	  rm -f misc.sql \
	  rm -f create_function_1.sql \
	  rm -f create_function_2.sql \
	  rm -f largeobject.sql \
	  rm -f copy.sql \
	  rm -f constraints.sql \
	  rm -f security_label.sql \
	  rm -f tablespace.sql \
	  rm -f xc_copy.sql)

override_dh_auto_configure:
	mkdir -p build
	cd build && ../configure --prefix=/usr \
		--libdir=/usr/lib/postgres-xc \
		--docdir=/usr/share/doc/postgres-xc-doc \
		--datarootdir=/usr/share \
		--datadir=/usr/share/postgres-xc \
		--includedir=/usr/include/postgres-xc \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--enable-nls \
		--enable-integer-datetimes \
		--enable-thread-safety \
		--enable-debug \
		--disable-maintainer-mode \
		--disable-dependency-tracking \
		--disable-rpath \
		--with-ossp-uuid \
		--with-gnu-ld \
		--with-pgport=5432 \
		--with-system-tzdata=/usr/share/zoneinfo \
		--with-tcl \
		--with-perl \
		--with-python \
		--with-pam \
		--with-krb5 \
		--with-gssapi \
		--with-openssl \
		--with-libxml \
		--with-libxslt \
		--with-ldap \
		--with-tclconfig=/usr/lib/tcl$(TCL_VER) \
		--with-tkconfig=/usr/lib/tk$(TCL_VER) \
		--with-includes=/usr/include/tcl$(TCL_VER) 

override_dh_auto_build:
	cd build && $(MAKE)

override_dh_auto_install:
	make -C build install-world DESTDIR=$(CURDIR)/debian/tmp

override_dh_install:
	dh_install

	# these get installed into the individual -/l* packages; -f because it
	# does not exist for an -A build
	rm -f debian/postgres-xc-contrib/usr/share/postgres-xc/extension/pl*

override_dh_strip:
	dh_strip --dbg-package=postgres-xc-dbg

override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# patch away the "don't execute as root" check for the test
	# suite; doing it here will ensure that the actual debs have
	# the check.
	set -e; \
	patch --no-backup-if-mismatch -p1 < debian/disable-root-check.patch; \
	patch --no-backup-if-mismatch -p1 < debian/pg_regress-in-tmp.patch; \
	#make -C build/src/test/regress bigcheck || fail=1; \
	patch --no-backup-if-mismatch -Rp1 < debian/pg_regress-in-tmp.patch; \
	patch --no-backup-if-mismatch -Rp1 < debian/disable-root-check.patch; \
	if [ -n "$$fail" ]; then \
	    for l in regression.diffs log/initdb.log log/postmaster.log; do \
	    	if [ -e build/src/test/regress/$$l ]; then \
		    echo "********* $$l *******"; \
		    cat build/src/test/regress/$$l; \
		fi; \
	    done; \
	    $(TESTSUITE_FAIL_CMD); \
	fi
endif

%:
	dh $@ --with autotools_dev 

