#!/usr/bin/make -f

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

%:
	dh "$@" --with autoreconf

# This helps (makes some tests pass on big endian architectures)
# but it's not enough, there are still segfaults to fix.
# See http://xcas.univ-grenoble-alpes.fr/forum/viewtopic.php?f=3&t=1874
ifeq ($(DEB_HOST_ARCH_ENDIAN),big)
export DEB_CPPFLAGS_MAINT_APPEND  = -DDOUBLEVAL -DBIGENDIAN
endif

# Use ccache if pkg.giac.ccache build-profile is active.
# Your rebuilds will be *much quicker*.
ifneq (,$(filter pkg.giac.ccache,$(DEB_BUILD_PROFILES)))
export PATH := /usr/lib/ccache:$(PATH)
export CCACHE_DIR := $(CURDIR)/debian/ccache
#export CCACHE_BASEDIR := $(CURDIR)/debian/build
$(shell mkdir -p "$(CCACHE_DIR)")
endif

# ~/.xcasrc sometimes interferes with doc/test targets, causing failures
export HOME=/nonexistent

override_dh_autoreconf:
# Deal with embedded stuff
	find doc/ -maxdepth 1 -type d -exec ln -sf /usr/share/texmf/tex/texinfo/texinfo.tex '{}/' \;
# Deal with emscripten placeholder
	cp debian/giac.js doc/giac.js
# Deal with french documentation
	cp -a debian/upstream-doc-fr doc/fr
	cp doc/fr/Makefile.am.Debian doc/fr/Makefile.am
# Actually configure
	dh_autoreconf

override_dh_auto_build-arch:
# Should be the same order as SUBDIRS in Makefile.am
	dh_auto_build -a -D src
	dh_auto_build -a -D intl
	dh_auto_build -a -D po
	dh_auto_build -a -D doc -- info

override_dh_auto_test-arch:
	dh_auto_test -a -D check -- VERBOSE=1

override_dh_auto_test-indep:
	true

override_dh_auto_install-arch:
# Should be the same order as SUBDIRS in Makefile.am
	dh_auto_install -a -D src
	dh_auto_install -a -D intl
	dh_auto_install -a -D po
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp AM_UPDATE_INFO_DIR=no -C doc \
	  install-info install-dist_pkgdataDATA
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install-exec-hook

override_dh_install:
	rm -f debian/tmp/usr/lib/*/libgiac.la
	rm -f debian/tmp/usr/include/giac/config.h
	dh_install

override_dh_missing:
	dh_missing --fail-missing

override_dh_fixperms-indep:
	dh_fixperms
	find debian/giac-doc/usr/share/giac/examples/ \
	  \( -name '*.cas' -o -name '*.xws' -o -name '*.cxx' \) -exec chmod -x '{}' \;
	chmod -x debian/giac-doc/usr/share/giac/examples/lewisw/*_?var
