# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=harfbuzz
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.8.2
pkgrel=1
pkgdesc="OpenType text shaping engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freedesktop.org/wiki/Software/HarfBuzz"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-icu"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-ragel")
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphite2")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-fonttools" 
              "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('strip' 'staticlibs')
optdepends=("${MINGW_PACKAGE_PREFIX}-icu: harfbuzz-icu support"
            "${MINGW_PACKAGE_PREFIX}-cairo: hb-view program")
source=("https://github.com/harfbuzz/harfbuzz/archive/${pkgver}.tar.gz")
sha256sums=('4164f68103e7b52757a732227cfa2a16cfa9984da513843bb4eb7669adc6f220')
noextract=("${pkgver}.tar.gz")

prepare() {
  tar -xzf ${srcdir}/${pkgver}.tar.gz -C ${srcdir} || true
  cd "${srcdir}/${_realname}-${pkgver}"
}

build() {
  # Build static and shared libs separately due to necessity of defining DGRAPHITE2_STATIC
  # manually when building static version
  export _static_dir="${srcdir}/build-${MINGW_CHOST}-static"
  export _shared_dir="${srcdir}/build-${MINGW_CHOST}-shared"

 [[ -d "${_static_dir}" ]] && rm -rf "${_static_dir}"
  mkdir -p "${_static_dir}" && pushd "${_static_dir}"

  # static build
  CFLAGS="${CFLAGS} -DGRAPHITE2_STATIC" \
  CXXFLAGS="${CXXFLAGS} -DGRAPHITE2_STATIC" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    --default-library=static \
    -Dauto_features=enabled \
    -Dintrospection=disabled \
    -Dgobject=enabled \
    -Dicu=enabled \
    -Dgdi=enabled \
    -Dgraphite=enabled \
    -Dchafa=disabled \
    -Ddirectwrite=enabled \
    ../${_realname}-${pkgver}

  ninja
  popd

  # shared build
  [[ -d "${_shared_dir}" ]] && rm -rf "${_shared_dir}"
  mkdir -p "${_shared_dir}" && pushd "${_shared_dir}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    --wrap-mode=nofallback \
    --default-library=shared \
    -Dauto_features=enabled \
    -Dintrospection=enabled \
    -Dgobject=enabled \
    -Dicu=enabled \
    -Dgdi=enabled \
    -Dgraphite=enabled \
    -Dchafa=disabled \
    -Ddirectwrite=enabled \
    ../${_realname}-${pkgver}

  ninja
  popd
}

check(){
  cd "${srcdir}/build-${MINGW_CHOST}-shared"

  meson test
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR="${pkgdir}" ninja install

  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR="${pkgdir}" ninja install

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