#!/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)
export METVIEW_DIR_DEV=$(CURDIR)
PATH:=$(PATH):$(CURDIR)/scripts
HOME:=$(CURDIR)/debian/tmp
MV_COMP:=$(CURDIR)/debian/tmp/usr/bin/Metview/mv_compress

# magic debhelper rule
%:
	dh $@ --with autoreconf

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) clean
	dh_clean
	rm -f share/metview/icons/make.dep config/CONFIG.site \
		share/metview/app-defaults/MvVersionDetails  \
		include/macro_api.h etc/ConfigDef
	find . -type l -delete
	find . -name '*.db' -delete

# 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:
	dh_auto_configure -- \
		--enable-magics \
		LIBS=" -lterralib -lm" CPPFLAGS=" -I/usr/include/terralib/kernel " \
		CXXFLAGS=" -fPIC " CFLAGS=" -fPIC"
	cp debian/CONFIG.site config/CONFIG.site
	chmod +x ./scripts/*.mv

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

override_dh_auto_install:
	dh_auto_install
	mkdir -p debian/libmetview-dev/$(LIBDIR)
	cp debian/tmp/usr/lib*/*.a debian/libmetview-dev/$(LIBDIR)
	sed -e 's%function findCommonPath()%findCommonpath()%'  \
		< $(MV_COMP) >  $(MV_COMP).tmp
	mv $(MV_COMP).tmp  $(MV_COMP)

override_dh_fixperms:
	dh_fixperms
	rm -rf debian/metview-data/usr/share/applications
	chmod +x debian/metview/usr/lib/metview/mv_compress
	chmod -x debian/metview/usr/lib/metview/*.magml
	chmod -x debian/metview/usr/lib/metview/metview_*.common
	chmod -x debian/metview/usr/lib/metview/metview_stat
	chmod -x debian/metview/usr/lib/metview/metview_patches
	chmod -x debian/metview/usr/lib/metview/*txt
	chmod -x debian/metview/usr/lib/metview/*.xml
	# 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 {} \;  )
