#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Show full commands when building Coq
export VERBOSE=1

include /usr/share/ocaml/ocamlvars.mk

COQ_VERSION := 8.14.0
COQ_ABI := $(COQ_VERSION)+$(OCAML_ABI)

ARCH := $(shell dpkg-architecture -q DEB_TARGET_ARCH)
ifeq ($(ARCH),$(filter $(ARCH),amd64 i386))
NATIVE_COMPUTE :=
else
NATIVE_COMPUTE := -native-compiler no
endif
ifeq ($(ARCH),s390x)
BUILD_VM := -bytecode-compiler no
else
BUILD_VM :=
endif

PACKAGES := $(shell dh_listpackages)

CONFIGUREOPTS := -arch Linux -prefix /usr -mandir /usr/share/man \
  -configdir /etc/xdg/coq \
  -browser "/usr/bin/x-www-browser %s &" \
  -with-doc no \
  $(NATIVE_COMPUTE) \
  $(BUILD_VM)

export OCAMLINIT_SED += \
  -e 's%@CoqVersion@%$(COQ_VERSION)%' \
  -e 's%@CoqABI@%$(COQ_ABI)%'

%:
	+dh $@ --with ocaml,python3

build:
	+dh $@ --with ocaml,python3

override_dh_auto_configure:
	./configure $(CONFIGUREOPTS)

override_dh_auto_build:
# Don't combine `make world` and `make byte`--doing so triggers a race
# in the build system. See upstream's CHANGES.
	$(MAKE) world STRIP=true
	$(MAKE) byte STRIP=true
	$(MAKE) doc-stdlib-html HTMLSTYLE=simple

# Check that $(COQ_VERSION) has the right value
	ACTUAL_COQ_VERSION="$$(./bin/coqc --version | awk '/version/{print $$6}')"; \
	if [ "$$ACTUAL_COQ_VERSION" != "$(COQ_VERSION)" ]; then \
	  echo "Please set COQ_VERSION to $$ACTUAL_COQ_VERSION in debian/rules"; \
	  exit 2; \
	fi

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	# OCAMLPATH=$(CURDIR)/_build_vo/default/lib make -C test-suite COQBIN=$(CURDIR)/_build_vo/default/bin/ COQLIB=$(CURDIR)/_build_vo/default/lib/coq
	@echo "TESTS ARE DISABLED FOR NOW!"
endif

override_dh_auto_install:
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install install-byte install-doc-stdlib-html
	# cleaning what upstream installs and we don't (need to) ship
	find $(CURDIR)/debian/tmp -name LICENSE -delete
	rm -rf $(CURDIR)/debian/tmp/usr/doc/coqide/odoc-pages \
		$(CURDIR)/debian/tmp/usr/lib/coqide
	rm -rf $(CURDIR)/debian/tmp/usr/doc/coqide-server/ \
		$(CURDIR)/debian/tmp/usr/lib/coqide-server/
	# ok, now we detect what to ship
	mkdir -p $(CURDIR)/debian/tmp/usr/share/pixmaps
	cp debian/coq.xpm debian/tmp/usr/share/pixmaps/coqide.xpm
	find debian/tmp -regextype posix-awk \
	  -regex '.*\.(cma|cmxs)$$' \
	  >> debian/libcoq-ocaml.install
	find debian/tmp -regextype posix-awk \
	  -regex '.*\.(a|cmi|cmo|cmt|cmti|cmx|cmxa|ml|mli|o])$$' \
	  >> debian/libcoq-ocaml-dev.install
	find debian/tmp -regextype posix-awk \
	  -regex '.*\.(v|vo|vos|glob)$$' \
	  >> debian/coq-theories.install

# to make dh-exec work
override_dh_install:
	chmod +x debian/coq.install
	dh_install

override_dh_gencontrol:
	for u in $(PACKAGES); do \
	  echo 'F:OCamlABI=$(OCAML_ABI)' >> debian/$$u.substvars; \
	  echo 'F:CoqABI=$(COQ_ABI)' >> debian/$$u.substvars; \
	done
	dh_gencontrol

# why is this necessary?
override_dh_fixperms:
	dh_fixperms
	find $(CURDIR)/debian -name TimeFileMaker.py -exec chmod -x '{}' \;
