#!/usr/bin/make -f
# -*- makefile -*-

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

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

package=emacspeak
debbase := $(shell pwd)/debian
backup  := $(debbase)/backup
pkgtmp  := $(debbase)/$(package)
eldir   := usr/share/emacs/site-lisp/$(package)
deblsp  := $(pkgtmp)/$(eldir)
tmpdir  := $(debbase)/tmp/$(eldir)

%:
	dh $@

override_dh_auto_build-arch:
	dh_auto_build --sourcedirectory=servers/native-espeak -- TCL_VERSION=""

override_dh_auto_build-indep:
	echo $(DEB_VERSION) > debian_version

# Upstream doesn't ship a changelog file but NEWS-* basically contain what is needed
# so cat that all together. Not 100% elegant, but it will do for now.
	cat etc/NEWS-8.0 > changelog
	cat etc/NEWS-9.0 >> changelog
	cat etc/NEWS-[0-9][0-9]* >> changelog
	cat etc/NEWS >> changelog

	# The 3d auditory icon theme was generated using CSound, but it expects the
	# reference files in the same directory.
	ln -s /usr/share/csound/hrtf/*.dat sounds/3d/src/
	cd sounds/3d/src ; \
	for file in *.csd ; do \
		if [ ! "$$file" = interactive.csd ] ; then \
			csound -o ../$${file%csd}wav $$file ; \
		fi ; \
	done

	$(MAKE) --directory=etc tips.html applications.html

# Do not put a default load file when require with no-error.
# Calling require with the second argument non nil could lead Emacs to load the
# file without an extension, but some other package could put in the load path
# a file with such a name. Using nil as second argument let Emacs search for
# file to load in the normal way.
# Rémi Vanicat <vanicat@debian.org>
# https://bugs.debian.org/913025
	if [ ! -d $(backup) ] ; then \
		mkdir $(backup) ; \
	fi
	for file in $$(rgrep  -l "\" 'no-error" lisp/*) ; do \
		cp $$file $(backup)/ ; \
		sed -i "s/\"[^ ]*\" 'no-error/() 'no-error/" $$file ; \
	done

override_dh_auto_clean:
	debconf-updatepo

	$(MAKE) clean
	$(MAKE) --directory servers/native-espeak clean

# the following items should be done by the get-orig-source target, but
# leave them here just in case we might not need that target in the future
# the sources included some byte-compiled file by mistake
	find . -name \*.elc -delete
	find . -name \*.pyc -delete
	find . -name \*.pyo -delete

# removed created symlinks
	find sounds/3d/src -type l -name \*.dat -delete

# restore patched files
	if [ -d $(backup) ] ; then \
		for file in $(backup)/* ; do \
			mv $$file lisp/ ; \
		done ; \
	fi

	dh_clean

override_dh_auto_install-indep:
	dh_auto_install

	# Upstream stopped providing an install target. Instead it recommends to
	# "copy the resulting emacspeak directory to the install location". So,
	# let's do that, but prevent the debian sub dir to be copied, as I fear
	# that would lead to some level of recursive copying.
	#
	# Also strip out personal tvr stuff.
	#
	# For now, also don't install the bash-utils, as it requires some thinking
	# and tweaking for Debian to function properly.
	#
	# And, to avoid clutter from etc/, lets do that via emacspeak.install
	mkdir -p $(tmpdir)
	cp -pR $$(find . -maxdepth 1 -type d ! -name .\* ! -name bash-utils ! -name debian \
		! -name etc ! -name tvr -o -type f) $(tmpdir)

	# Obviously, now we need to remove the stuff we don't want here.
	find $(tmpdir) -name \*~ -delete
	find $(tmpdir) -name \*.elc -delete
	find $(tmpdir) -depth -name .\* -exec rm -r {} \;
	# Doesn't seem to make sense in a Debian package where we use emacspeak.sh
	# instead
	rm $(tmpdir)/run
	rm $(tmpdir)/runq
	# Already compiled and shipped in emacspeak-espeak-server
	rm -r $(tmpdir)/servers/native-espeak
	# Should be made into a proper package
	rm -r $(tmpdir)/servers/linux-outloud
	# Files for Mac, not for Debian
	rm -r $(tmpdir)/servers/mac
	rm -r $(tmpdir)/servers/log-mac
	# Source files
	rm -r $(tmpdir)/sounds/3d/src
	# Original convertion script, but useless now
	rm $(tmpdir)/sounds/pan-chimes/apply-pan.sh
	# Let it generated at configuration
	rm $(tmpdir)/lisp/emacspeak-loaddefs.el

override_dh_auto_install-arch:

override_dh_install-indep:
	# Exclude the files that go into the documentation
	dh_install -Xdtk/DTK -Xetc/NEWS -XREADME -X.html -Xremote.txt -Xstumpwm
	# We will sed it at configuration
	mv debian/emacspeak/$(eldir)/etc/emacspeak.sh debian/emacspeak/$(eldir)/etc/emacspeak.sh.in

override_dh_fixperms-indep:
# Only fix the following in the indep case
# scripts and speech servers should be executable
	if [ -d $(deblsp) ] ; then \
		chmod 644 `find $(deblsp) -type f`; \
		chmod 755 $(deblsp)/etc/*.pl				\
			$(deblsp)/etc/*2text				\
			$(deblsp)/etc/emacspeak.sh.in          \
			$(deblsp)/servers/cloud*			\
			$(deblsp)/servers/dtk-exp			\
			$(deblsp)/servers/espeak			\
			$(deblsp)/servers/log-*			\
			$(deblsp)/servers/outloud			\
			$(deblsp)/servers/speech-server			\
			$(deblsp)/servers/ssh-dtk-exp			\
			$(deblsp)/servers/ssh-espeak			\
			$(deblsp)/servers/ssh-outloud ;			\
	fi

	dh_fixperms

override_dh_compress:
# NEWS can be called from emacspeak command, don't compress
	dh_compress -XNEWS

override_dh_auto_test-indep:
	dh_auto_test
	# To see if compiling the code works and we are not missing dependencies
	$(MAKE)
