#!/usr/bin/make -f

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

DESTDIR:=`pwd`/debian/tmp/
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
INCDIR:=/usr/include/$(DEB_HOST_MULTIARCH)/hdf

PKGDIR:=`pwd`/debian/libhdfeos0
DEVPKGDIR:=`pwd`/debian/libhdfeos-dev

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

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

override_dh_auto_clean:
	find . -type l -delete
	-$(MAKE) -k clean
	rm -f libtool config.status config.log samples/*.hdf testdrivers/*/*.hdf

override_dh_autoreconf:
	SHELL=/bin/bash dh_autoreconf --as-needed 

override_dh_auto_test:
	@echo "Tests disabled"

override_dh_auto_configure:
	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
	# Delete the libtool crap and install debian libtool
	rm -f libtool
	ln -sf /usr/bin/libtool . 

override_dh_auto_install:
	dh_auto_install
	# Undo permissions we don't want
	$(MAKE) -C samples clean
	chmod -x samples/*
	rm config.status config.log
	mkdir -p $(DEVPKGDIR)/$(LIBDIR)/pkgconfig
	cp hdf-eos4.pc $(DEVPKGDIR)/$(LIBDIR)/pkgconfig
	mkdir -p $(PKGDIR)/$(LIBDIR)
	mv $(DESTDIR)/usr/lib/*.0 $(PKGDIR)/$(LIBDIR)
	mkdir -p $(DEVPKGDIR)/$(LIBDIR)
	mv $(DESTDIR)/usr/lib/*.a $(DEVPKGDIR)/$(LIBDIR)
	mkdir -p  $(DEVPKGDIR)/$(INCDIR)
	for d in ease.h cfortHdf.h HdfEosDef.h  HDFEOSVersion.h HE2_config.h ; do \
	  cp include/$$d $(DEVPKGDIR)/$(INCDIR) ; done
	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
