#!/usr/bin/make -f

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

include /usr/share/ocaml/ocamlvars.mk

VIM_ADIR = $(CURDIR)/debian/coccinelle/usr/share/vim/addons
VIM_RDIR = $(CURDIR)/debian/coccinelle/usr/share/vim/registry

CONFIG_FLAGS = --prefix=/usr --sysconfdir=/etc --enable-ocaml

ifeq ($(OCAML_NATDYNLINK),yes)
CONFIG_FLAGS += --enable-release
endif

%:
	dh $@ --with ocaml,python2 --parallel

override_dh_auto_configure:
	./configure $(CONFIG_FLAGS)

override_dh_auto_build-arch:
	$(MAKE) depend
	# make all.opt does not build documentation
	$(MAKE) -C docs all
	# Re-create this directory here because git does not store empty directories
	mkdir -p ocaml/coccilib
	dh_auto_build

override_dh_auto_build-indep:
	mkdir texmf-var
	mkdir texmf-config
	TEXMFVAR="$(CURDIR)/texmf-var" TEXMFCONFIG="$(CURDIR)/texmf-config" $(MAKE) -C docs/manual all-pdf

override_dh_auto_test:
	$(MAKE) check

override_dh_auto_install-arch:
	$(MAKE) DESTDIR=$(CURDIR)/debian/coccinelle install install-bash
	# install the wrapper script, i dont like upstreams one
	rm $(CURDIR)/debian/coccinelle/usr/bin/spatch*
	install $(CURDIR)/debian/spatch $(CURDIR)/debian/coccinelle/usr/bin/
	# move binary into /usr/lib/coccinelle
	mv $(CURDIR)/debian/coccinelle/usr/share/coccinelle/spatch* \
		$(CURDIR)/debian/coccinelle/usr/lib/coccinelle/spatch

	# Vim files
	install -d $(VIM_ADIR)/ftdetect $(VIM_ADIR)/syntax
	cp $(CURDIR)/editors/vim/ftdetect/cocci.vim $(VIM_ADIR)/ftdetect/
	cp $(CURDIR)/editors/vim/syntax/cocci.vim $(VIM_ADIR)/syntax/
	install -d $(VIM_RDIR)
	cp $(CURDIR)/debian/coccinelle.yaml $(VIM_RDIR)/

	# Emacs files
	cp $(CURDIR)/editors/emacs/cocci.el $(CURDIR)/debian/coccinelle/usr/share/emacs/site-lisp/

override_dh_auto_install-indep:
	# Nothing

override_dh_auto_clean:
	$(MAKE) distclean
	rm -rf myocamlbuild.ml python/.depend texmf-var texmf-config

override_dh_compress:
	dh_compress -X.pdf -X.cocci -X.c
