#!/usr/bin/make -f
# -*- makefile -*-

export DH_VERBOSE=1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export MPLCONFIGDIR=$(CURDIR)
export DESTDIR=$(CURDIR)/debian/tmp
# hkl-test (binoculars-ng) fails in the build environment for locale/encoding
# reasons (hGetContents cannot decode the UTF-8 test data under a non-UTF-8
# locale); skip the test suite.
export DEB_BUILD_OPTIONS := $(DEB_BUILD_OPTIONS) nocheck

HDF5_VERSION = $(shell h5cc -showconfig | grep 'HDF5 Version' | awk '{print $$3}')

# Whether the *installed Haskell bindings* (libghc-hdf5-dev) export the new
# HDF5-1.14 style name H5L_info1_t.  The C library version alone is not a
# reliable criterion: openKylin's bindings-hdf5 1.8.15 is built against
# HDF5 1.14 but still exports the old name H5L_info_t, so renaming the
# imports in H5.hs would break the GHC build.
H5L_INFO1_T := $(shell d=$$(ghc-pkg field hdf5 import-dirs --simple-output 2>/dev/null | cut -d' ' -f1); [ -n "$$d" ] && grep -qs H5L_info1_t "$$d/Bindings/HDF5/Raw.hi" && echo yes)

%:
	dh $@ --with=gir

override_dh_clean:
	set -x; [ ! -f binoculars-ng/src/Hkl/H5.hs.orig ] || mv binoculars-ng/src/Hkl/H5.hs.orig binoculars-ng/src/Hkl/H5.hs
	rm -f Documentation/api/hkl-docs.sgml
	dh_clean

override_dh_autoreconf:
	# Support for HDF5 1.14: rename H5L_info_t/h5l_iterate only when the
	# installed Haskell bindings actually export the new-style names.
	set -x; [ '$(H5L_INFO1_T)' != yes ] || [ -f binoculars-ng/src/Hkl/H5.hs.orig ] || \
	  sed -i.orig 's/H5L_info_t/H5L_info1_t/g;s/h5l_iterate/h5l_iterate1/g' binoculars-ng/src/Hkl/H5.hs
	dh_autoreconf ./autogen.sh

override_dh_auto_configure-indep: docflags = \
	--enable-gtk-doc \
	--htmldir=/usr/share/doc/libhkl-doc \
	--with-mathjax=/usr/share/javascript/mathjax/MathJax.js?config=TeX-AMS_HTML

override_dh_auto_configure-arch: docflags = --disable-gtk-doc --disable-hkl-doc  --enable-datatype99

override_dh_auto_configure-arch override_dh_auto_configure-indep:
	dh_auto_configure -- $(docflags) --enable-hkl3d

override_dh_auto_install-arch:
	dh_auto_install

	# ghkl
	dh_install -p ghkl usr/bin
	dh_install -p ghkl usr/share/applications
	dh_install -p ghkl usr/share/hkl
	dh_install -p ghkl usr/share/man

	# gir1.2-hkl-5.0
	dh_install -p gir1.2-hkl-5.0 usr/lib/*/girepository-1.0

	# libhkl-dev
	dh_install -p libhkl-dev usr/include
	dh_install -p libhkl-dev usr/lib/*/lib*.so
	dh_install -p libhkl-dev usr/lib/*/pkgconfig/*.pc
	dh_install -p libhkl-dev usr/share/gir-1.0/

	# libhkl5
	dh_install -p libhkl5 usr/lib/*/lib*.so.*

override_dh_auto_install-indep:
	dh_auto_build -- install-html DESTDIR="$(DESTDIR)"
	dh_auto_build -- install-data DESTDIR="$(DESTDIR)"

	# libhkl-doc
	dh_install -p libhkl-doc usr/share/doc
	dh_install -p libhkl-doc usr/share/gtk-doc

override_dh_missing:
	dh_missing --list-missing -Xlibhkl.la -Xlibhkl.a
