#!/usr/bin/make -f
#export DH_VERBOSE = 1

include /usr/share/GNUstep/debian/config.mk
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export LINKSQLCLIENT := 1

sover := 1.9

# Run ecpg via cross-exe-wrapper if cross-building.
ifneq ($(DEB_HOST_ARCH),$(DEB_BUILD_ARCH))
OLDPATH := $(PATH)
export PATH := $(CURDIR)/debian:$(OLDPATH)

execute_before_dh_auto_build:
	sed -e 's|@DEB_HOST_GNU_TYPE@|$(DEB_HOST_GNU_TYPE)|' \
	  debian/cross-ecpg.in > debian/ecpg
	chmod +x debian/ecpg
endif

%:
	dh $@

override_dh_auto_build:
ifdef docs
	cp debian/gnustep-sqlclient-doc.install.in \
	  debian/gnustep-sqlclient-doc.install
	cp debian/gnustep-sqlclient-doc.links.in \
	  debian/gnustep-sqlclient-doc.links
endif
	dh_auto_build -- $(optim) $(verbose) $(docs) \
	  $(shell dpkg-buildflags --export=cmdline)

override_dh_auto_install:
	dh_auto_install -- $(docs)

override_dh_auto_clean:
	dh_auto_clean -- $(docs)
	$(RM) -r Bundles
	dh_clean test test.conf debian/ecpg \
	  debian/gnustep-sqlclient-doc.{install,links}

override_dh_auto_test:
# Test the SQLite, Postgres and ECPG bundles.
	echo "GNUSTEP_LOCAL_LIBRARY=$(CURDIR)" > test.conf
	chmod 0644 test.conf
	mkdir -p Bundles/SQLClient$(sover)
	cp -a SQLite.bundle Bundles/SQLClient$(sover)
	LD_LIBRARY_PATH=$(CURDIR)/obj:$$LD_LIBRARY_PATH \
	  GNUSTEP_CONFIG_FILE=$(CURDIR)/test.conf ./obj/testSQLite
# Temporarily skip these tests on GNU/Hurd (#1137426).
ifneq ($(DEB_HOST_ARCH_OS),hurd)
	cp -a Postgres.bundle Bundles/SQLClient$(sover)
	LD_LIBRARY_PATH=$(CURDIR)/obj:$$LD_LIBRARY_PATH \
	  GNUSTEP_CONFIG_FILE=$(CURDIR)/test.conf LC_ALL=C.UTF-8 \
	  pg_virtualenv -i "-U postgres -A trust" ./obj/testPostgres
	cp -a ECPG.bundle Bundles/SQLClient$(sover)
	LD_LIBRARY_PATH=$(CURDIR)/obj:$$LD_LIBRARY_PATH \
	  GNUSTEP_CONFIG_FILE=$(CURDIR)/test.conf LC_ALL=C.UTF-8 \
	  pg_virtualenv -i "-U postgres -A trust" ./obj/testECPG
endif

execute_before_dh_link:
	gsdh_gnustep
