#!/usr/bin/make -f

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

DEB_LDFLAGS_MAINT_PREPEND:= -Wl,-z,defs -Wl,--as-needed
DEB_BUILD_MAINT_OPTIONS:= hardening=+all,-format
DEB_CFLAGS_MAINT_APPEND:= -Wall -pedantic -fPIC
export DEB_BUILD_MAINT_OPTIONS DEB_CFLAGS_MAINT_APPEND DEB_LDFLAGS_MAINT_PREPEND
export QT_SELECT=5
CXXFLAGS:= $(shell dpkg-buildflags --get CXXFLAGS) -std=c++11

# Needed for sbuild. metview creates temp directories during build
export HOME:=/tmp
export METVIEW_LOCALHOST:=127.0.0.1
export METVIEW_DEBUG:=1

DESTDIR:=$(CURDIR)/debian/tmp/
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
TARGET_ARCH ?= $(shell dpkg-architecture -qDEB_TARGET_ARCH_CPU)
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
LC_ALL=C
export LC_ALL

FPIC:= ''

ifeq ($(TARGET_ARCH), i386)
	FPIC:= -fPIC
endif

# 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})
	[ ! -f scripts/metview_base.in.bak ] || \
		( mv scripts/metview_base.in.bak scripts/metview_base.in )

# 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
	# Hack. how to do this via ecbuild?
	cp scripts/metview_base.in scripts/metview_base.in.bak
	sed -e 's%@DEB_HOST_MULTIARCH@%${DEB_HOST_MULTIARCH}%g' < scripts/metview_base.in \
		> scripts/tmpx && mv scripts/tmpx scripts/metview_base.in
	dh_auto_configure -- \
		-DMETVIEW_SITE=debian  \
		-DCMAKE_BUILD_TYPE=Release \
		-DMETVIEW_EXTRA_LIBRARIES='terralib -lemosR64' \
		-DMETVIEW_EXTRA_INCLUDES='/usr/include/terralib/kernel' \
		-DMAGICS_HAVE_METVIEW=True \
		-DMAGICS_HAVE_NETCDF=True \
		-DMAGICS_PATH=/usr \
		-DBUILD_SHARED_LIBS=On \
		-DENABLE_RPATHS=Off \
		-DFLEXTRA_PATH=/usr/bin/flextra \
		-DENABLE_QT5=On  \
		-DGRIB_API_PATH=/usr/lib \
		-DDISABLE_OS_CHECK=On \
		-DFPIC=${FPIC}
	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 
	# These get generated ok on a machine with network connection, but fail on sbuilds, etc.
	# so copy pre-generated versions stored in etc
	mkdir -p debian/build/share/metview/etc/
	cp debian/etc/* debian/build/share/metview/etc/
	cd debian/build/share/metview/etc && $(MAKE) ObjectListAllTarget

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

override_dh_fixperms-arch:
	dh_fixperms
	chmod 755 debian/metview/$(MV_DIR)/mv_compress
	chmod 644 debian/metview/$(MV_DIR)/metview_*.common
	chmod 644 debian/metview/$(MV_DIR)/metview_stat
	chmod 644 debian/metview/$(MV_DIR)/metview_patches
	file debian/metview/$(MV_DIR)/* | grep ELF | cut -f1 -d: | xargs chrpath -d

override_dh_fixperms-indep:
	dh_fixperms
	rm -rf debian/metview-data/usr/share/applications
	find debian/metview-data/usr/share/metview -type f -exec chmod 644 {} \;
	find debian/metview-data/usr/share/metview -type d -exec chmod 755 {} \; 
	# Hack. Why weren't these already compressed -n ?
	gunzip debian/metview-data/usr/share/metview/app-defaults/Sample_Drawers.tar.gz
	gunzip debian/metview-data/usr/share/metview/app-defaults/User_Dir_Frame.tar.gz
	gzip -n debian/metview-data/usr/share/metview/app-defaults/Sample_Drawers.tar
	gzip -n debian/metview-data/usr/share/metview/app-defaults/User_Dir_Frame.tar
