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

DESTDIR=$(CURDIR)/debian/tmp

include /usr/share/ocaml/ocamlvars.mk

export OCAMLFIND_DESTDIR := $(DESTDIR)/$(OCAML_STDLIB_DIR)

%:
	dh $@ --with ocaml

override_dh_auto_clean:
	dune clean
	rm -rf _tmp

override_dh_auto_build:
	dune build -p macaddr
	opam-installer --prefix=$(CURDIR)/_tmp/usr --libdir=..$(OCAML_STDLIB_DIR) macaddr.install
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune build -p ipaddr
	opam-installer --prefix=$(CURDIR)/_tmp/usr --libdir=..$(OCAML_STDLIB_DIR) ipaddr.install
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune build -p ipaddr-cstruct
	opam-installer --prefix=$(CURDIR)/_tmp/usr --libdir=..$(OCAML_STDLIB_DIR) ipaddr-cstruct.install
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune build -p ipaddr-sexp
	opam-installer --prefix=$(CURDIR)/_tmp/usr --libdir=..$(OCAML_STDLIB_DIR) ipaddr-sexp.install

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dune runtest -p macaddr
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune runtest -p ipaddr
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune runtest -p ipaddr-cstruct
	OCAMLPATH=$(CURDIR)/_tmp$(OCAML_STDLIB_DIR) dune runtest -p ipaddr-sexp
endif

override_dh_auto_install:
	rm -rf debian/tmp
	cp -a $(CURDIR)/_tmp debian/tmp
	rm -rf debian/tmp/usr/doc/ipaddr/LICENSE.md
	for u in macaddr ipaddr-cstruct ipaddr-sexp; do rm -rf $(DESTDIR)/usr/doc/$$u; done
