#!/usr/bin/make -f

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

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Passing --no-parallel because of some creepy race conditions, like
# on building x11-compat which can get built twice and if the second
# build happens at just the wrong time it interferes with the first
# build or with an invocation of the generated executable.

%:
	dh $@ --no-parallel

override_dh_autoreconf:
	dh_autoreconf sh -- -e -x -c \
	  'for d in src doc; \
	    do (cd $$d && autoreconf -f -i) done'

CONF_FLAGS += --docdir=/usr/share/doc/mit-scheme-doc
CONF_FLAGS += --enable-html=/usr/share/doc/mit-scheme-doc/html
CONF_FLAGS += --enable-pdf=/usr/share/doc/mit-scheme-doc/pdf
CONF_FLAGS += --enable-ps=no

# According to https://savannah.gnu.org/support/?110264 a build error like
#
#  Bad compiled-code version in FASL File: make.com
#  File has: compiled-code interface 3; architecture 14.
#  Expected: compiled-code interface 4; architecture 14.
#
# can occur when, e.g., 10.1.11 is installed but 11.1 is being built.
# And can be prevented using a configuration flag.

INSTALLED_MAJOR_VERSION:=$(shell dpkg --status mit-scheme | egrep '^Version:' | sed 's/^Version: //' | cut --delimiter=. --fields=1)
BUILDING_MAJOR_VERSION:=$(shell echo $(DEB_VERSION_UPSTREAM) | cut --delimiter=. --fields=1)
ifneq ($(INSTALLED_MAJOR_VERSION),$(BUILDING_MAJOR_VERSION))
CONF_FLAGS += --enable-cross-compiling
endif

override_dh_auto_configure-arch:
	dh_auto_configure --sourcedirectory=src -- $(CONF_FLAGS)

override_dh_auto_configure-indep:
	dh_auto_configure --sourcedirectory=doc -- $(CONF_FLAGS)

# May wish to pass parameter in dh_auto_build
#  SCHEME_COMPILER=mit-scheme

override_dh_auto_build-arch:
	dh_auto_build --sourcedirectory=src

override_dh_auto_build-indep:
	dh_auto_build --sourcedirectory=doc

ifeq ($(DEB_HOST_GNU_CPU),x86_64)
export BIN_ARCH=x86-64
else
export BIN_ARCH=$(DEB_HOST_GNU_CPU)
endif

override_dh_auto_install-arch:
	dh_auto_install --sourcedirectory=src

execute_after_dh_auto_install-arch:
#	# This doesn't work because doc/ hasn't been *configured*
#	# which hardly seems worth working around
#	dh_auto_install --sourcedirectory=doc -- install-man
	@echo "remove symbolic link executables"
	cd debian/tmp/usr/bin && \
	for f in scheme bchscheme mit-scheme mit-scheme-native; do \
	  if [ -L $$f ]; then \
	    rm --verbose -f $$f; \
	  else \
	    echo skipping $$f; \
	  fi; \
	done
#	@echo "delete runtime.com for MYSTERIOUS reasons"
#	@echo "stop doing this because it breaks future builds"
#	find debian/tmp/usr/lib -name runtime.com -ls -delete
	@echo "remove .la library support files as these are not actually used by the dynamic linker"
	find debian/tmp -name '*.la' -ls -delete

override_dh_auto_install-indep:
	dh_auto_install --sourcedirectory=doc -- install-html install-pdf

execute_after_dh_auto_install-indep:
	@echo "allow dh scripts to compress info files themselves"
	find debian/tmp \( -name '*.info.gz' -o -name '*.info-*.gz' \) -ls -exec gunzip '{}' ';'

execute_after_dh_install-arch:
	@echo "All .bci files belong in -dbg, move them manually for robustness"
	mkdir -p debian/mit-scheme-dbg
	tar -C debian/mit-scheme --create --file=- --files-from=/dev/null $$(cd debian/mit-scheme && find -name '*.bci') \
	 | tar -C debian/mit-scheme-dbg -xvf -
	find debian/mit-scheme -name '*.bci' -delete
	@echo "remove empty directory"
	-rmdir debian/mit-scheme/usr/lib/x86_64-linux-gnu/mit-scheme/lib

# Upstream changelog is over 5MB, leave a copy in the doc package but
# remove it from the binary package to reduce bloat.
# See http://dedup.debian.net/.
override_dh_installchangelogs-arch:
	dh_installchangelogs
	-rm --verbose \
	   debian/mit-scheme/usr/share/doc/mit-scheme/changelog \
	   debian/mit-scheme-dbg/usr/share/doc/mit-scheme-dbg/changelog

override_dh_auto_clean:
	-dh_auto_clean
