#!/usr/bin/make -f

include /usr/share/coq/coqvars.mk
include /usr/share/ocaml/ocamlvars.mk

export COQTOP := $(COQ_STDLIB_DIR)
export COQLIB := $(COQ_STDLIB_DIR)
export COQBIN := /usr/bin/

PACKAGES := $(shell dh_listpackages)

INSTALL_DIR := $(CURDIR)/debian/tmp/usr/lib/coq/user-contrib/mathcomp

# The following must be kept in sync with d/libssreflect-ocaml*.install.in
PLUGIN_TARGETS := ssreflect.cma
ifeq ($(OCAML_NATDYNLINK),yes)
  PLUGIN_TARGETS += ssreflect.cmxs
endif
EXTRA_FILES := ssrmatching.cmi ssreflect.cmi

%:
	+dh $@ --with ocaml

.PHONY: override_dh_auto_build
override_dh_auto_build:

# Building is done here because dh_listpackages is useless in the
# build target. Should be reconsidered when dpkg-buildpackage starts
# calling build-{arch,indep} targets (see #604397)
.PHONY: override_dh_auto_install
override_dh_auto_install:
ifeq ($(findstring libssreflect-coq,$(PACKAGES)),)
# Express build: only plugins
	$(MAKE) -C mathcomp Makefile.coq
	$(MAKE) -C mathcomp -f Makefile.coq $(PLUGIN_TARGETS)
	install -d $(INSTALL_DIR)
	cd mathcomp; \
		install -m 644 $(PLUGIN_TARGETS) $(EXTRA_FILES) $(INSTALL_DIR)
else
# Full build
	$(MAKE) -C mathcomp
	$(MAKE) -C mathcomp install DSTROOT=$(CURDIR)/debian/tmp
	mkdir -p $(CURDIR)/debian/tmp/usr/share/doc/libssreflect-coq/html
	cp -r htmldoc $(CURDIR)/debian/tmp/html
endif

.PHONY: override_dh_install
override_dh_install:
	dh_install --fail-missing

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	dh_auto_clean
	rm -Rf bin

.PHONY: override_dh_clean
override_dh_clean:
	dh_clean
	rm -Rf html

.PHONY: override_dh_gencontrol
override_dh_gencontrol:
	dh_gencontrol -- -VF:CoqABI="$(COQ_ABI)"
