#!/usr/bin/make	-f 
TCL_VER := 8.5
MAJOR_VER := 9.2
MINOR_VER := 4

name_prefix = pe-
pe_prefix = opt/puppet
etc_location = etc/puppetlabs

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

LDFLAGS+= -L/opt/puppet/lib -Wl,--as-needed -Wl,-z,now -Wl,-rpath=/opt/puppet/lib
CFLAGS+= -I/opt/puppet/include/openssl -I/opt/puppet/include -fPIC

# 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
export LIBNAME=lib

COMMON_CONFIGURE_FLAGS= \
  --prefix=/$(pe_prefix) \
  --mandir=/$(pe_prefix)/share/man \
  --docdir=/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER) \
  --sysconfdir=/$(etc_location)/postgresql \
  --datadir=/$(pe_prefix)/share/postgresql \
  --includedir=/$(pe_prefix)/include \
  --enable-nls \
  --enable-integer-datetimes \
  --enable-thread-safety \
  --disable-rpath \
  --with-ossp-uuid \
  --with-pgport=5432 \
  --with-system-tzdata=/usr/share/zoneinfo \
	--with-pam \
	--with-openssl \
	--with-libxml \
	--with-libxslt \
	--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 \
           $(COMMON_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/$(pe_prefix)/share/man/man*/*.[137]
override_dh_makeshlibs:
	dh_makeshlibs

override_dh_shlibdeps:
	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):debian/$(name_prefix)postgresql/$(pe_prefix)/lib 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:
	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/pe-postgresql-contrib/opt/puppet/postgresql/share/extension/pl*

	if [ -d debian/$(name_prefix)postgresql ]; then \
		mkdir -p debian/$(name_prefix)postgresql/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER)/tutorial; \
		install src/tutorial/*.c src/tutorial/*.source src/tutorial/Makefile src/tutorial/README debian/$(name_prefix)postgresql/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER)/tutorial; \
		mkdir -p debian/$(name_prefix)postgresql/usr/share/man ; \
		mkdir -p debian/$(name_prefix)postgresql/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER)/sgml; \
		install doc/src/sgml/*.sgml debian/$(name_prefix)postgresql/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER)/sgml; \
		install -d doc/src/sgml/ref debian/$(name_prefix)postgresql/usr/share/doc/$(name_prefix)postgresql-$(MAJOR_VER).$(MINOR_VER)/sgml; \
	fi

override_dh_installinit:
	dh_installinit --no-scripts --name=$(name_prefix)postgresql

override_dh_auto_test:

contrib-check:

contrib-installcheck:
