#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# The build system is averse to VPATH building, so we can't properly use
# postgresql-server-dev-all
SRCDIR      = $(CURDIR)
PKGVERS     = $(shell dpkg-parsechangelog | awk -F '[:-]' '/^Version:/ { print substr($$2, 2) }')
ORIG_EXCLUDE=--exclude-vcs --exclude=debian

include /usr/share/postgresql-common/pgxs_debian_control.mk

debian/control-pgver-stamp: debian/control.in debian/pgversions
	rm -f debian/control
	$(MAKE) -f debian/rules debian/control
	touch $@

config.mak: debian/control-pgver-stamp
	#./autogen.sh
	./configure --prefix=/usr --with-asciidoc --with-sk3-subdir

override_dh_auto_configure: config.mak

override_dh_auto_clean: config.mak
	$(MAKE) -C doc realclean
	dh_auto_clean -- distclean
	for version in $$(pg_buildext supported-versions .) ; do \
		rm -rf debian/postgresql-$${version}-pgq3; \
	done
	rm -f debian/control-pgver-stamp

# upstream build system will build fine for one given PostgreSQL version,
# then we build the PostgreSQL module and files for yet another version of
# it so that we have binary packages for postgresql-8.4-pgq3 and 9.0.
override_dh_auto_install: 
	mkdir -p $(CURDIR)/debian/tmp
	dh_auto_install
	$(MAKE) -C doc htmlinstall DESTDIR=$(CURDIR)/debian/tmp
	# now care about any previous supported versions
	for version in $$(pg_buildext supported-versions .) ; do \
		echo "### Building for PostgreSQL $$version" && \
		make -C sql clean install \
			PG_CONFIG=/usr/lib/postgresql/$$version/bin/pg_config \
			DESTDIR=$(CURDIR)/debian/tmp \
		|| exit 1 ; \
	done

orig: config.mak
	rm -rf dist
	make tgz
	mv dist/*.tar.gz ../skytools3_$(PKGVERS).orig.tar.gz

%:
	dh $@

