# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=babl
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.1.116
pkgrel=1
pkgdesc="Dynamic Pixel Format Translation Library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gegl.org/babl/"
msys2_repository_url="https://gitlab.gnome.org/GNOME/babl"
license=("spdx:LGPL-3.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2")
source=("https://download.gimp.org/pub/babl/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "001-file-open-unicode.patch")
sha256sums=('50fae069867c7ade1259888ff1e3db85fec86d708252e5385b5a4f39a78ec483'
            '98cf43394eb1d1a8140438fbb745ed31f3768c07086afab297e4be9d609d07c5')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -bp1 -i "${srcdir}/${_patch}"
  done
}

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

  #FIXME: remove this patch on babl 0.1.118
  apply_patch_with_msg \
    001-file-open-unicode.patch
}

build() {
  mkdir -p "build-${MSYSTEM}" && cd "build-${MSYSTEM}"

  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-Denable-sse2=false"
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dwith-lcms=true \
    -Denable-gir=true \
    -Dwith-docs=false \
    ${_arch_opt} \
    "../${_realname}-${pkgver}"

  meson compile
}

package() {
  cd "build-${MSYSTEM}"

  meson install --destdir "${pkgdir}"

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