#!/usr/bin/make -f

SRCDIR=$(CURDIR)
include /usr/share/postgresql-common/pgxs_debian_control.mk

# Workaround for #488989:
# We need the expanded path at least for the RPATH because the symlink is only
# shipped in gcj-jre-headless which the bbuilt package does not depend on
export USR_LIB_JVM_GCJ := $(shell readlink -f /usr/lib/jvm/java-1.5.0-gcj)

build: build-stamp
build-stamp:
	dh_testdir
	test -d $(USR_LIB_JVM_GCJ)/include
	test -d $(USR_LIB_JVM_GCJ)/lib
	for version in $$(pg_buildext supported-versions .) ; do \
		echo "### Building for PostgreSQL $$version" && \
		$(MAKE) -C . USE_GCJ=1 PG_CONFIG=/usr/lib/postgresql/$$version/bin/pg_config CPPFLAGS+="-I$(USR_LIB_JVM_GCJ)/include -I$(USR_LIB_JVM_GCJ)/include/linux" && \
		mv build build-$$version || exit 1 ; \
	done
	touch build-stamp

clean: debian/control
	dh_testdir
	dh_testroot
	rm -rf build build-?.? build-stamp
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	for version in $$(pg_buildext supported-versions .) ; do \
		echo "### Installing for PostgreSQL $$version" && \
		mv build-$$version build && \
		PKG=postgresql-$$version-pljava-gcj && \
		DESTDIR=$(CURDIR)/debian/$$PKG && \
		$(MAKE) -C . install USE_GCJ=1 PG_CONFIG=/usr/lib/postgresql/$$version/bin/pg_config DESTDIR=$$DESTDIR && \
		install -d $$DESTDIR/usr/share/postgresql-$$version-pljava && \
		install -m 644 src/sql/*install.sql build/deploy.jar $$DESTDIR/usr/share/postgresql-$$version-pljava && \
		install -d $$DESTDIR/usr/share/doc/$$PKG/examples && \
		install -m 644 build/examples.jar $$DESTDIR/usr/share/doc/$$PKG/examples && \
		install -d $$DESTDIR/usr/share/lintian/overrides && \
		install -m644 debian/lintian-overrides $$DESTDIR/usr/share/lintian/overrides/$$PKG && \
		mv build build-$$version || exit 1 ; \
	done

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs -a --all debian/README.Debian docs/*.html
	dh_installexamples -a --all
	dh_install -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

.PHONY: build clean binary-indep binary-arch binary debian/control
