# $Id: PKGBUILD 205607 2014-02-09 10:29:39Z andyrtr $
# Maintainer:
# Contributor: Andrea Scarpino <andrea@archlinux.org>
# Contributor: Simo Leone <neotuli@gmail.com>

pkgname=festival
pkgver=2.1
pkgrel=4
pkgdesc="A general multi-lingual speech synthesis system"
arch=('i686' 'x86_64')
url="http://www.cstr.ed.ac.uk/projects/festival/"
license=('BSD' 'GPL' 'custom')
depends=('perl')
options=('!makeflags')
source=(http://www.cstr.ed.ac.uk/downloads/${pkgname}/${pkgver}/${pkgname}-${pkgver}-release.tar.gz
        http://www.cstr.ed.ac.uk/downloads/${pkgname}/${pkgver}/speech_tools-${pkgver}-release.tar.gz
        http://www.cstr.ed.ac.uk/downloads/${pkgname}/${pkgver}/festlex_CMU.tar.gz
        http://www.cstr.ed.ac.uk/downloads/${pkgname}/${pkgver}/festlex_OALD.tar.gz
        http://www.cstr.ed.ac.uk/downloads/${pkgname}/${pkgver}/festlex_POSLEX.tar.gz
        speechconfig.patch
        festconfig.patch
        festival-shared-build.patch
        festival.gcc47.patch
        festival-2.1-bettersonamehack.patch)
md5sums=('c93eb3e389ed171ab9abd46afe8897a8'
         '6920ddc75b042910a3bcfee3ab106938'
         '6a2ee4fed7c3ebedf197a3b8524ccb87'
         '84af32a914d996f57bc4cb36fe8cdc97'
         'aa80f9250065b318325f16fdad3a4484'
         'c30ae990baca40c9c2b1d5cd27502139'
         '348cc033430aee7989463198818ae74c'
         '02f589688182527308e3fe8d1a82c056'
         'ed738fdf981aaddbae6d00c3a0120d62'
         '85367db2b57bae214d1032e2fc896753')

prepare() {
  cd $srcdir
  patch -Np0 -i ${srcdir}/speechconfig.patch
  patch -Np0 -i ${srcdir}/festconfig.patch

  # fix build with new gcc versions and build shared libs - taken from Mageia
  patch -Np0 -i ${srcdir}/festival-shared-build.patch
  patch -Np0 -i ${srcdir}/festival-2.1-bettersonamehack.patch
  patch -Np0 -i ${srcdir}/festival.gcc47.patch
  
  # Avoid make failure on making scripts and docs
  sed -i "s#examples bin doc#examples#" festival/Makefile
}

build() {
  # Build Speech Tools first
  cd "$srcdir/speech_tools"
  ./configure --prefix=/usr --sysconfdir=/etc
  make OPTIMISE_CXXFLAGS="${CXXFLAGS} -fno-strict-aliasing" OPTIMISE_CCFLAGS="${CFLAGS} -fno-strict-aliasing"

  # Build Festival itself
  cd "$srcdir/festival"
  ./configure --prefix=/usr --sysconfdir=/etc
  make OPTIMISE_CXXFLAGS="${CXXFLAGS}" OPTIMISE_CCFLAGS="${CFLAGS}"
}

package() {
  # Install Speech Tools first
  cd "$srcdir/speech_tools"
  install -dm755 "$pkgdir"/usr/{bin,lib,include/speech_tools/{,instantiate,ling_class,rxp,sigpr,unix}}

  #binaries
  #move binaries over wrappers (FS#7864)
  for i in $(grep -l 'EST shared script' bin/*); do
    cp -f main/$(basename $i) bin;
  done
  install -m755 -t "$pkgdir"/usr/bin bin/[a-z]*
  rm -f "$pkgdir"/usr/bin/{est_gdb,est_examples,est_program}

  #libraries
  install -m755 -t "$pkgdir"/usr/lib lib/lib*.so.*
  ln -sf libestbase.so.${pkgver}.1 "$pkgdir"/usr/lib/libestbase.so
  ln -sf libestools.so.${pkgver}.1 "$pkgdir"/usr/lib/libestools.so
  ln -sf libeststring.so.1.2 "$pkgdir"/usr/lib/libeststring.so

  #headers
  for dir in {.,instantiate,ling_class,rxp,sigpr,unix}; do
    install -m644 -t "${pkgdir}"/usr/include/speech_tools/$dir include/$dir/*.h
  done

  # Install Festival itself
  cd "$srcdir"/festival

  #binaries
  install -m755 src/main/festival "$pkgdir"/usr/bin/
  install -m755 src/main/festival_client "$pkgdir"/usr/bin/
  install -m755 examples/benchmark "$pkgdir"/usr/bin/
  install -m755 examples/dumpfeats "$pkgdir"/usr/bin/
  install -m755 examples/durmeanstd "$pkgdir"/usr/bin/
  install -m755 examples/latest "$pkgdir"/usr/bin/
  install -m755 examples/make_utts "$pkgdir"/usr/bin/
  install -m755 examples/powmeanstd "$pkgdir"/usr/bin/
  install -m755 examples/run-festival-script "$pkgdir"/usr/bin/
  install -m755 examples/saytime "$pkgdir"/usr/bin/
  install -m755 examples/scfg_parse_text "$pkgdir"/usr/bin/
  install -m755 examples/text2pos "$pkgdir"/usr/bin/
  install -m755 examples/text2wave "$pkgdir"/usr/bin

  #libraries
  install -m755 src/lib/libFestival.so.* "$pkgdir"/usr/lib/
  ln -sf libFestival.so.2.1.0 "$pkgdir"/usr/lib/libFestival.so

  #headers
  install -dm755 "$pkgdir"/usr/include/festival
  install -m644 -t "$pkgdir"/usr/include/festival src/include/*.h

  mkdir -p "$pkgdir"/usr/share/festival
  cp -aR lib/* "$pkgdir"/usr/share/festival
  rm -fv $(find "$pkgdir"/usr/share/festival -name Makefile)
  rm -fv $(find "$pkgdir"/usr/bin -name Makefile)

  #create voices directory
  install -dm755 "$pkgdir"/usr/share/festival/voices

  #licenses
  install -D -m644 "$srcdir"/festival/COPYING \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
  install -D -m644 "$srcdir"/speech_tools/README \
    "$pkgdir"/usr/share/licenses/$pkgname/LICENSE.other

  # Ok now some general cleanups
  for i in $(find "$pkgdir"/usr/include/ -type f); do
    sed -i -e 's,"EST.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/\",speech_tools/,g' \
      -e 's,"siod.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/\",speech_tools/,g' \
      -e 's,"instantiate/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/instantiate/\",speech_tools/instantiate/,g' -e 's,"instantiate,instantiate,g' \
      -e 's,"ling_class/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/ling_class/\",speech_tools/ling_class/,g' -e 's,"ling_class,ling_class,g' \
      -e 's,"rxp/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/rxp/\",speech_tools/rxp/,g' -e 's,"rxp,rxp,g' \
      -e 's,"sigpr/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/sigpr/\",speech_tools/sigpr/,g' -e 's,"sigpr,sigpr,g' \
      -e 's,"unix/.*\.h",\<speech_tools/&\>,g' -e 's,speech_tools/unix/\",speech_tools/unix/,g' -e 's,\.h\">,.h\>,g' -e 's,"unix,unix,g' \
      -e 's,"festival\.h",\<festival/festival.h\>,g' \
      -e 's,"ModuleDescription\.h",\<festival/ModuleDescription.h\>,g' \
      -e 's,"Phone\.h",\<festival/Phone.h\>,g' $i
  done
}
