#!/usr/bin/make -f
SHELL          = /bin/bash
MAJOR_VER      := 9.4
MINOR_VER      := 7
# Prevous major version of postgres, used for detecting upgrades
PREV_MAJOR_VER := 9.2

upstream_name = postgresql
name          = pe-$(upstream_name)
prefix        = /opt/puppetlabs/server
bindir        = $(prefix)/bin
homedir       = $(prefix)/apps/$(upstream_name)
datadir       = $(homedir)/share
docdir        = $(datadir)/doc
libdir        = $(homedir)/lib
includedir    = $(homedir)/include
persistdir    = $(prefix)/data/$(upstream_name)
confdir       = /etc/puppetlabs/$(upstream_name)

puppet_libdir     = /opt/puppetlabs/puppet/lib
puppet_includedir = /opt/puppetlabs/puppet/include

# support both hardening-wrapper (for backports) and dpkg-buildflags
export DEB_BUILD_HARDENING = 1
export DEB_BUILD_OPTIONS = 1
export DH_OPTIONS

LDFLAGS+= -L$(puppet_libdir) -L$(libdir) -Wl,--as-needed -Wl,-z,now -Wl,-rpath=$(puppet_libdir),-rpath=$(libdir)
CFLAGS+= -I$(puppet_includedir)/openssl -I$(puppet_includedir) -I$(includedir) -fPIC

# Used to branch between sysv init vs. systemd - not sure why dh_installinit
# isn't doing the right thing (it installs both)
CODENAME := $(shell lsb_release -c | sed -n 's/^Codename:\s*\(.*\)$$/\1/p')
SYSV_CODENAMES := squeeze wheezy lucid precise trusty

# When protecting the postmaster with oom_adj=-17, allow the OOM killer to slay
# the backends (http://archives.$(upstream_name).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
export LIBNAME=lib

CONFIGURE_FLAGS= \
	--prefix=$(homedir) \
	--mandir=$(prefix)/share/man \
	--docdir=$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER) \
	--sysconfdir=$(confdir) \
	--datadir=$(datadir) \
	--includedir=$(includedir) \
	--enable-nls \
	--enable-integer-datetimes \
	--enable-thread-safety \
	--with-ossp-uuid \
	--with-pgport=5432 \
	--with-system-tzdata=/usr/share/zoneinfo \
	--with-pam \
	--with-openssl \
	--with-ldap \
	CFLAGS='$(CFLAGS)' \
	LDFLAGS='$(LDFLAGS)'

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


%:
	dh $@

override_dh_auto_configure: stamp/configure-build

stamp/configure-build:
	mkdir -p stamp build
	cd build && ../configure $(CONFIGURE_FLAGS)
	touch "$@"

override_dh_auto_build: stamp/build

stamp/build: stamp/configure-build
	cd build && $(MAKE) all && $(MAKE) -C contrib all && $(MAKE) -C contrib/uuid-ossp all
	# generate POT files for translators
	find -name nls.mk -exec sh -c "make -C \$$(dirname {}) init-po" \;
	# build tutorial stuff
	make -C build/src/tutorial NO_PGXS=1
	touch "$@"

override_dh_auto_install:
	cd build && make DESTDIR=$(CURDIR)/debian/tmp install && \
		make -C contrib DESTDIR=$(CURDIR)/debian/tmp install && \
		make -C contrib/uuid-ossp DESTDIR=$(CURDIR)/debian/tmp install && \
		make DESTDIR=$(CURDIR)/debian/tmp install-docs
	# compress manpages
	gzip -9n $(CURDIR)/debian/tmp/$(prefix)/share/man/man*/*.[137]

override_dh_makeshlibs:
	dh_makeshlibs

override_dh_shlibdeps:
	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):debian/$(name)/$(libdir) dh_shlibdeps

override_dh_auto_clean:
	rm -rf build* stamp

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

override_dh_compress:
	dh_compress -X.source -X.c

override_dh_install:
	# init functions have been pulled out into separate scripts for systemd targets
	sed \
		-e 's|^PGVERSION=.*$$|PGVERSION=$(MAJOR_VER).$(MINOR_VER)|' \
		-e 's|^PGMAJORVERSION=.*$$|PGMAJORVERSION=$(MAJOR_VER)|' \
		-e 's|^PGENGINE=.*$$|PGENGINE=$(bindir)|' \
		-e 's|^PREVMAJORVERSION=.*$$|PREVMAJORVERSION=$(PREV_MAJOR_VER)|' \
		-e 's|^PREVPGENGINE=.*$$|PREVPGENGINE=$(libdir)/pgsql/postgresql-$(PREV_MAJOR_VER)/bin|' \
	< debian/extras/postgresql-setup >$(CURDIR)/debian/tmp/$(homedir)/bin/postgresql-setup ; \
	chmod +x $(CURDIR)/debian/tmp/$(homedir)/bin/postgresql-setup ; \
	sed \
		-e 's|^PGVERSION=.*$$|PGVERSION=$(MAJOR_VER).$(MINOR_VER)|' \
		-e 's|^PREVMAJORVERSION=.*$$|PREVMAJORVERSION=$(PREV_MAJOR_VER)|' \
		-e 's|^PGDOCDIR=.*$$|PGDOCDIR=$(docdir)|' \
	< debian/extras/postgresql-check-db-dir >$(CURDIR)/debian/tmp/$(homedir)/bin/postgresql-check-db-dir ; \
	chmod +x $(CURDIR)/debian/tmp/$(homedir)/bin/postgresql-check-db-dir ; \
	dh_install --fail-missing
	# these get installed into the individual -/l* packages; -f because it
	# does not exist for an -A build
	rm -f debian/$(name)-contrib$(datadir)/extension/pl*
	if [ -d debian/$(name) ]; then \
		mkdir -p debian/$(name)$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER)/tutorial; \
		install src/tutorial/*.c src/tutorial/*.source src/tutorial/Makefile src/tutorial/README debian/$(name)$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER)/tutorial; \
		mkdir -p debian/$(name)$(prefix)/share/man ; \
		mkdir -p debian/$(name)$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER)/sgml; \
		install doc/src/sgml/*.sgml debian/$(name)$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER)/sgml; \
		install -d doc/src/sgml/ref debian/$(name)$(prefix)/share/doc/$(upstream_name)-$(MAJOR_VER).$(MINOR_VER)/sgml; \
	fi

override_dh_installinit:
	if $$(echo "$(SYSV_CODENAMES)" | grep -q $(CODENAME)) ; then \
		cp debian/extras/pe-postgresql.init debian/pe-postgresql-server.pe-postgresql.init ; \
	else \
		cp debian/extras/pe-postgresql.service debian/pe-postgresql-server.pe-postgresql.service ; \
		cp debian/extras/pe-postgresql.tmpfile debian/pe-postgresql-server.pe-postgresql.tmpfile ; \
	fi
	dh_installinit --noscripts --name=$(name)

override_dh_auto_test:

contrib-check:

contrib-installcheck:
