#!/usr/bin/make	-f 
TCL_VER := 8.5
MAJOR_VER := 9.0

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

LDFLAGS+= -Wl,--as-needed
CFLAGS+= -fPIC

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 DEB_BUILD_HARDENING=1

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

DEB_DH_INSTALL_SOURCEDIR=debian/tmp
DEB_CONFIGURE_EXTRA_FLAGS := --mandir=\$${prefix}/share/postgresql/$(MAJOR_VER)/man \
  --with-docdir=\$${prefix}/share/doc/postgresql-doc-$(MAJOR_VER) \
  --sysconfdir=/etc/postgresql-common \
  --datadir=\$${prefix}/share/postgresql/$(MAJOR_VER) \
  --bindir=\$${prefix}/lib/postgresql/$(MAJOR_VER)/bin \
  --includedir=\$${prefix}/include/postgresql/ \
  --enable-nls \
  --enable-integer-datetimes \
  --enable-thread-safety \
  --enable-debug \
  --disable-rpath \
  --with-tcl \
  --with-perl \
  --with-python \
  --with-pam \
  --with-krb5 \
  --with-gssapi \
  --with-openssl \
  --with-libxml \
  --with-libxslt \
  --with-ldap \
  --with-ossp-uuid \
  --with-gnu-ld \
  --with-tclconfig=/usr/lib/tcl$(TCL_VER) \
  --with-tkconfig=/usr/lib/tk$(TCL_VER) \
  --with-includes=/usr/include/tcl$(TCL_VER) \
  --with-system-tzdata=/usr/share/zoneinfo \
  --with-pgport=5432 \
  $(ARCH_OPTS) \
  CFLAGS='$(CFLAGS)' \
  LDFLAGS='$(LDFLAGS)'

DEB_MAKE_BUILD_TARGET = world
DEB_MAKE_INSTALL_TARGET := $(patsubst install,install-world,$(DEB_MAKE_INSTALL_TARGET))

DEB_DH_MAKESHLIBS_ARGS := -Xusr/lib/postgresql/$(MAJOR_VER)
DEB_DH_INSTALLCHANGELOGS_ARGS := HISTORY
DEB_COMPRESS_EXCLUDE := .source .c
DPKG_GENSYMBOLS_CHECK_LEVEL=4
export DPKG_GENSYMBOLS_CHECK_LEVEL

common-binary-predeb-arch::
	# verify that all files are installed; ignore manpages, since
	# they are handled by dh_installman
	missing=`dh_install --sourcedir=debian/tmp --list-missing --no-act 2>&1 | grep -v man/man`; \
	if [ -n "$$missing" ]; then \
	    echo "Not all files are installed:"; \
	    echo "$$missing"; \
	    exit 1; \
	fi

common-post-build-arch::
	# generate POT files for translators
	find -name nls.mk -exec sh -c "make -C \$$(dirname {}) init-po" \;
	
common-post-build-indep::
	# build tutorial stuff
	make -C src/tutorial NO_PGXS=1

install/postgresql-doc-$(MAJOR_VER)::
	install src/tutorial/*.c src/tutorial/*.source src/tutorial/Makefile src/tutorial/README debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/tutorial

binary-predeb/postgresql-$(MAJOR_VER)::
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; \
	make -C src/test/regress bigcheck || fail=1; \
	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 src/test/regress/$$l ]; then \
		    echo "********* $$l *******"; \
		    cat src/test/regress/$$l; \
		fi; \
	    done; \
	    $(TESTSUITE_FAIL_CMD); \
	fi
endif

	# compress manpages
	find debian/postgresql-$(MAJOR_VER)/usr/share/postgresql/$(MAJOR_VER)/man -type f -exec gzip -9 '{}' \;

binary-predeb/postgresql-client-$(MAJOR_VER)::
	find debian/postgresql-client-$(MAJOR_VER)/usr/share/postgresql/$(MAJOR_VER)/man -type f -exec gzip -9 '{}' \;

# run tests in contrib in temporary test installations, using programs
# from local tree
contrib-check:
	set -e; cd contrib; \
	for d in *; do \
	    [ -d $$d/sql ] || continue; \
	    echo "==============  Running tests in $$d"; \
	    (cd $$d; \
	    if ! ../../src/test/regress/pg_regress --top-builddir=../.. --temp-install=tmp_check --dbname=contrib_regression `cd sql; ls *.sql | sed 's/.sql$$//'`; then \
		 cat regression.diffs; \
	    fi); \
	done

# run tests in contrib in temporary test installation, using programs
# from system installation
contrib-installcheck:
	# set up temporary db
	rm -rf tmp_data
	mkdir tmp_data
	/usr/lib/postgresql/$(MAJOR_VER)/bin/initdb -D tmp_data
	/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_ctl -D tmp_data -l tmp_data/postgres.log -o '-k /tmp' start
	# wait until it started up
	while !/usr/lib/postgresql/$(MAJOR_VER)/bin/psql -h /tmp -l >/dev/null 2>&1; do sleep 1; done
	sleep 1
	while !/usr/lib/postgresql/$(MAJOR_VER)/bin/psql -h /tmp -l >/dev/null 2>&1; do sleep 1; done
	# run the tests
	-cd contrib; make installcheck
	/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_ctl -D tmp_data stop
	# find and print the regression diffs
	find contrib/ -name regression.diffs -exec cat '{}' \;
