#!/usr/bin/make -f

include /usr/share/ocaml/ocamlvars.mk
OCAML_NATIVE_ARCHS := $(subst lpia,,$(OCAML_NATIVE_ARCHS))

BUILDDIR=debian/tmp

%:
	dh $@ --with ocaml,tex

override_dh_auto_configure:
	autoconf
	dh_auto_configure -- \
		--disable-emacs-compilation \
		--disable-coq-libs \
		--libdir=/usr/lib/ocaml

override_dh_auto_build-arch:
	$(MAKE) all byte

override_dh_auto_build-indep:
	$(MAKE) doc
	cd doc/html; \
	for f in *.html; do \
	  sed -e "s|https://cdnjs.cloudflare.com/ajax/libs/mathjax/[0-9.]\+/latest.js|/usr/share/javascript/mathjax/MathJax.js|" $$f > $$f.out; \
	  mv $$f.out $$f; \
	done

override_dh_auto_install:
	# do nothing

override_dh_install-arch: build-arch
	$(MAKE) install install-lib DESTDIR=$(CURDIR)/$(BUILDDIR)
	dh_install -a -XLICENSE

override_dh_install-indep:
	# do nothing

override_dh_installexamples:
	dh_installexamples
	if [ -d debian/why3-examples/usr/share/doc/why3-examples/examples ]; \
	then \
	  find debian/why3-examples/usr/share/doc/why3-examples/examples \
	    -name why3shapes.gz \
	    -exec sh -c 'if [ $$(zcat {} | wc -c) -eq 0 ]; then \rm {}; fi' \;;\
	fi

override_dh_compress:
	dh_compress -Xmanual.pdf

ifneq ($(wildcard debian/why3/usr/lib/ocaml/why3/commands/why3ide.cmxs),)
  WHY3IDE_UNITS := $(shell ocamlobjinfo debian/why3/usr/lib/ocaml/why3/commands/why3ide.cmxs | sed -n 's/Name: //p' | xargs echo | tr ' ' ,)
else
  WHY3IDE_UNITS := $(shell ocamlobjinfo debian/why3/usr/lib/ocaml/why3/commands/why3ide.cma | sed -n 's/Unit name: //p' | xargs echo | tr ' ' ,)
endif

WHY3HYPO_UNITS := $(shell ocamlobjinfo debian/why3/usr/lib/ocaml/why3/plugins/hypothesis_selection.cma | sed -n 's/Unit name: //p' | xargs echo | tr ' ' ,)

override_dh_ocaml:
	dh_ocaml --nodefined-map=why3:$(WHY3IDE_UNITS),$(WHY3HYPO_UNITS)
