#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/vendor.mk

MAJOR_VER := 14
#CASSERT_FLAGS := --enable-cassert

CATVERSION = $(shell awk '/CATALOG_VERSION_NO/ { print $$3 }' src/include/catalog/catversion.h)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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

export DPKG_GENSYMBOLS_CHECK_LEVEL=4

# if LLVM is installed, use it
ifneq ($(wildcard /usr/bin/llvm-config*),)
LLVM_FLAGS = --with-llvm LLVM_CONFIG=/usr/bin/llvm-config-11 CLANG=/usr/bin/clang-11
endif

# PostgreSQL 9.5+ does not have native spinlock support on alpha
ifneq ($(findstring $(DEB_HOST_ARCH), alpha),)
SPINLOCK_FLAGS = --disable-spinlocks
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
SYSTEMD_FLAGS = --with-systemd
SELINUX_FLAGS= --with-selinux
endif

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/ \
  --with-extra-version=" ($(DEB_VENDOR) $(DEB_VERSION))" \
  --enable-nls \
  --enable-thread-safety \
  --enable-tap-tests \
  --enable-debug \
  --enable-dtrace \
  $(CASSERT_FLAGS) \
  --disable-rpath \
  --with-uuid=e2fs \
  --with-gnu-ld \
  --with-lz4 \
  --with-pgport=5432 \
  --with-system-tzdata=/usr/share/zoneinfo \
  $(LLVM_FLAGS) \
  $(SYSTEMD_FLAGS) \
  $(SELINUX_FLAGS) \
  $(SPINLOCK_FLAGS) \
  AWK=mawk \
  MKDIR_P='/bin/mkdir -p' \
  PROVE='/usr/bin/prove' \
  TAR='/bin/tar' \
  XSLTPROC='xsltproc --nonet' \
  CFLAGS='$(CFLAGS)' \
  LDFLAGS='$(LDFLAGS)'

# allow missing gssapi and ldap during bootstrapping
ifeq ($(DEB_STAGE),stage1)
BOOTSTRAP_FLAGS=
else
BOOTSTRAP_FLAGS= --with-gssapi --with-ldap
endif

%:
	dh $@

override_dh_strip:
	dh_strip --no-automatic-dbgsym

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build -- \
           --with-icu \
           --with-tcl \
           --with-perl \
           --with-python \
           --with-pam \
           --with-openssl \
           --with-libxml \
           --with-libxslt \
           PYTHON=/usr/bin/python3 \
           $(COMMON_CONFIGURE_FLAGS) \
           $(BOOTSTRAP_FLAGS)
	# remove pre-built documentation
	rm -fv doc/src/sgml/*-stamp

override_dh_auto_build-indep:
	$(MAKE) -C build/doc all # build man + html

override_dh_auto_build-arch:
	# set MAKELEVEL to 0 to force building submake-generated-headers in src/Makefile.global(.in)
	MAKELEVEL=0 $(MAKE) -C build/src all
	$(MAKE) -C build/doc man # build man only
	$(MAKE) -C build/config all
	$(MAKE) -C build/contrib all
	# build tutorial stuff
	$(MAKE) -C build/src/tutorial NO_PGXS=1

override_dh_auto_install-arch:
	$(MAKE) -C build/doc/src/sgml install-man DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C build/src install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C build/config install DESTDIR=$(CURDIR)/debian/tmp
	$(MAKE) -C build/contrib install DESTDIR=$(CURDIR)/debian/tmp
	# move SPI examples into server package (they wouldn't be in the doc package in an -A build)
	mkdir -p debian/postgresql-$(MAJOR_VER)/usr/share/doc/postgresql-$(MAJOR_VER)
	mv debian/tmp/usr/share/doc/postgresql-doc-$(MAJOR_VER)/extension debian/postgresql-$(MAJOR_VER)/usr/share/doc/postgresql-$(MAJOR_VER)/examples

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

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

override_dh_auto_clean:
	rm -rf build

override_dh_installchangelogs:
	dh_installchangelogs HISTORY

override_dh_compress:
	dh_compress -X.source -X.c
	# compress manpages (excluding debian/tmp/)
	gzip -9n $(CURDIR)/debian/*-*/usr/share/postgresql/*/man/man*/*.[137]

override_dh_install-arch:
	dh_install -a

	# link README.Debian.gz to postgresql-common
	mkdir -p debian/postgresql-$(MAJOR_VER)/usr/share/doc/postgresql-$(MAJOR_VER)
	ln -s ../postgresql-common/README.Debian.gz debian/postgresql-$(MAJOR_VER)/usr/share/doc/postgresql-$(MAJOR_VER)/README.Debian.gz

	# assemble perl version of pg_config in libpq-dev
	sed -ne '1,/__DATA__/p' debian/pg_config.pl > debian/libpq-dev/usr/bin/pg_config
	LC_ALL=C debian/postgresql-client-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_config >> debian/libpq-dev/usr/bin/pg_config
	LC_ALL=C debian/postgresql-client-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/bin/pg_config --help >> debian/libpq-dev/usr/bin/pg_config
	chmod 755 debian/libpq-dev/usr/bin/pg_config

	# 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/..!" \
	       -e 's!-f\(debug\|file\)-prefix-map=[^ ]* !!g' \
	       debian/postgresql-client-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/lib/pgxs/src/Makefile.global

	# these are shipped in the pl packages
	bash -c "rm -v debian/postgresql-$(MAJOR_VER)/usr/share/postgresql/$(MAJOR_VER)/extension/{plperl,plpython,pltcl,*_pl}*"
	bash -c "rm -v debian/postgresql-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/lib/{plperl,plpython,pltcl,*_pl}*"
	bash -c "rm -rfv debian/postgresql-$(MAJOR_VER)/usr/lib/postgresql/$(MAJOR_VER)/lib/bitcode/*{plperl,plpython,pltcl}*"

	# record catversion in a file
	echo $(CATVERSION) > debian/postgresql-$(MAJOR_VER)/usr/share/postgresql/$(MAJOR_VER)/catalog_version

override_dh_install-indep:
	dh_install -i

	if [ -d debian/postgresql-doc-$(MAJOR_VER) ]; then set -e; \
		install -d debian/postgresql-doc-$(MAJOR_VER)/usr/share/doc/postgresql-doc-$(MAJOR_VER)/tutorial; \
		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

override_dh_auto_test-indep:
	# nothing to do

override_dh_auto_test-arch:
ifeq (, $(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# when tests fail, print newest 3 log files
	# initdb doesn't like LANG and LC_ALL to contradict, unset LANG and LC_CTYPE here
	# 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 LANG LC_CTYPE MAKELEVEL; ulimit -c unlimited; \
	if ! make -C build check-world \
	  EXTRA_REGRESS_OPTS='--port=$(shell perl -le 'print 1024 + int(rand(64000))')' \
	  TEMP_CONFIG=$(CURDIR)/debian/tests/postgresql.conf \
	  PG_TEST_EXTRA='ssl' \
	  PROVE_FLAGS="--verbose"; \
	then \
	    for l in `find build -name 'regress*' -o -name '*.log' | perl -we 'print map { "$$_\n"; } sort { (stat $$a)[9] <=> (stat $$b)[9] } map { chomp; $$_; } <>' | tail -n 10`; do \
		echo "******** $$l ********"; \
		cat $$l; \
	    done; \
	    for c in `find build -name 'core*'`; do \
	        echo "******** $$c ********"; \
	        gdb -batch -ex 'bt full' build/tmp_install/usr/lib/postgresql/$(MAJOR_VER)/bin/postgres $$c || :; \
	    done; \
	    case $(DEB_HOST_ARCH) in \
	        hurd-*|kfreebsd-*) exit 0 ;; \
	        *) exit 1 ;; \
	    esac; \
	fi
endif

override_dh_installdeb-arch:
	dh_installdeb
	# record catversion in preinst
	sed -i -e 's/@CATVERSION@/$(CATVERSION)/' debian/postgresql-$(MAJOR_VER)/DEBIAN/preinst

override_dh_gencontrol:
	# record catversion in .deb control file
	dh_gencontrol -- -Vpostgresql:Catversion=$(CATVERSION)
