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

_realname=fontconfig
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.13.93
pkgrel=3
pkgdesc="A library for configuring and customizing font access (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://wiki.freedesktop.org/www/Software/fontconfig/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-lxml"
             "${MINGW_PACKAGE_PREFIX}-gperf")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat>=2.1.0"
         "${MINGW_PACKAGE_PREFIX}-freetype>=2.3.11"
         "${MINGW_PACKAGE_PREFIX}-bzip2>=1.0.6"
         "${MINGW_PACKAGE_PREFIX}-libiconv")
options=('staticlibs' 'strip')
install=${_realname}-${MSYSTEM}.install
source=("https://www.freedesktop.org/software/fontconfig/release/fontconfig-${pkgver}.tar.xz"
        0001-fix-config-linking.all.patch
        0002-fix-mkdir.mingw.patch
        0004-fix-mkdtemp.mingw.patch
        0005-fix-setenv.mingw.patch
        0007-pkgconfig.mingw.patch
        fontconfig.hook.in)
sha256sums=('ea968631eadc5739bc7c8856cef5c77da812d1f67b763f5e51b57b8026c1a0a0'
            '1266d4bbd8270f013fee2401c890f0251babf50a175a69d681d3a6af5003c899'
            '9e5700b9c555e60cd8116fdc2e24cf0a01de01e83373dbbe78cef1e32b91da3b'
            '57ff8420dbf62873b6fcb38b52fb7b37e0e278425a9125e15dccba54668c8ab9'
            '552b54010f9fe4097f332cf2397bbd3e78489542d3bbf07792ed1cfe9381796e'
            'af373531873da46d0356305da5444c1ec74f443cd2635ea2db6b7dadd1561f5b'
            'ad4ce48983045d7f39b40ca9e04d4be2eaa9177b89dcc516388d779c131a88bc')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/0001-fix-config-linking.all.patch
  patch -p1 -i ${srcdir}/0002-fix-mkdir.mingw.patch
  patch -p1 -i ${srcdir}/0004-fix-mkdtemp.mingw.patch
  patch -p1 -i ${srcdir}/0005-fix-setenv.mingw.patch
  patch -p1 -i ${srcdir}/0007-pkgconfig.mingw.patch

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --enable-static \
    --enable-iconv \
    --with-libiconv=${MINGW_PREFIX} \
    --disable-docs \
    --with-arch=${CARCH} \
    --with-cache-dir=/var/cache/${_realname} \
    as_ln_s="cp -pR"

  sed -i 's,all-am: Makefile $(PROGRAMS),all-am:,' test/Makefile
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  mv ${pkgdir}{,${MINGW_PREFIX}}/var

  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm

  # Convert MSYS2-specific paths to pkg-config variables (see issue #872)
  sed -i "s#${MINGW_PREFIX}/lib#\${libdir}#g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/fontconfig.pc
  sed -i "s#${MINGW_PREFIX}/include#\${includedir}#g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/fontconfig.pc

  for hook in fontconfig; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done
}
