#!/usr/bin/make -f

CFLAGS+=$(CPPFLAGS)
DEB_BUILD_ARCH_CPU := $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU 2>/dev/null)
ifeq ($(DEB_BUILD_ARCH_CPU), arm)
    DISABLE_GUI := --disable-gui
else ifeq ($(DEB_BUILD_ARCH_CPU), armhf)
    DISABLE_GUI := --disable-gui
else ifeq ($(DEB_BUILD_ARCH_CPU), armel)
    DISABLE_GUI := --disable-gui
else
    DISABLE_GUI := 
endif

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

override_dh_auto_configure:
	dh_auto_configure -- $(DISABLE_GUI) --prefix=/usr --enable-python $(shell dpkg-buildflags --export=configure)

VERSION := $(shell grep '^AC_INIT' configure.ac | \
	sed 's/AC_INIT(saga, \([0-9|\.]*\)\,.*)/\1/g')
UPSTREAM_URL ?=$(shell uscan --dehs | sed -n 's/.*<upstream-url>\(.*\)<\/upstream-url>.*/\1/p')
UPSTREAM_VERSION ?=$(shell uscan --dehs | sed -n 's/.*<upstream-version>\(.*\)<\/upstream-version>.*/\1/p')

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

override_dh_python2:
	dh_python2 --no-guessing-versions

override_dh_clean:
	dh_clean
	rm -f src/saga_core/saga_api/saga_api.py

override_dh_makeshlibs:
	dh_makeshlibs -Xpython-saga

get-orig-source:
	wget $(UPSTREAM_URL)
	gunzip *.tar.gz
	tar --delete -f saga_$(UPSTREAM_VERSION).tar saga-$(UPSTREAM_VERSION)/src/modules/grid/grid_gridding/nn/triangle.c
	tar --delete -f saga_$(UPSTREAM_VERSION).tar saga-$(UPSTREAM_VERSION)/src/modules/grid/grid_gridding/nn/triangle.h
	tar --delete -f saga_$(UPSTREAM_VERSION).tar saga-$(UPSTREAM_VERSION)/src/modules/simulation/sim_fire_spreading
	gzip -9n saga_$(UPSTREAM_VERSION).tar
	mv saga_$(UPSTREAM_VERSION).tar.gz ../saga_$(UPSTREAM_VERSION)+dfsg.orig.tar.gz 
