#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk # for SOURCE_DATE_EPOCH

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

# needed for reproducible-version.patch
export CURDIR

VERSION := $(shell cat M2/VERSION)

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

# use exec_prefix instead of prefix in libdir, as that's what's needed
# to properly substitute paths in Layout#2, and set emacs lisp file
# installation directory so that the documentation matches where dh_elpa
# installs them
EMACSDIR := \
	\$${prefix}/share/emacs/site-lisp/elpa/macaulay2-$(VERSION)
override_dh_auto_configure:
	dh_auto_configure -- \
		--libdir=\$${exec_prefix}/lib/$(DEB_HOST_MULTIARCH) \
		--with-emacsdir=$(EMACSDIR) \
		--with-python

# 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)

$(KATEX_DIR)/%:
	touch $@

# copy cached example output files (for examples known to fail on
# various architectures) to source directory
# see debian/scripts/generate-examples.m2
EXAMPLE_SRC := $(wildcard $(CURDIR)/debian/examples/*)
EXAMPLE_DEST := $(patsubst $(CURDIR)/debian/examples/%, \
	$(CURDIR)/M2/Macaulay2/packages/%/examples, $(EXAMPLE_SRC))

$(CURDIR)/M2/Macaulay2/packages/%/examples: $(CURDIR)/debian/examples/%
	mkdir -p $@
	cp -v $</*.out $@

execute_before_dh_auto_build-indep: $(KATEX_FILES) $(EXAMPLE_DEST)

ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
MakeDocumentation = false
else
MakeDocumentation := $(if $(shell dh_listpackages -i),true,false)
endif

override_dh_auto_build:
	dh_auto_build -- MakeDocumentation=$(MakeDocumentation)

# 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:
	$(MAKE) -C M2 check

override_dh_auto_test-indep:
# uncomment after #994765 is fixed
#	$(MAKE) -C M2 validate-html

# create dumps of package documentation databases to ship with macaulay2-common
# and then load during postinst
PKGLIBDIR := debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/Macaulay2
PKGDOCDIR := debian/tmp/usr/share/doc/Macaulay2
DB_FILENAME := rawdocumentation-$(if \
	$(filter little, $(DEB_HOST_ARCH_ENDIAN)),dcba,abcd)-$(if \
	$(filter 64, $(DEB_HOST_ARCH_BITS)),8,4).db

DB_FILES = $(wildcard $(PKGLIBDIR)/*/cache/$(DB_FILENAME))
DUMP_FILES = $(DB_FILES:$(PKGLIBDIR)/%/cache/$(DB_FILENAME)=\
	$(PKGDOCDIR)/%/dump/rawdocumentation.dump)

$(PKGDOCDIR)/%/dump/rawdocumentation.dump: $(PKGLIBDIR)/%/cache/$(DB_FILENAME)
	mkdir -p $(dir $@)
# gdbm_load records the path of the database file and the timestamp in the
# dump file, so use a relative path and faketime for reproducibility
	cd $(dir $<) && \
		faketime '@$(SOURCE_DATE_EPOCH)' \
		gdbm_dump $(notdir $<) $(CURDIR)/$@

execute_after_dh_auto_install-indep: $(DUMP_FILES)
	rm -rf $(EXAMPLE_DEST)
	for DIR in $(subst /examples,,$(EXAMPLE_DEST)); \
	do if [ -z "$$(ls -A $$DIR)" ]; then rm -rf $$DIR; fi; \
	done
	rm -rf $(PKGLIBDIR) # remove package documentation database files

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

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

execute_before_dh_elpa: debian/macaulay2-pkg.el

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

# prevent dh_missing from failing when only building arch-dependent packages
execute_before_dh_missing:
	if [ ! -f debian/macaulay2-pkg.el ]; \
	then \
		rm -rf debian/tmp/usr/share/emacs; \
	fi

override_dh_installdeb:
	dh_installdeb --define DEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		--define DB_FILENAME=$(DB_FILENAME)

# 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
