#!/usr/bin/make -f
# -*- makefile -*-

# 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

ARCHIVES=libspatialite spatialite-tools
FULL_VERSION := $(shell dpkg-parsechangelog | grep ^Version | cut -d' ' -f2 | cut -d- -f1)
VERSION := $(shell echo $(FULL_VERSION) | sed -e "s/~beta[[:digit:]]\+/-BETA/")
LC_VERSION := $(shell echo $(VERSION) | tr A-Z a-z)

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
# see FTBFS #649302
ifeq ($(DEB_HOST_ARCH),powerpc)
EPSG := --disable-epsg
else
EPSG :=
endif

%:
	dh $@

get-orig-source:
	mkdir -p $(CURDIR)/tmp || true
	for archive in $(ARCHIVES); \
	do \
	    wget -O $(CURDIR)/$$archive.tar.gz http://www.gaia-gis.it/spatialite-$(VERSION)/$$archive-$(LC_VERSION).tar.gz; \
		tar xzovf $(CURDIR)/$$archive.tar.gz -C $(CURDIR)/tmp; \
		mv $(CURDIR)/tmp/$$archive-$(LC_VERSION) $(CURDIR)/tmp/$$archive; \
	done
	mv $(CURDIR)/tmp $(CURDIR)/spatialite-$(LC_VERSION)
	rm -rf $(CURDIR)/*.tar.gz
	tar czvf spatialite_$(FULL_VERSION).orig.tar.gz --exclude=debian --exclude-vcs spatialite-$(LC_VERSION)
	rm -rf $(CURDIR)/spatialite-$(LC_VERSION)

override_dh_auto_configure:
	dh_auto_configure -Dlibspatialite -- \
		--enable-geos \
		--enable-proj \
		$(EPSG) \
		--disable-geosadvanced
	dh_auto_configure -Dspatialite-tools

override_dh_auto_build:
	dh_auto_build -Dlibspatialite
	dh_auto_build -Dspatialite-tools

override_dh_auto_clean:
	dh_auto_clean -Dlibspatialite
	dh_auto_clean -Dspatialite-tools

override_dh_auto_install:
	dh_auto_install -Dlibspatialite
	dh_auto_install -Dspatialite-tools

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib:$(CURDIR)/debian/tmp/usr/lib
