# Maintainer: Michael Rans <rans@email.com>

_realname=espeak-ng
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.52.0.r141.g0d451f8c
pkgrel=1
pkgdesc="A text-to-speech synthesizer for Linux, Windows, Android and other operating systems (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/espeak-ng/espeak-ng"
license=("spdx:GPL-3.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-pcaudiolib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git")
_commit=0d451f8c1c6ae837418b823bd9c4cbc574ea9ff5
source=("git+https://github.com/espeak-ng/${_realname}#commit=${_commit}"
        "001-fix-build-on-mingw.patch")
sha256sums=('c6956dc62d0441b27bc81a373dee60905d688c708373b41270f89dd33fea30c0'
            'cb1ba339d22c65e46667646722ce777143d09c82262483b5f108d222f0c1fcbc')

pkgver() {
  cd "${srcdir}/${_realname}"
  git describe --long --tags ${_commit} | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

prepare() {
  cd "${srcdir}/${_realname}"
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DHAVE_PTHREAD=ON \
    -DUSE_LIBPCAUDIO=ON \
    ${extra_config[@]} \
    -S "${srcdir}/${_realname}" \
    -B "${srcdir}/build-${MSYSTEM}"

  cmake --build build-${MSYSTEM}

}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  cd "${pkgdir}"${MINGW_PREFIX}/include/espeak
  echo "#include \"../${_realname}/speak_lib.h\"" > speak_lib.h

  install -Dm644 "${srcdir}"/${_realname}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
