#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

MAJOR_VER := 9.3
TCL_VER := 8.6
#CASSERT_FLAGS := --enable-cassert

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#PIE# # On jessie/zesty and older, uncomment the #PIE# lines
#PIE# # "-pie" because it would break linking our .so files
#PIE# export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-pie
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
#PIE# # skip -pie on 32bit archs for performance and stability reasons
#PIE# # (http://www.postgresql.org/message-id/20140519115318.GB7296@msgid.df7cb.de, #797530)
#PIE# ifneq ($(DEB_HOST_ARCH_BITS || :),32)
#PIE# CFLAGS+= -fPIC -pie
#PIE# else
#PIE# # if gcc recognizes -no-pie, add it to CFLAGS (exit is 4 for unknown options)
#PIE# CFLAGS+= $(shell gcc -no-pie 2> /dev/null; [ $$? = 1 ] && echo "-no-pie")
#PIE# endif

# 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_SCORE_ADJ=0
endif

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

# Facilitate hierarchical profile generation on amd64 (#730134)
ifeq ($(DEB_HOST_ARCH),amd64)
CFLAGS+= -fno-omit-frame-pointer
endif

export DPKG_GENSYMBOLS_CHECK_LEVEL=4

COMMON_CONFIGURE_FLAGS= \
  --mandir=/usr/share/postgresql/$(MAJOR_VER)/man \
  --docdir=/usr/share/doc/postgresql-doc-$(MAJOR_VER) \
  --sysconfdir=/etc/postgresql-common \
  --datarootdir=/usr/share/ \
  --datadir=/usr/share/postgresql/$(MAJOR_VER) \
  --bindir=/usr/lib/postgresql/$(MAJOR_VER)/bin \
  --libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/ \
  --libexecdir=/usr/lib/postgresql/ \
  --includedir=/usr/include/postgresql/ \
  --enable-nls \
  --enable-integer-datetimes \
  --enable-thread-safety \
  --enable-debug \
  --enable-dtrace \
  $(CASSERT_FLAGS) \
  --disable-rpath \
  --with-ossp-uuid \
  --with-gnu-ld \
  --with-pgport=5432 \
  --with-system-tzdata=/usr/share/zoneinfo \
  MKDIR_P='/bin/mkdir -p' \
  TAR='/bin/tar' \
  CFLAGS='$(CFLAGS)' \
  LDFLAGS='$(LDFLAGS)' \
  DPKG_VERSION=$(shell dpkg-parsechangelog | awk '/^Version:/ { print $$2 }') \
  DPKG_VENDOR=$(shell dpkg-vendor --query vendor)

# allow missing krb5 and ldap during bootstrapping
ifeq ($(DEB_STAGE),stage1)
BOOTSTRAP_FLAGS=
else
BOOTSTRAP_FLAGS= --with-gssapi --with-ldap \
		 --with-includes=/usr/include/mit-krb5 \
		 --with-libs=/usr/lib/mit-krb5 \
		 --with-libs=/usr/lib/$(DEB_HOST_MULTIARCH)/mit-krb5
endif

%:
	dh $@

override_dh_auto_configure: stamp/configure-build stamp/configure-build-py3

stamp/configure-build:
	mkdir -p stamp
	dh_auto_configure --builddirectory=build -- \
           --with-tcl \
           --with-perl \
           --with-python \
           --with-pam \
           --with-openssl \
           --with-libxml \
           --with-libxslt \
           --with-tclconfig=/usr/lib/$(DEB_HOST_MULTIARCH)/tcl$(TCL_VER) \
           --with-includes=/usr/include/tcl$(TCL_VER) \
           PYTHON=/usr/bin/python \
           $(COMMON_CONFIGURE_FLAGS) \
           $(BOOTSTRAP_FLAGS)
	touch "$@"
	
stamp/configure-build-py3:
	mkdir -p stamp
	dh_auto_configure --builddirectory=build-py3 -- \
           --with-python \
           PYTHON=/usr/bin/python3 \
           $(COMMON_CONFIGURE_FLAGS)
	touch "$@"

override_dh_auto_build: stamp/build stamp/build-py3

stamp/build: stamp/configure-build
	cd build && $(MAKE) world

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

	touch "$@"

stamp/build-py3: stamp/configure-build-py3
	cd build-py3 && $(MAKE) -C src/backend/ submake-errcodes && $(MAKE) -C src/pl/plpython
	touch "$@"

override_dh_auto_install:
	make -C build-py3/src/pl/plpython install DESTDIR=$(CURDIR)/debian/tmp
	make -C build install-world DESTDIR=$(CURDIR)/debian/tmp
	# compress manpages
	gzip -9n $(CURDIR)/debian/tmp/usr/share/postgresql/*/man/man*/*.[137]

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

override_dh_auto_clean:
	rm -rf build* stamp contrib/file_fdw/sql/file_fdw.sql

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

override_dh_compress:
	dh_compress -X.source -X.c

override_dh_install:
	dh_install --fail-missing

	# these go into the -pl* packages; -f because they don't exist for an -A build
	rm -f debian/postgresql-contrib-$(MAJOR_VER)/usr/share/postgresql/$(MAJOR_VER)/extension/pl*

	if [ -d debian/postgresql-doc-$(MAJOR_VER) ]; then \
		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; \
	fi

	# remove actual build path from Makefile.global for reproducibility
	sed -i -e "s!^abs_top_builddir.*!abs_top_builddir = /build/postgresql-$(MAJOR_VER)/build!" \
	       -e "s!^abs_top_srcdir.*!abs_top_srcdir = /build/postgresql-$(MAJOR_VER)/build/..!" \
	       debian/postgresql-server-dev-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/lib/pgxs/src/Makefile.global

override_dh_auto_test:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# when tests fail, print newest 3 log files
	# temp-install wants to be invoked from a top-level make, unset MAKELEVEL here
	# hurd doesn't implement semaphores shared between processes yet; succeed anyway so they at least have libpq5
	# plperl currently fails on kfreebsd-*
	unset MAKELEVEL; if ! make -C build check-world EXTRA_REGRESS_OPTS='--port=$(shell perl -le 'print 1024 + int(rand(64000))')'; then \
	    for l in `find build \( -name regression.diffs -o -name initdb.log -o -name postmaster.log \) | perl -we 'print map { "$$_\n"; } sort { (stat $$a)[9] <=> (stat $$b)[9] } map { chomp; $$_; } <>' | tail -3`; do \
		echo "******** $$l ********"; \
		cat $$l; \
	    done; \
	    case $(DEB_HOST_ARCH) in \
	        hurd-*|kfreebsd-*) exit 0 ;; \
	        *) exit 1 ;; \
	    esac; \
	fi
endif

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

override_dh_builddeb:
	dh_builddeb -- -Zxz # explicit xz compression for wheezy and precise
