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

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# testrunner fails on some architectures
DEB_BUILD_ARCH      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

# Guessing geos version, which is required for C++ binding support
VER_MAJOR=$(shell grep ^VERSION_MAJOR configure.*|cut -d= -f2)
VER_MINOR=$(shell grep ^VERSION_MINOR configure.*|cut -d= -f2)
VER_PATCH=$(shell grep ^VERSION_PATCH configure.*|cut -d= -f2|cut -dr -f1)
VER=$(VER_MAJOR).$(VER_MINOR).$(VER_PATCH)

ifeq (..,$(VER))
$(error configure.ac/configure.in not found or version cannot be guessed)
else
$(info Guessed geos version is $(VER))
endif

CXX = g++ -fpermissive

# catch both armel and armhf 
ifneq (,$(findstring arm-linux-gnueabi,$(DEB_HOST_GNU_TYPE)))
       CONFFLAGS += --disable-inline
endif

debian/control: 
	@sed -e 's/@VERSION@/$(VER)/' debian/control.in >$@

%:
	dh $@ --with autoreconf --parallel

override_dh_clean:
	dh_clean

	rm -f debian/libgeos-$(VER).install debian/libgeos-$(VER).dirs debian/libgeos++-dev.install

override_dh_auto_configure: debian/control
	dh_auto_configure -- \
		--host=$(DEB_HOST_GNU_TYPE) \
        	--build=$(DEB_BUILD_GNU_TYPE) \
        	--enable-ruby \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		$(CONFFLAGS) \
		CXX="$(CXX)" \
		$(shell dpkg-buildflags --export=configure)

	cp debian/libgeos-c++.install debian/libgeos-$(VER).install
	cp debian/libgeos-c++.dirs debian/libgeos-$(VER).dirs

override_dh_auto_build-arch:
	dh_auto_build --arch

override_dh_auto_build-indep:
	$(MAKE) -C doc doxygen-html

override_dh_auto_test:
ifneq (,$(findstring $(DEB_BUILD_ARCH),"arm64 s390x powerpc powerpcspe ppc64 ppc64el"))
	# Skip tests
else
	dh_auto_test
endif

override_dh_install-arch:
	# add C++ headers to C++ dev package install file
	cp debian/libgeos++-dev.install.in debian/libgeos++-dev.install
	find debian/tmp/usr/include/geos -name "*.h" -a ! -name "export.h" | sed -e 's/^debian\/tmp\///' >>debian/libgeos++-dev.install

	dh_install --same-arch

	# remove the library from the -dev package
	rm -f $(CURDIR)/debian/libgeos-dev/usr/lib/libgeos-*.so

override_dh_install-indep:
	dh_install -i

	# Install doxygen html files
	install -m 644 doc/doxygen_docs/html/* debian/libgeos-doc/usr/share/doc/libgeos-doc/html

	# Install example program source
	install -m 644 doc/example.cpp debian/libgeos-doc/usr/share/doc/libgeos-doc/examples
	install -m 644 debian/Makefile.example debian/libgeos-doc/usr/share/doc/libgeos-doc/examples/Makefile

override_dh_installman:
	dh_installman -plibgeos-dev debian/geos-config.1

override_dh_strip:
	dh_strip --dbg-package=libgeos-dbg

override_dh_makeshlibs:
	dh_makeshlibs -V

override_dh_gencontrol:
	for i in $$(grep ^Package: debian/control|cut -d' ' -f2); do \
		echo "geos:Version=$(VER)" >> $$i.substvars; \
	done

	dh_gencontrol

