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

DESTDIR=$(CURDIR)/debian/tmp

include /usr/share/ocaml/ocamlvars.mk

export OCAMLPATH := $(CURDIR)/_tmp$(OCAML_STDLIB_DIR)
export PATH := $(CURDIR)/_tmp/usr/bin:$(PATH)

DUNE_INSTALL := dune install --destdir=$(CURDIR)/_tmp --prefix=/usr --libdir=..$(OCAML_STDLIB_DIR)

%:
	dh $@ --with ocaml

override_dh_auto_clean:
	dh_auto_clean
	rm -rf _tmp

override_dh_auto_build:
	rm -f compiler/tests-jsoo/dune         # makes the build fail on amd64
	rm -f compiler/lib-runtime/tests/dune  # makes the build fail on bytecode architectures
ifeq ($(OCAML_HAVE_OCAMLOPT),no)
	sed -i 's/ native//' compiler/bin-js_of_ocaml/dune
endif
	dune build -p js_of_ocaml-compiler
	$(DUNE_INSTALL) js_of_ocaml-compiler
	dune build -p js_of_ocaml
	$(DUNE_INSTALL) js_of_ocaml
	dune build -p js_of_ocaml-ppx
	$(DUNE_INSTALL) js_of_ocaml-ppx
	dune build -p js_of_ocaml-ppx_deriving_json
	$(DUNE_INSTALL) js_of_ocaml-ppx_deriving_json
	dune build -p js_of_ocaml-lwt
	$(DUNE_INSTALL) js_of_ocaml-lwt
	dune build -p js_of_ocaml-ocamlbuild
	$(DUNE_INSTALL) js_of_ocaml-ocamlbuild
	dune build -p js_of_ocaml-toplevel
	$(DUNE_INSTALL) js_of_ocaml-toplevel
	dune build -p js_of_ocaml-tyxml
	$(DUNE_INSTALL) js_of_ocaml-tyxml
	$(MAKE) doc

override_dh_auto_test:
# Needs ppx-expect

override_dh_auto_install:
	rm -rf debian/tmp
	cp -a _tmp debian/tmp
	for u in js_of_ocaml-compiler js_of_ocaml-ppx js_of_ocaml-ppx_deriving_json js_of_ocaml-lwt js_of_ocaml-ocamlbuild js_of_ocaml-toplevel js_of_ocaml-tyxml; do \
	  rm -rf debian/tmp/usr/doc/$$u; \
	done
	rm -f debian/tmp/usr/doc/js_of_ocaml/LICENSE

override_dh_install:
	dh_install
	mkdir -p debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc
	rm -rf debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc/api
	rm -rf debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc/manual
	cp -a _build/default/_doc/_html debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc/api
	cp -a _build/default/manual debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc/manual
	sed -i \
	  's@href=".*//maxcdn\.bootstrapcdn\.com/bootstrap.*/bootstrap\.min\.css"@href="file:///usr/share/javascript/bootstrap/css/bootstrap.min.css"@g' \
	  debian/libjs-of-ocaml-doc/usr/share/doc/libjs-of-ocaml-doc/manual/files/toplevel/index.html
