#!/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)

LIBS := -ljpeg -lz -lm -lsz

CFLAGS=$(shell dpkg-buildflags --get CFLAGS)
CFLAGS_NOWARN:=$(shell dpkg-buildflags --get CFLAGS | sed -e 's/-Werror=implicit-function-declaration//' )
CPPFLAGS=$(shell dpkg-buildflags --get CPPFLAGS)

# Set SHELL in examples/ makefiles to /bin/sh ?
# if set to sh then autoconf breaks
# 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

override_dh_auto_test:
	@echo "Tests disabled"

override_dh_auto_configure:
	dh_auto_configure -- --enable-shared --with-pic \
		--enable-fortran \
		CFLAGS="$(CFLAGS_NOWARN)" \
		CPPFLAGS="$(CPPFLAGS) -Df2cFortran -I/usr/include/hdf" \
		LIBS="$(LIBS)"
	# 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 libhdfeos0t64 $(LIBDIR)/libhdfeos.so.0.0.0  $(LIBDIR)/libhdfeos.so.0
	dh_link -p libhdfeos-dev $(LIBDIR)/libhdfeos.so.0.0.0 $(LIBDIR)/libhdfeos.so
	# Don't ship
	rm -f debian/tmp/usr/lib/*/libhdfeos.la
