#!/usr/bin/make -f
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/makefile.mk

IS_FREMANTLE=$(shell if grep Fremantle /etc/maemo_version &> /dev/null; then echo y; else echo n; fi)
IS_DIABLO=$(shell if grep Diablo /etc/maemo_version &> /dev/null; then echo y; else echo n; fi)

#DEB_TAR_SRCDIR:=espeak-1.45.02/src
DEB_TAR_SRCDIR:=espeak-1.45.02-source/src
DEB_DESTDIR=$(CURDIR)/debian/tmp
#DEB_MAKE_BUILD_TARGET:=all AUDIO=none
#DEB_MAKE_INSTALL_TARGET=install DESTDIR=$(DEB_DESTDIR) AUDIO=none

# Pulseaudio for Fremantle, portaudio for the rest
# And move data files to /opt, this sucks so bad
ifeq ($(IS_FREMANTLE),y)
DEB_MAKE_BUILD_TARGET:=all AUDIO=pulseaudio DATADIR=/opt/espeak/data
DEB_MAKE_INSTALL_TARGET=install DESTDIR=$(DEB_DESTDIR) AUDIO=pulseaudio DATADIR=/opt/espeak/data
DEB_OPT_FLAG_armel:=-O2 -fomit-frame-pointer -pipe -mcpu=cortex-a8 -mfpu=neon -mfloat-abi=softfp
ES_DATA_DIR=/opt/espeak/data
else
DEB_MAKE_BUILD_TARGET:=all AUDIO=portaudio
DEB_MAKE_INSTALL_TARGET=install DESTDIR=$(DEB_DESTDIR) AUDIO=portaudio
DEB_OPT_FLAG_armel:=-O2 -fomit-frame-pointer -pipe -mcpu=arm1136jf-s -mfpu=vfp -mfloat-abi=softfp
ES_DATA_DIR=/usr/share/espeak-data
endif

# Make sure we get properly optimized
DEB_OPT_FLAG_i386:=-O2 -fomit-frame-pointer -pipe -march=i686 -mtune=pentium2
DEB_OPT_FLAG:=$(DEB_OPT_FLAG_$(DEB_HOST_ARCH))

include /usr/share/cdbs/1/rules/tarball.mk

configure/espeak::
	-make -C build-tree/$(DEB_TAR_SRCDIR) clean
	cp build-tree/$(DEB_TAR_SRCDIR)/portaudio19.h build-tree/$(DEB_TAR_SRCDIR)/portaudio.h
ifeq ($(IS_FREMANTLE),y)
	sed -i -e s,/usr/share/espeak-data,/opt/espeak/data, build-tree/$(DEB_TAR_SRCDIR)/speech.h
	sed -e s,@DATADIR@,/opt/espeak/data, debian/espeak-data.install.in > debian/espeak-data.install 
else
	sed -e s,@DATADIR@,/usr/share/espeak-data, debian/espeak-data.install.in > debian/espeak-data.install 
endif

# argh, stupid software writes directly to DATADIR even if not installed yet, need to pre-generate and package those instead
#build/espeak::
#	@echo "Building extra languages data"
#	cp ru_listx zh_listx zhy_list build-tree/$(DEB_TAR_SRCDIR)/../dictsource/
#	cd build-tree/$(DEB_TAR_SRCDIR)/../dictsource/ && ../src/espeak --compile=ru
#	cd build-tree/$(DEB_TAR_SRCDIR)/../dictsource/ && ../src/espeak --compile=zh-yue
#	cd build-tree/$(DEB_TAR_SRCDIR)/../dictsource/ && ../src/espeak --compile=zh

install/espeak-data::
	rm debian/tmp/$(ES_DATA_DIR)/ru_dict
	rm debian/tmp/$(ES_DATA_DIR)/zhy_dict
	rm debian/tmp/$(ES_DATA_DIR)/zh_dict

install/espeak::
	strip -g debian/tmp/usr/bin/espeak
