#!/usr/bin/make -f
MAJOR_VER := 8.2

# this must also work for old releases with multiarch, so don't fail if the
# variable doesn't exist
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH || true)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie,-format
DPKG_EXPORT_BUILDFLAGS = 1
-include /usr/share/dpkg/buildflags.mk
LDFLAGS+= -Wl,--as-needed -L/usr/lib/mit-krb5 -L/usr/lib/$(DEB_HOST_MULTIARCH)/mit-krb5
CFLAGS+= -fPIC -pie -I/usr/include/mit-krb5 -fno-aggressive-loop-optimizations
# cf. http://stackoverflow.com/a/34204352/3872653

# 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

#py2#PYTHON_FLAGS = --with-python

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

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- \
  --mandir=/usr/share/postgresql/$(MAJOR_VER)/man \
  --with-docdir=/usr/share/doc/postgresql-doc-$(MAJOR_VER) \
  --sysconfdir=/etc/postgresql-common \
  --datadir=/usr/share/postgresql/$(MAJOR_VER) \
  --bindir=/usr/lib/postgresql/$(MAJOR_VER)/bin \
  --libdir=/usr/lib \
  --includedir=/usr/include/postgresql/ \
  --enable-nls \
  --enable-integer-datetimes \
  --enable-thread-safety \
  --enable-debug \
  --disable-rpath \
  --with-tcl \
  --with-perl \
  --with-pam \
  --with-krb5 \
  --with-gssapi \
  --without-openssl \
  --with-libxml \
  --with-libxslt \
  --with-ldap \
  --with-ossp-uuid \
  --with-gnu-ld \
  --with-system-tzdata=/usr/share/zoneinfo \
  --with-pgport=5432 \
  CFLAGS='$(CFLAGS)' \
  LDFLAGS='$(LDFLAGS)' \
  $(PYTHON_FLAGS)

override_dh_auto_build:
	$(MAKE)
	$(MAKE) -C contrib
	$(MAKE) -C doc

	# generate POT files for translators
	find -name nls.mk -exec sh -c "make -C \$$(dirname {}) init-po" \;

	# build tutorial stuff
	make -C src/tutorial NO_PGXS=1

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C contrib install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C doc install DESTDIR=$(CURDIR)/debian/tmp

override_dh_makeshlibs:
	dh_makeshlibs -Xusr/lib/postgresql/$(MAJOR_VER)

override_dh_auto_clean:
	[ ! -f GNUmakefile ] || dh_auto_clean	

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

override_dh_compress:
	dh_compress -X.source -X.c

override_dh_install-arch:
	dh_install -a

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

override_dh_install-indep:
	dh_install -i

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

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 src/test/regress check 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 src/test/regress/$$l ]; then \
		    echo "********* $$l *******"; \
		    cat src/test/regress/$$l; \
		fi; \
	    done; \
	    $(TESTSUITE_FAIL_CMD); \
	fi
endif

override_dh_strip:
	dh_strip --dbg-package=postgresql-$(MAJOR_VER)-dbg

# 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 '{}' \;
