#!/usr/bin/make -f

include /usr/share/ocaml/ocamlvars.mk

empty :=
space := $(empty) $(empty)
comma := ,

SRC_PACKAGE := ocaml-dune-source
DEB_PACKAGES := libdune-ocaml-dev libdune-ocaml
MAIN_DUNE_PACKAGE := dune-private-libs
ALL_DUNE_PACKAGES := $(MAIN_DUNE_PACKAGE) dune-site stdune dyn

%:
	dh $@

override_dh_auto_configure:
	mkdir -p debian/build
	cd debian/build \
	&& tar xf /usr/src/$(SRC_PACKAGE)-*.tar \
	&& tar xf */*.orig.tar.* \
	&& rm -rf ocaml-dune-*

override_dh_auto_build:
	cd debian/build/dune-* \
	&& dune build -p $(subst $(space),$(comma),$(ALL_DUNE_PACKAGES))

override_dh_auto_install:
	cd debian/build/dune-* \
	&& dune install --destdir=$(CURDIR)/debian/tmp --prefix=/usr --libdir=$(OCAML_STDLIB_DIR) $(ALL_DUNE_PACKAGES)

override_dh_auto_clean:
	rm -rf debian/build

override_dh_installchangelogs:
	for pkg in $(DEB_PACKAGES); do \
	  dh_installchangelogs -p$$pkg debian/tmp/usr/doc/$(MAIN_DUNE_PACKAGE)/CHANGES.md; \
	done

execute_before_dh_installdocs:
	for pkg in $(DEB_PACKAGES); do \
	  cp /usr/share/doc/$(SRC_PACKAGE)/copyright debian/$$pkg.copyright; \
	done

execute_after_dh_installdocs:
	for pkg in $(DEB_PACKAGES); do \
	  rm -f debian/$$pkg/usr/share/doc/*/CHANGES.md*; \
	done
