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

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

soname=libespeak.so.1
version=$(shell dpkg-parsechangelog|sed -n -e 's/^Version: //p'|cut -d- -f1|cut -d. -f1-2)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
ESPEAK_DATA=/usr/lib/$(DEB_HOST_MULTIARCH)

CFLAGS = -Wall -g

export DEB_BUILD_HARDENING=1

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif


build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp:
	dh_testdir

	cp src/portaudio19.h src/portaudio.h
	cd src && $(MAKE) CXXFLAGS="$(CXXFLAGS) $(CFLAGS)" DATADIR=$(ESPEAK_DATA)/espeak-data libespeak.a
	# Remove object files so we can rebuild them with
	# AUDIO=runtime enabled
	rm -f src/*.o
	cd src && $(MAKE) CXXFLAGS="$(CXXFLAGS) $(CFLAGS)" DATADIR=$(ESPEAK_DATA)/espeak-data espeak AUDIO=runtime

	# Build static speak binary
	cd platforms/big_endian && CFLAGS="$(CFLAGS)" $(MAKE)
	# Build voice metadata.
	cp -a $(CURDIR)/espeak-data $(CURDIR)/espeak-data-local
	$(CURDIR)/platforms/big_endian/espeak-phoneme-data $(CURDIR)/espeak-data-local $(CURDIR)/espeak-data-local $(CURDIR)/espeak-data-local/phondata-manifest

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp

	-cd src && $(MAKE) distclean
	cp -f src/portaudio18.h src/portaudio.h
	-cd platforms/big_endian && $(MAKE) clean

	rm -fr espeak-data-local

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	install -m 755 -o root -g root src/espeak $(CURDIR)/debian/espeak/usr/bin/espeak
	install -d $(CURDIR)/debian/espeak-data$(ESPEAK_DATA)
	cp -r espeak-data-local $(CURDIR)/debian/espeak-data$(ESPEAK_DATA)/espeak-data
	find $(CURDIR)/debian/espeak-data/usr/lib -type f -exec chmod 644 {} \;
	find $(CURDIR)/debian/espeak-data/usr/lib -type d -exec chmod 755 {} \;
	cp -al $(CURDIR)/debian/espeak-data/usr $(CURDIR)/debian/espeak-data-udeb
	rm -fr $(CURDIR)/debian/espeak-data-udeb$(ESPEAK_DATA)/espeak-data/mbrola* $(CURDIR)/debian/espeak-data-udeb$(ESPEAK_DATA)/espeak-data/voices/mb $(CURDIR)/debian/espeak-data-udeb$(ESPEAK_DATA)/espeak-data/voices/test
	mkdir -p $(CURDIR)/debian/libespeak1$(ESPEAK_DATA)
	mkdir -p $(CURDIR)/debian/libespeak-dev$(ESPEAK_DATA)
	install -m 644 -o root -g root src/$(soname).$(version) $(CURDIR)/debian/libespeak1$(ESPEAK_DATA)/$(soname).$(version)
	cd $(CURDIR)/debian/libespeak1$(ESPEAK_DATA) && ln -s $(soname).$(version) $(soname)
	cd $(CURDIR)/debian/libespeak1$(ESPEAK_DATA) && ln -s $(soname).$(version) libespeak.so 
	mv $(CURDIR)/debian/libespeak1$(ESPEAK_DATA)/libespeak.so $(CURDIR)/debian/libespeak-dev$(ESPEAK_DATA)
	install -m 644 -o root -g root src/libespeak.a $(CURDIR)/debian/libespeak-dev$(ESPEAK_DATA)
	install -m 644 -o root -g root src/speak_lib.h $(CURDIR)/debian/libespeak-dev/usr/include/espeak

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog.txt
	dh_installdocs
	dh_installman debian/espeak.1
	dh_link
	dh_strip --dbg-package=espeak-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_shlibdeps -L libespeak1 -l debian/libespeak1/usr/lib
	# workaround for LP#1220681; drop in trusty+1
	sed "s!@DATADIR@!$(ESPEAK_DATA)!g" debian/espeak-data.preinst.in > debian/espeak-data.preinst
	dh_installdeb -v
	rm debian/espeak-data.preinst
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
