#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh "$@" --with autoreconf

# 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

include /usr/share/dpkg/architecture.mk

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

execute_before_dh_auto_clean:
# Remove Makefile if it exists because it contains wrong hard-coded paths
	if [ -f ./Makefile ]; then\
	    rm -f ./Makefile;\
	fi
# Remove configure generated files
	if [ -f ./config.log ]; then\
	    rm -f ./config.log;\
	fi
	if [ -f ./config.status ]; then\
	    rm -f ./config.status;\
	fi

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
# Actually configure
	dh_autoreconf

override_dh_auto_build-arch:
# Should be the same order as SUBDIRS in Makefile.am
	dh_auto_build -a -D micropython-1.12
	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: fix_encoding
	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

ENCODING_FILES = \
	/usr/share/giac/doc/en/casinter/casinter001.html \
	/usr/share/giac/doc/en/casinter/casinter002.html \
	/usr/share/giac/doc/en/casinter/casinter004.html \
	/usr/share/giac/doc/en/casinter/casinter005.html \
	/usr/share/giac/doc/en/casinter/casinter006.html \
	/usr/share/giac/doc/en/casinter/index.html \
	/usr/share/giac/examples/arit/codage.cas \
	/usr/share/giac/examples/demo/e05.tex \
	/usr/share/giac/examples/geo/geo2 \
	/usr/include/giac/monomial.h \
	/usr/include/giac/poly.h \
	/usr/include/giac/prog.h

fix_encoding:
	for toto in $(ENCODING_FILES); do \
		iconv -f iso-8859-15 -t utf-8 debian/tmp$$toto > debian/tmp$$toto.fix ; \
		mv debian/tmp$$toto.fix debian/tmp$$toto ; \
	done
