#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
export VERBOSE=1
endif

# needed for reproducible-config-args.patch
export CURDIR

%:
	dh $@ --sourcedirectory=M2 --with elpa,linktree

# use exec_prefix instead of prefix in libdir, as that's what's needed
# to properly substitute paths in Layout#2,
override_dh_auto_configure:
	dh_auto_configure -- \
		--libdir=\$${exec_prefix}/lib/$(DEB_HOST_MULTIARCH)

# prevent html-check-links from failing to find KaTeX files before
# they get added by dh_linktree
KATEX_DIR := $(CURDIR)/M2/Macaulay2/packages/Style/katex
KATEX_FILES := $(patsubst %, $(KATEX_DIR)/%, \
	katex.min.css katex.min.js contrib/auto-render.min.js \
	contrib/copy-tex.min.css contrib/copy-tex.min.js)

execute_before_dh_auto_build: $(KATEX_FILES)

$(KATEX_DIR)/%:
	touch $@

# since a "test" target exists, dh_auto_test defaults to using that, but
# it just prints the values of some variables.  we want the "check" target
# instead to run the test suite
# related: https://bugs.debian.org/924052
override_dh_auto_test-arch:
	cd M2 && make check

override_dh_auto_test-indep:
	cd M2 && make validate-html

# remove empty directory
execute_after_dh_install-arch:
	rm -rf debian/macaulay2/usr/lib/$(DEB_HOST_MULTIARCH)/Macaulay2/lib

# https://github.com/Macaulay2/M2/issues/1144
execute_before_dh_link-indep:
	jdupes -rl debian/macaulay2-common/usr/share/doc/Macaulay2

# example output and package test files must be plain text
override_dh_compress:
	dh_compress -Xexample-output -X.m2

execute_before_dh_elpa: debian/macaulay2-pkg.el

debian/macaulay2-pkg.el: debian/macaulay2-pkg.el.in
	sed -e 's|@VERSION@|$(shell cat M2/VERSION)|g' $< > $@

# we use uscan to check for the most recent version, but to create an
# orig tarball, we also need the contents of the M2-emacs submodule,
# so use this target instead of uscan directly
get-orig-source:
	debian/scripts/m2-get-orig-source.sh
