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

_with_harfbuzz="yes"
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-i686* \
   || ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]]; then
  _with_harfbuzz="no"
fi

_realname=freetype
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.10.4
pkgrel=4
pkgdesc="TrueType font rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.freetype.org/"
license=(GPL2+ custom:FreeType)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
if [ "${_with_harfbuzz}" == "yes" ]; then
  depends+=("${MINGW_PACKAGE_PREFIX}-harfbuzz")
fi
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!libtool' 'staticlibs')
_commit="d911cb5392bd558642fcd6e85d6224618b8e1421"
source=("https://gitlab.freedesktop.org/freetype/freetype/-/archive/${_commit}/freetype-${_commit}.tar.gz"
        freetype-2.10.0-enable-valid.patch
        freetype-2.10.0-enable-spr.patch
        freetype-2.5.1-enable-sph.patch
        001-missing-header.patch)
sha256sums=('b2441499e46ab0fea4ee0a9f4d630b8d0983d62cb16c809ffc2905d9e1bae73e'
            '30e84993f899b66c31451992667f7ac6cc9e0b19dfe2f47ccb546d77bbc77ef5'
            '0571f18d97834df020e4cbdb1338b14f9160669f868237080951e01f5b86727c'
            '324012ad68388d22c63368314721d2360b48dd40daa808d0383699a281d931b0'
            '5858982baa8f25573bfebb4d4541b4fbbdb4ee6f86c8215fc9c89a3e54c784a8')
validpgpkeys=('58E0C111E39F5408C5D3EC76C1A60EACE707FDA5')

prepare() {
  mv "${srcdir}/freetype-${_commit}" "${srcdir}/freetype-${pkgver}"
  cd "${srcdir}/freetype-${pkgver}"

  plain "freetype-2.10.0-enable-valid.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-valid.patch"
  plain "freetype-2.10.0-enable-spr.patch"
  patch -p1 -i "${srcdir}/freetype-2.10.0-enable-spr.patch"

  # https://gitlab.freedesktop.org/freetype/freetype/-/issues/1058
  patch -p1 -i "${srcdir}/001-missing-header.patch"
}

build() {
  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  declare -a extra_config
  if [ "${_with_harfbuzz}" != "yes" ]; then
    extra_config+=("-Dharfbuzz=disabled")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

  meson compile
}

package () {
  cd "${srcdir}/build-${CARCH}"

  DESTDIR="${pkgdir}" meson install

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/GPLv2.TXT"   "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/GPLv2.TXT"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/docs/FTL.TXT"     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/FTL.TXT"
}
