#!/usr/bin/make -f

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

DEB_BUILD_MAINT_OPTIONS:= hardening=+all,-format
DEB_CFLAGS_MAINT_APPEND:= -Wall -pedantic -fPIC
export DEB_BUILD_MAINT_OPTIONS
export DEB_CFLAGS_MAINT_APPEND

DESTDIR:=$(CURDIR)/debian/tmp/
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
PATH:=$(PATH):$(CURDIR)/scripts
AUTOGENERATED:= libmetview-dev.install  metview.install
MV_DIR:=/usr/lib/$(DEB_HOST_MULTIARCH)/metview
export METVIEW_DIR_DEV:=$(CURDIR)/debian/build
export METVIEW_BIN:=$(METVIEW_DIR_DEV)/bin

# magic debhelper rule
%:
	dh $@ --buildsystem=cmake --builddirectory=$(METVIEW_DIR_DEV)

override_dh_auto_clean:
	dh_clean
	rm -rf $(METVIEW_DIR_DEV)
	rm -f $(patsubst %, debian/%, ${AUTOGENERATED})

# magics++ upstream ships with a copy of terralib, which is not used in Debian. 
# Instead we use # a shared terralib library, and so need to include terralib.
# Also -fPIC, for some reason.

override_dh_auto_configure:
	chmod +x ./scripts/*.mv
	dh_auto_configure -- \
		-DMETVIEW_SITE=debian  \
		-DCMAKE_BUILD_TYPE=Debug \
		-DMETVIEW_EXTRA_LIBRARIES='terralib' \
		-DMETVIEW_EXTRA_INCLUDES='/usr/include/terralib/kernel' \
		-DMAGICS_PATH=/usr \
		-DBUILD_SHARED_LIBS=On \
		-DENABLE_RPATHS=Off
	for f in ${AUTOGENERATED} ; do \
                sed -e 's%@ARCH@%${DEB_HOST_MULTIARCH}%g' < debian/$$f.in  > debian/$$f ; \
                done
	sed -e 's%@ARCH@%${DEB_HOST_MULTIARCH}%g'  < debian/CONFIG.site.in \
		> $(METVIEW_DIR_DEV)/scripts/CONFIG.site 

override_dh_auto_test:
	@echo "Checks disabled for the moment due to syntax errors in tests" 

override_dh_fixperms:
	dh_fixperms
	rm -rf debian/metview-data/usr/share/applications
	chmod +x debian/metview/$(MV_DIR)/mv_compress
	chmod -x debian/metview/$(MV_DIR)/metview_*.common
	chmod -x debian/metview/$(MV_DIR)/metview_stat
	chmod -x debian/metview/$(MV_DIR)/metview_patches
	file debian/metview/$(MV_DIR)/* | grep ELF | cut -f1 -d: | xargs chrpath -d
	# This directory won't exist on arch-only builds, so ...
	[ ! -d debian/metview-data/usr/share/metview ] || (  \
		chmod -R -x debian/metview-data/usr/share/metview/* ; \
		find debian/metview-data/usr/share/metview -type d -exec chmod 755 {} \;  )
