#!/usr/bin/make -f

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
BUILDDIR:=$(shell pwd)
AUTOGENERATED:= libhdfeos-dev.install

CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)

# Set SHELL in examples/ makefiles to /bin/sh
export CONFIG_SHELL=/bin/sh

# Magic debhelper rule
%:
	dh   $@ 

override_dh_auto_clean:
	find . -type l -delete
	rm -f libtool config.status config.log samples/*.hdf testdrivers/*/*.hdf
	rm -f $(patsubst %, debian/%, ${AUTOGENERATED})

override_dh_auto_test:
	@echo "Tests disabled"

override_dh_auto_configure:
	for f in ${AUTOGENERATED} ; do \
                sed -e 's%@ARCH@%${DEB_HOST_MULTIARCH}%g' < debian/$$f.in  > debian/$$f ; \
                done
	dh_auto_configure -- --enable-shared --with-pic \
		CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS) -Df2cFortran -I/usr/include/hdf"
	# FIXME: a sort of hack
	ln -sf testswath.f testdrivers/swath/testswath77.f
	ln -sf testpoint.f testdrivers/point/testpoint77.f

override_dh_auto_install:
	dh_auto_install
	# Undo permissions we don't want
	$(MAKE) -C samples clean
	chmod -x samples/*
	# Lose the builddir for reproducibility
	sed -e 's%${BUILDDIR}%/nonexistant%' < samples/Makefile > x
	mv x samples/Makefile
	# Symlinks
	dh_link -p libhdfeos0 $(LIBDIR)/libhdfeos.so.0.0.0  $(LIBDIR)/libhdfeos.so.0
	dh_link -p libhdfeos-dev $(LIBDIR)/libhdfeos.so.0.0.0 $(LIBDIR)/libhdfeos.so
