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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/ocaml/ocamlvars.mk

DESTDIR := $(CURDIR)/debian/tmp
export OCAMLOBJINFO := ocamlduceobjinfo

ifeq ($(OCAML_HAVE_OCAMLOPT),yes)
  OPT_CONFFLAGS := --enable-nativecode
else
  OPT_CONFFLAGS := --disable-nativecode
endif

INSTALL_TARGET := $(if $(findstring libocsigen-ocaml-doc,$(shell dh_listpackages)),install,installnodoc)

%:
	dh --with ocaml $@


.PHONY: override_dh_auto_configure
override_dh_auto_configure:
	chmod +x configure
	./configure \
	    --temproot $(DESTDIR) \
	    --prefix /usr \
	    --bindir /usr/bin \
	    --libdir $(OCAML_STDLIB_DIR) \
	    --mandir /usr/share/man/man1 \
	    --docdir /usr/share/doc \
	    --staticpagesdir /usr/share \
	    --ocsigen-user ocsigen \
	    --ocsigen-group ocsigen \
	    --stubdir $(OCAML_DLL_DIR) \
	    $(OPT_CONFFLAGS) \
	    --enable-ocamlduce
	cp -a examples debian
# Remove irrelevant files (incl. those installed in /usr/share or /var/lib)
	mv debian/examples/miniwiki/files/miniwiki.conf debian/examples/miniwiki
	rm -Rf \
	  debian/examples/miniwiki/wikidata \
	  debian/examples/miniwiki/files \
	  debian/examples/Makefile


.PHONY: override_dh_auto_install
override_dh_auto_install:
	$(MAKE) $(INSTALL_TARGET) CHOWN=true STD_METAS_DIR=$(OCAML_STDLIB_DIR)/METAS

# For each installed .cmi file, install the corresponding .mli file next to it
	find $(DESTDIR) -name '*.cmi' -printf '%h %f\n' | \
	  while read d f; do \
	    find . -name $${f%.cmi}.mli -exec cp '{}' $$d ';' -quit; \
	  done

	$(MAKE) logrotate
	rm -f $(DESTDIR)/var/run/ocsigen_command
	rm -f $(DESTDIR)/usr/share/ocsigen/ocsigenstuff/LICENSE


.PHONY: override_dh_auto_build
override_dh_auto_build:
# This directory, present in .orig.tar.gz, is created here because
# git doesn't handle empty directories
	mkdir -p doc
	$(MAKE) depend
	$(MAKE) all


.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	[ ! -f Makefile.config ] || $(MAKE) distclean
	rm -Rf debian/examples xmlp4/newocaml/pp
	mkdir xmlp4/newocaml/pp


.PHONY: override_dh_auto_clean
override_dh_compress:
	dh_compress -X.ml


.PHONY: override_dh_install
override_dh_install:
	dh_install --list-missing


.PHONY: override_dh_ocaml
override_dh_ocaml:
	dh_ocaml \
	  --nodefined-map=libocsigen-ocaml-dev:Xmllexer \
	  --nodefined-map=libocsigen-xhtml-ocaml-dev:Xmllexer
