#!/usr/bin/make -f

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

include /usr/share/ocaml/ocamlvars.mk

COCCI_FLAGS = BYTECODE_STATIC="" MANDIR=/usr/share/man

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

COMMON_CONFIG = --prefix=/usr --with-python --without-trac

ifeq ($(OCAML_HAVE_OCAMLOPT),yes)
CONFIG_FLAGS = $(COMMON_CONFIG) --opt
MAKE_TGT = all.opt
else
CONFIG_FLAGS = $(COMMON_CONFIG) --no-opt
MAKE_TGT = all
endif

build clean binary binary-arch binary-indep install:
	dh --with ocaml $@

override_dh_auto_configure:
	./configure $(CONFIG_FLAGS)

override_dh_auto_build:
	$(MAKE) $(COCCI_FLAGS) depend
	# make all.opt does not build documentation
	$(MAKE) $(COCCI_FLAGS) -C docs all
	$(MAKE) $(COCCI_FLAGS) $(MAKE_TGT)

override_dh_auto_test:
	# Nothing

override_dh_auto_install:
	$(MAKE) DESTDIR=$(CURDIR)/debian/coccinelle $(COCCI_FLAGS) 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_clean:
	$(MAKE) CLEANSUBDIRS=\$$\(MAKESUBDIRS\) distclean

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

.PHONY: build clean binary-indep binary-arch binary install
.PHONY: override_dh_auto_configure override_dh_auto_build override_dh_auto_test override_dh_auto_install
