PORTNAME=	festival
PORTVERSION=	2.5.1
CATEGORIES=	audio accessibility

PATCH_SITES=	https://salsa.debian.org/tts-team/festival/-/raw/1c640c62a62f47176b540484454dcc763b12957f/debian/patches/
PATCHFILES=	15-hts21compat.diff:-p1

MAINTAINER=	ports@virtual-estates.net
COMMENT=	Multi-lingual speech synthesis system
WWW=		https://www.cstr.ed.ac.uk/projects/festival/

LICENSE=	BSD4CLAUSE
LICENSE_FILE=	${WRKSRC}/COPYING

BUILD_DEPENDS=	${NONEXISTENT}:audio/hts_engine-API:patch
LIB_DEPENDS=	libHTSEngine.so:audio/hts_engine-API \
		libestools.so:audio/speech_tools \
		librxp.so:textproc/rxp

USES=		gmake localbase:ldflags
USE_CXXSTD=	gnu++14
USE_GITHUB=	yes
GH_ACCOUNT=	festvox
GH_TAGNAME=	affb4cc
# hts_engine comes from audio/hts_engine-API -- do not even extract the
# bundled copy.  Only Festival's glue (and Makefile) in that directory stay.
# Also not extracted: the Java interface (not built) and the 1999 Perl module
# in examples/, whose archive is damaged.
EXTRACT_AFTER_ARGS=	--no-same-owner --no-same-permissions \
		--exclude '*/src/modules/hts_engine/HTS_*' \
		--exclude '*/src/modules/hts_engine/[ACIR]*' \
		--exclude '*/src/modules/java' \
		--exclude '*/examples/speech_pm_1.0.tar'

GNU_CONFIGURE=	yes
CONFIGURE_ARGS=	--without-systemd
MAKE_JOBS_UNSAFE=	yes	# Recursive make-framework of speech_tools

# speech_tools installs its make-framework (and links to the libraries,
# headers, etc.) in ESTDIR.  Festival's own compiler-flags come from
# speech_tools' configuration -- override them with ours.
ESTDIR=		${LOCALBASE}/lib/speech_tools
MAKE_ARGS=	EST=${ESTDIR} REQUIRED_LIBDEPS= \
		FTLIBDIR=${DATADIR}/lib \
		CC="${CC}" CXX="${CXX}" \
		OPTIMISE_CCFLAGS= OPTIMISE_CXXFLAGS= OPTIMISE_LINKFLAGS= \
		USER_CPPFLAGS="${CPPFLAGS}" USER_CFLAGS="${CFLAGS}" \
		USER_CXXFLAGS="${CXXFLAGS}" USER_LINKFLAGS="${LDFLAGS}"
CXXFLAGS+=	-DFTETCDIRC=${PREFIX}/libexec/${PORTNAME} \
		-DFTSYSCONFDIRC=${PREFIX}/etc \
		-fno-delete-null-pointer-checks	# See BUG 269583
ALL_TARGET=	# empty

SCRIPTS=	festival_server festival_server_control text2wave
# Executables among the (otherwise data) files installed by COPYTREE_SHARE
EXECUTABLES=	lib/etc/email_filter examples/benchmark \
		${EXAMPLE_SCRIPTS:S,^,examples/,}
EXAMPLE_SCRIPTS=dumpfeats durmeanstd latest make_utts powmeanstd \
		run-festival-script saytime scfg_parse_text text2pos \
		text2utt text2wave
# System-wide configuration, as in Debian -- loaded from ${PREFIX}/etc instead
# of upstream's lib/sitevars.scm and lib/siteinit.scm
SUB_FILES=	festival.scm.sample festivalvars.scm.sample

post-extract:
	${CP} ${FILESDIR}/unknown_FreeBSD.mak ${WRKSRC}/config/systems/

# Festival's glue and clustergen use the engine's internals, which are not
# installed -- take them from the source of the library being linked in
pre-configure:
	cd `${MAKE} -C ${PORTSDIR}/audio/hts_engine-API -V WRKSRC` && \
	    ${CP} lib/HTS_hidden.h lib/HTS_vocoder.c \
	    ${WRKSRC}/src/modules/hts_engine/

# The voices are not included with festival itself, but provided by
# separate ports, which depend on festival...  The tests need the kal
# and rab diphone voices and the lexicons to be installed.  The "voice"
# and "modes" tests also use the cmu_us_{awb,rms,slt}_cg voices, which
# are not ported yet, and the newer (2008) CMU lexicon.
do-test:
.for d in voices dicts
	@if [ ! -d ${DATADIR}/lib/$d ]; then \
		${ECHO_MSG} "Festival's tests need ${DATADIR}/lib/$d -- install audio/festvox-kal16, audio/festvox-rab16, audio/festlex-cmu, audio/festlex-poslex"; \
		${FALSE}; \
	fi
	${LN} -sfh ${DATADIR}/lib/$d ${WRKSRC}/lib/
.endfor
	# Staging (COPYTREE_SHARE hard-links, then chmods) may have reset these
	cd ${WRKSRC} && ${CHMOD} 755 ${EXECUTABLES}
	${RM} ${WRKSRC}/bin/festival	# A symlink to src/main/festival
	${PRINTF} '#!/bin/sh\nFESTLIBDIR=%s exec %s "$$@"\n' \
	    ${WRKSRC}/lib ${WRKSRC}/src/main/festival > ${WRKSRC}/bin/festival
	${CHMOD} +x ${WRKSRC}/bin/festival
	${DO_MAKE_BUILD} -C ${WRKSRC}/testsuite test
	${RM} ${WRKSRC}/bin/festival ${WRKSRC}/lib/voices ${WRKSRC}/lib/dicts
	${LN} -s ${WRKSRC}/src/main/festival ${WRKSRC}/bin/

do-install:
	${INSTALL_PROGRAM} ${WRKSRC}/src/main/festival \
	    ${WRKSRC}/src/main/festival_client ${STAGEDIR}${PREFIX}/bin/
.for s in ${SCRIPTS}
	${SED} -e 's,${WRKSRC}/bin/,${PREFIX}/bin/,g' \
	    -e 's,^TOP=${WRKSRC}$$,TOP=${DATADIR},' ${WRKSRC}/bin/$s > \
	    ${STAGEDIR}${PREFIX}/bin/$s
	${CHMOD} 755 ${STAGEDIR}${PREFIX}/bin/$s
.endfor
	${MKDIR} ${STAGEDIR}${PREFIX}/libexec/${PORTNAME}
	${INSTALL_PROGRAM} ${WRKSRC}/lib/etc/unknown_FreeBSD/audsp \
	    ${STAGEDIR}${PREFIX}/libexec/${PORTNAME}/
	${MKDIR} ${STAGEDIR}${DATADIR}
	cd ${WRKSRC} && ${COPYTREE_SHARE} lib ${STAGEDIR}${DATADIR}
	${FIND} ${STAGEDIR}${DATADIR}/lib \( -name Makefile -o -name '*.orig' \
	    -o -name VCLocalRules -o -name 'unknown_*' -o -name siteinit.scm \) \
	    -prune -exec ${RM} -r {} +
	${INSTALL_DATA} ${SUB_FILES:S,^,${WRKDIR}/,} ${STAGEDIR}${PREFIX}/etc/
	cd ${WRKSRC} && ${COPYTREE_SHARE} examples ${STAGEDIR}${DATADIR}
	${GREP} -lr ${WRKSRC} ${STAGEDIR}${DATADIR}/examples | \
	    ${XARGS} ${REINPLACE_CMD} -e 's,${WRKSRC}/bin/,${PREFIX}/bin/,g'
	${FIND} ${STAGEDIR}${DATADIR}/examples \( -name Makefile \
	    -o -name '*.orig' -o -name '*.bak' \) -delete
	cd ${STAGEDIR}${DATADIR} && ${CHMOD} 755 ${EXECUTABLES}
.for m in festival festival_client text2wave
	${INSTALL_MAN} ${WRKSRC}/doc/$m.1 ${STAGEDIR}${PREFIX}/share/man/man1/
.endfor

.include <bsd.port.mk>
