pkgname=fontconfig
pkgver=2.17.1
pkgdesc="Library for configuring and customizing font access"
homepage=https://www.freedesktop.org/wiki/Software/fontconfig/
arch=(x86_64)
license=('HPND AND Unicode-DFS-2016')
depends=(
  bash
  expat
  freetype
  glibc
)
makedepends=(
  gperf
  meson
)
_patches=("fontconfig-2.14.0-latin-update.patch"
  "fontconfig-2.17.0-skip-bubblewrap-tests.patch"
  "fontconfig-2.17.0-network-test.patch"
  "fontconfig-2.17.0-macro-preprocess.patch"
)
_patch_sums=("92015fbfc95fae01a0ad6f346132d215"
  "e23add7395db7d284c8557329058c694"
  "897596e23b6c42d4b15911dc70df4542"
  "886ae03ad92de457c3f3ac6aafe1fcf6"
)
sources=("${pkgname}-${pkgver}.tar.xz"
  "40-fontconfig-config.HOOK"
  ${_patches[@]}
)
urls=("https://gitlab.freedesktop.org/api/v4/projects/890/packages/generic/fontconfig/${pkgver}/${sources[0]}"
  "40-fontconfig-config.HOOK"
  ${_patches[@]}
)
md5sums=("f68f95052c7297b98eccb7709d817f6a"
  "fafd8323134280f4da770f95037c9324"
  ${_patch_sums[@]}
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  for _patch in ${_patchs[@]}; do
    patch -p1 -i ${filedir}/${_patch}
  done
}

src_build() {
  local addfonts=(
    /usr/local/share/fonts
  )
  local meson_options=(
    --prefix=/usr
    --buildtype=release
    -D bitmap-conf=noinstall
    -D default-sub-pixel-rendering=noinstall
    -D doc=disabled
    -D doc-html=disabled
    -D doc-pdf=disabled
    -D doc-man=disabled
    -D doc-txt=disabled
    -D nls=enabled
    -D tests=disabled
    
    -D cache-build=disabled
    -D cache-dir=/var/cache/fontconfig
    -D default-fonts-dirs=/usr/share/fonts
    -D additional-fonts-dirs=$(IFS=, ; echo "${addfonts[*]}" )
    -D template-dir=/etc/fonts/conf.avail
    -D fontations=disabled
    -D xml-backend=expat
  )
  meson setup build "${meson_options[@]}"
  meson compile -C build
}

src_install() {
  meson install -C build --destdir "$pkgdir"

  install -Dvm644 build/fonts.conf "${pkgdir}/etc/fonts/"
  
  install -Dvm644 fc-lang/*.orth -t "${pkgdir}/usr/share/fc-lang"

  install -Dvm644 doc/fontconfig-user.{txt,pdf} -t "${pkgdir}/usr/share/doc/${pkgname}-${pkgver}"

  leaf_install_hook "${filedir}/${sources[1]}"
}

src_preinstall() {
  echo -e "${GREEN_COLOR}Syncing fontconfig configuration to system${CLEAR_COLOR}"
  if [[ -e /etc/fonts/conf.d ]] ; then
    local file f
    for file in ""/etc/fonts/conf.avail/* ; do
      f=${file##*/}
      if [[ -L /etc/fonts/conf.d/${f} ]] ; then
        [[ -f ${pkgdir}/etc/fonts/conf.avail/${f} ]] \
              && ln -sf ../conf.avail/"${f}" \
                  "${pkgdir}"/etc/fonts/conf.d/ &>/dev/null
      else
        [[ -f ${pkgdir}/etc/fonts/conf.avail/${f} ]] \
              && rm "${pkgdir}"/etc/fonts/conf.d/"${f}" &>/dev/null
      fi
    done
  fi
}

src_postinstall() {
  echo -e "${GREEN_COLOR}Cleaning broken symlinks in /etc/fonts/conf.d/${CLEAR_COLOR}"
  find -L /etc/fonts/conf.d/ -type l -delete
}

# vim:ft=sh syn=sh et sw=2:
