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

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

export DEB_BUILD_OPTIONS += noautodbgsym

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

execute_before_dh_auto_build:
	cp src/trace_atd.ts src/trace_atd.ts.orig
	if test -f .git; then \
	  @echo "Found .git, ok"; \
        else \
          touch .false_git; \
          git init --initial-branch=main; \
	  git config user.email "foo@bar.baz"; \
	  git config user.name "Foo Bar"; \
          git add dune; \
          git commit -m foo; \
          git tag -a v$(DEB_VERSION_UPSTREAM) -m foo; \
        fi

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make tests STACK=11141120
endif

execute_after_dh_auto_install:
	# drop excess LICENSE files
	rm -f debian/tmp/usr/doc/elpi/LICENSE
	# don't declare the documentation files as part of the package
	find debian/tmp -name dune-package | xargs sed -i -e "s|(doc[^()]*([^()]*))||g"
	find debian/tmp -name dune-package | xargs sed -i -e "s|(doc[^()]*)||g"
	# 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 src/trace_atd.ts.orig; then \
	  mv src/trace_atd.ts.orig src/trace_atd.ts; \
	fi
	if test -f .false_git; then \
	  rm -rf .false_git .git; \
	fi
