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

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

package=emacspeak
debbase         := $(shell pwd)/debian
debtmp          := $(debbase)/$(package)
eldir		:= usr/share/emacs/site-lisp/$(package)
deblsp          := $(debtmp)/$(eldir)
debOrgFiles	:= $(patsubst %.debian_org,%,$(wildcard servers/*.debian_org))
restoreBackup	= mv $(file).debian_org $(file) ;

%:
	dh  $@

override_dh_auto_build-arch:
	cd servers/linux-espeak ; $(MAKE)

override_dh_auto_build-indep:
# As we depend on tcl8.4-dev for the binary, we depend and check only specific tclsh8.4
# and we should make sure the scripts are explicitely working for them
	cd servers ; 								\
	for file in dtk-exp dtk-soft espeak outloud speech-server ssh-dtk-exp	\
		ssh-outloud ; do 						\
		cp -p $${file} $${file}.debian_org ;				\
		sed -r --in-place 						\
			"s%/usr/bin/tclsh$$%/usr/bin/tclsh8.4%" $${file} ;\
	done

	echo $(shell dpkg-parsechangelog | grep Version | perl -p -e 's/Version: //') \
		> debian_version

	cd servers/python ; epydoc -o httpspeaker-html *.py

override_dh_auto_install:
	dh_install

override_dh_auto_clean:
	debconf-updatepo
# Restore backups.
	$(foreach file, $(debOrgFiles), $(restoreBackup))

# Add here commands to clean up after the build process.
	$(MAKE) clean
	cd servers/linux-espeak ; $(MAKE) clean

# the sources included some byte-compiled file by mistake
	find . -name \*.elc -delete
	find . -name \*.pyc -delete
	find . -name \*.pyo -delete
# patches are already applied and don't need to go in
	rm -rf lisp/g-client/patches
# documentation is generated, make sure we don't use the shipped stuff
	rm -rf servers/python/doc
	rm -rf servers/python/httpspeaker-html

	dh_clean 

override_dh_fixperms:
# 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 644 $(debtmp)/usr/share/doc/emacspeak/examples/tables.html ; \
		chmod 755 $(deblsp)/etc/*.pl				\
			$(deblsp)/etc/*2html				\
			$(deblsp)/etc/*2text				\
			$(deblsp)/lisp/g-client/python/contacts.py	\
			$(deblsp)/servers/32-outloud			\
			$(deblsp)/servers/dtk-exp			\
			$(deblsp)/servers/dtk-soft			\
			$(deblsp)/servers/espeak			\
			$(deblsp)/servers/outloud			\
			$(deblsp)/servers/python/HTTPSpeaker.py		\
			$(deblsp)/servers/speech-server			\
			$(deblsp)/servers/ssh-dtk-exp   		\
			$(deblsp)/servers/ssh-outloud ;			\
	fi

	dh_fixperms

override_dh_compress:
# FAQ/HELP/NEWS can be called from emacspeak command, don't compress
	dh_compress -XFAQ -XHELP -XNEWS

get-orig-source:
	-uscan --rename --force-download
