PORTNAME=	speech_tools
PORTVERSION=	2.5.1
CATEGORIES=	audio devel

MAINTAINER=	ports@virtual-estates.net
COMMENT=	C++ library and tools for generating speech
WWW=		https://github.com/festvox/speech_tools/

LICENSE=	BSD4CLAUSE
LICENSE_FILE=	${WRKSRC}/COPYING

LIB_DEPENDS=	librxp.so:textproc/rxp

USES=		localbase:ldflags meson perl5 pkgconfig python:build
USE_LDCONFIG=	yes
USE_PERL5=	build run
USE_GITHUB=	yes
GH_ACCOUNT=	festvox
GH_TAGNAME=	63ff019

# Use textproc/rxp instead of the bundled copy -- only CSTR's own C++
# wrapper (XML_Parser) and build-glue are extracted from the rxp/ and
# include/rxp/ directories.  The bundled SIOD, however, is a CSTR-specific
# fork, which lang/siod can not replace.
EXTRACT_AFTER_ARGS=	--exclude 'rxp/*.c' --exclude 'include/rxp/[a-z]*.h'

# ALSA support is in the same source file as OSS, and on FreeBSD
# (where OSS is always built) it is compiled out.  The "mplayer" method
# is Windows Media Player.
MESON_ARGS=	-Daudio_alsa=disabled -Daudio_esd=disabled \
		-Daudio_irix=disabled -Daudio_mplayer=disabled \
		-Daudio_osx=disabled -Daudio_win32=disabled \
		-Deditline=enabled -Dsiod_python=disabled
CXXFLAGS+=	-fno-delete-null-pointer-checks	# See BUG 269583

# Festival (and festvox, flite's voice-building tools) expect a built
# speech_tools tree in ESTDIR: the configured make-framework (config/),
# include/, lib/ (libraries and siod/), and bin/.  Assemble one from
# symlinks to the installed files, as Debian does.
ESTDIR=		${PREFIX}/lib/speech_tools
DATADIR=	${PREFIX}/share/speech-tools
DOCSDIR=	${PREFIX}/share/doc/speech-tools
PORTDOCS=	*

# OSS output is always built, the other sound systems are optional
OPTIONS_DEFINE=		DOCS NAS PULSEAUDIO

EST_CONFIGURE_ENV=	ac_cv_header_alsa_asoundlib_h=no
DOCS_BUILD_DEPENDS=	doxygen:devel/doxygen \
			xsltproc:textproc/libxslt
DOCS_MESON_ENABLED=	docs
NAS_LIB_DEPENDS=	libaudio.so:audio/nas
NAS_VARS=		EST_CONFIGURE_ARGS+=--with-nas_audio
PULSEAUDIO_LIB_DEPENDS=	libpulse-simple.so:audio/pulseaudio
PULSEAUDIO_VARS=	EST_CONFIGURE_ARGS+=--with-pulseaudio

.for o in NAS PULSEAUDIO
$o_MESON_ENABLED=	audio_${o:tl}
.endfor

post-extract:
	${CP} ${FILESDIR}/rxp.h ${WRKSRC}/include/rxp/

# Upstream's meson tests only run the shell-scripts, but -- except for
# wagon.sh -- do not compare their output with the expected results.
post-test:
.for t in ch_track ch_wave lpc tilt viterbi wagon
	${SED} 's,${WRKSRC}/lib/example_data//*,../lib/example_data/,g' \
	    ${TEST_WRKSRC}/$t_script.out | \
	    ${DIFF} -u ${WRKSRC}/testsuite/correct/$t_script.out -
.endfor

# Upstream's configure only generates config/config and user_flags.mak
# for the make-framework, which meson-builds do not need, but festival does.
post-configure:
	cd ${WRKSRC} && ${SETENV} ${CONFIGURE_ENV} ${EST_CONFIGURE_ENV} \
	    CC="${CC}" CXX="${CXX}" CPPFLAGS="${CPPFLAGS}" \
	    CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" LDFLAGS="${LDFLAGS}" \
	    ${CONFIGURE_SHELL} ./configure ${EST_CONFIGURE_ARGS}

post-install:
	${MKDIR} ${STAGEDIR}${ESTDIR}/lib
	cd ${WRKSRC} && ${COPYTREE_SHARE} config ${STAGEDIR}${ESTDIR}
	${FIND} ${STAGEDIR}${ESTDIR}/config \( -name Makefile \
	    -o -name '*.in' -o -name '*.orig' -o -name '*.bak' \) -delete
	# Any FreeBSD platform, not just i386 (ix86_FreeBSD.mak)
	${LN} -s ix86_FreeBSD.mak \
	    ${STAGEDIR}${ESTDIR}/config/systems/unknown_FreeBSD.mak
	${RLN} ${STAGEDIR}${PREFIX}/bin ${STAGEDIR}${ESTDIR}/bin
	${RLN} ${STAGEDIR}${PREFIX}/include/speech-tools/include \
	    ${STAGEDIR}${ESTDIR}/include
	${RLN} ${STAGEDIR}${DATADIR}/siod ${STAGEDIR}${ESTDIR}/lib/siod
.for l in eststring estbase estools
	${RLN} ${STAGEDIR}${PREFIX}/lib/lib$l.so ${STAGEDIR}${ESTDIR}/lib/
.endfor

.include <bsd.port.mk>
