#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export VERBOSE=1

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

include /usr/share/dpkg/pkg-info.mk
VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | sed 's|\(^[0-9\.]*\).*|\1|')

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

# 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)
# needed for remove-build-paths-from-cached-files.patch
	cd M2/Macaulay2 && ln -s m2 Core

# 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:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd M2 && make -j4 check VERBOSE=1
endif

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

# example output files must be plain text
override_dh_compress:
	dh_compress -Xexample-output

override_dh_elpa: debian/macaulay2-pkg.el
	dh_elpa

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