#!/usr/bin/make -f

include /usr/share/ocaml/ocamlvars.mk

export DESTDIR := $(CURDIR)/debian/tmp
export OCAMLFIND_DESTDIR := $(DESTDIR)$(OCAML_STDLIB_DIR)
export OCAML_HAVE_OCAMLOPT

ifeq ($(OCAML_HAVE_OCAMLOPT),yes)
	INDENTMAIN = src/indentMain.native
else
	INDENTMAIN = src/indentMain.byte
endif

%:
	dh $@ --with ocaml

override_dh_auto_clean:
	rm -f config.status config.log Makefile.config version.ocp
	rm -f $(INDENTMAIN) ocp-indent.1 src/approx_lexer.mli src/util.mli
	rm -f src/indentVersion.ml src/approx_common.ml src/compat.ml
	rm -rf META _build

override_dh_auto_build:
	# build library
	dh_auto_build -- -f Makefile.simple
	# build ocp-indent program
	ocamlbuild -use-ocamlfind -package findlib -package dynlink -package cmdliner $(INDENTMAIN)
	# build man page
	_build/$(INDENTMAIN) --help=groff > ocp-indent.1
	# create the mli for util.ml and approx_lexer.mll
	ocamlc -i src/util.ml > src/util.mli
	ocamlc -I _build/src -i _build/src/approx_lexer.ml > src/approx_lexer.mli

# skip because requires source directory to be git repository
override_dh_auto_test:

override_dh_install:
	dh_install --fail-missing

override_dh_auto_install:
	mkdir -p $(OCAMLFIND_DESTDIR)
	mkdir -p $(DESTDIR)/usr/bin
	cp _build/$(INDENTMAIN) $(DESTDIR)/usr/bin/ocp-indent
	mkdir -p $(DESTDIR)/usr/share/emacs/site-lisp
	cp tools/ocp-indent.el $(DESTDIR)/usr/share/emacs/site-lisp/ocp-indent.el
	mkdir -p $(DESTDIR)/usr/share/vim/addons/syntax
	cp tools/ocp-indent.vim $(DESTDIR)/usr/share/vim/addons/syntax/ocp-indent.vim
	dh_auto_install -- -f Makefile.simple
