#!/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 dune-glob

%:
	dh $@

override_dh_auto_configure:
	mkdir -p debian/build
	cd debian/build \
	&& tar xf /usr/src/$(SRC_PACKAGE)-*.tar \
	&& tar xf */*.orig.tar.* \
	&& ( if [ -d */debian-patches ]; then \
	       dir="$$(echo dune-*)"; \
	       mv */debian-patches $$dir/patches \
	       && cd $$dir \
	       && quilt push -a; \
	     fi ) \
	&& 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) \
	  --docdir=/usr/share/ocaml-dune/doc \
	  $(ALL_DUNE_PACKAGES)

override_dh_auto_clean:
	rm -rf debian/build
