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

include /usr/share/dpkg/pkg-info.mk
include /usr/share/ocaml/ocamlvars.mk

%:
	dh $@ --with ocaml --buildsystem ocaml_dune

execute_before_dh_auto_build:
	cp elpi_REPL.ml elpi_REPL.ml.orig
	sed -e 's/%%VERSION_NUM%%/$(DEB_VERSION_UPSTREAM)/g' -i elpi_REPL.ml
	cp src/compiler.ml src/compiler.ml.orig
	sed -e 's/%%VERSION_NUM%%/$(DEB_VERSION_UPSTREAM)/g' -i src/compiler.ml
	cp src/trace_atd.ts src/trace_atd.ts.orig

override_dh_auto_test:
	make tests STACK=11141120

execute_after_dh_auto_install:
	# drop excess LICENSE files
	find debian/tmp -name "LICENSE*" -delete
	# we detect what to ship
	find debian/tmp -regextype posix-awk \
	  -regex '.*\.(cma|cmxs)$$' \
	  >> debian/libelpi-ocaml.install
	find debian/tmp -regextype posix-awk \
	  -regex '.*\.(a|cmi|cmo|cmt|cmti|cmx|cmxa|ml|mli|o])$$' \
	  >> debian/libelpi-ocaml-dev.install

execute_after_dh_auto_clean:
	rm -rf _log/ tests/test.exe data.csv*
	if test -f elpi_REPL.ml.orig; then \
	  mv elpi_REPL.ml.orig elpi_REPL.ml; \
	fi
	if test -f src/compiler.ml.orig; then \
	  mv src/compiler.ml.orig src/compiler.ml; \
	fi
	if test -f src/trace_atd.ts.orig; then \
	  mv src/trace_atd.ts.orig src/trace_atd.ts; \
	fi
