#!/usr/bin/make -f
PACKAGE = cduce

include /usr/share/ocaml/ocamlinit.mk
ifeq ($(OCAML_NATDYNLINK),yes)
  MAKE_OPTS := "NATIVE=true"
else
  MAKE_OPTS := "NATIVE=false"
endif

ifeq ($(DEB_BUILD_ARCH), armhf)
  CAMLOPT_P := "ocamlopt -inline 100"
else ifeq ($(DEB_BUILD_ARCH), armel)
  CAMLOPT_P := "ocamlopt -inline 1000"
else ifeq ($(DEB_BUILD_ARCH), powerpc)
  CAMLOPT_P := "ocamlopt -inline 1000"
else
  CAMLOPT_P := "ocamlopt -inline 10000"
endif

LIBDIR=$(CURDIR)/debian/cduce/$(OCAML_STDLIB_DIR)
DOCDIR=$(CURDIR)/debian/cduce/usr/share/doc/cduce/html
BINDIR=$(CURDIR)/debian/cduce/usr/bin
MANDIR=$(CURDIR)/debian/cduce/usr/share/doc/man

CONFIGURE_OPTS := --prefix=/usr \
  --mliface=debian/ocaml-$(OCAML_ABI) \
  --with-curl \
  --without-netclient \
  --with-expat \
  --without-pxp

%:
	dh --with ocaml $@

.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	tar -C debian -xf /usr/src/ocaml-source-$(OCAML_ABI).tar
	tar -C debian -xf debian/ocaml-$(OCAML_ABI)/ocaml_*.orig.tar.*
	./configure  $(CONFIGURE_OPTS)

.PHONY: override_dh_auto_build
override_dh_auto_build:
	$(MAKE) CAMLOPT_P=$(CAMLOPT_P) $(MAKE_OPTS) all doc

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	touch Makefile.conf
	[ ! -f Makefile ] || $(MAKE) $(MAKE_OPTS) clean
	-$(RM) Makefile.conf
	-$(RM) -rf debian/ocaml-$(OCAML_ABI)/

.PHONY: override_dh_auto_install
override_dh_auto_install:
	$(MAKE) $(MAKE_OPTS) DOCDIR=$(DOCDIR) BINDIR=$(BINDIR) MANDIR=$(MANDIR) OCAMLFIND_DESTDIR=$(LIBDIR) OCAMLFIND_LDCONF=ignore install

	# Remove the wrong RPATH
	chrpath -d $(BINDIR)/cduce

	# CDuce Makefile installs all the documentation in DOCDIR, including
	# README, LICENSE, and so on. We don't want these. Let's hack.
	find $(DOCDIR) -maxdepth 1 -not -name "*.html" -not -name "img" -exec rm {} \;
	-$(RM) -rf $(MANDIR)

.PHONY: override_dh_ocaml
override_dh_ocaml:
	dh_ocaml --runtime-map cduce

# Tests suppose the OCaml library is installed
.PHONY: override_dh_auto_test
override_dh_auto_test:
