#!/usr/bin/make -f

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


DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS) \
	-I/usr/include/cairo  \
	-I/usr/include/freetype2 \
	-I/usr/include/fontconfig

# Build MPI version
ifneq (,$(findstring mpi,$(DEB_BUILD_OPTIONS)))
        COND_CC:= "CC=mpicc"
else
        COND_CC:=
endif

export SUPPLIBS=/usr

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  WITH_HDF5:=-with-hdf5_include=/usr/include/hdf5/serial \
	--with-hdf5_libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
else
  WITH_HDF5:=--with-hdf5
endif

override_dh_auto_clean:
	# Remove stuff added from tarballs
	rm -f doc/model.* doc/sample
	rm -f src/grads src/bufrscan src/wgrib src/stnmap src/gribmap src/gribscan
	find . -name '*.o' -delete
	# rm -f data/font* data/mres data/lowres data/hires data/udunits.dat

override_dh_auto_configure:
	autoreconf -fiv
	dh_auto_configure -- $(COND_CC) \
		--enable-dyn-supplibs --with-readline --with-printim \
		--with-grib2 --with-netcdf  --with-cairo --with-gui \
		$(WITH_HDF5) \
		--with-geotiff --with-sdf \
		--with-hdf4-include=/usr/include/hdf --with-hdf4-libdir=/usr/lib \
		--with-gadap \
		LIBS=' -lcairo -lfreetype'
	mkdir -p  data doc
	( cd data ; tar xapf ../../grads_2.1.a3.orig-data.tar.xz )
	( cd doc ; tar xapf ../../grads_2.1.a3.orig-example.tar.xz )

