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

_realname=librsvg
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=2.61.1
pkgrel=2
pkgdesc="SVG rendering library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://wiki.gnome.org/Projects/LibRsvg"
msys2_references=(
  "cpe: cpe:/a:gnome:librsvg"
  "purl: pkg:cargo/librsvg"
)
license=("spdx:LGPL-2.1-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-vala"
             "${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-cc")
depends=("${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3: for rsvg-view-3")
source=("https://download.gnome.org/sources/librsvg/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        "0004-add-option-for-disabling-convert.patch")
sha256sums=('bc1bbcd419120b098db28bea55335d9de2470d4e6a9f6ee97207b410fc15867d'
            '0e042886d313b37f5db5e0405a5e37b3d3e4a6c66f94186677406136fa0e7a17')
msys2_repository_url="https://gitlab.gnome.org/GNOME/librsvg"
noextract=("${_realname}-${pkgver}.tar.xz")

prepare() {
  bsdtar -xf "${_realname}-${pkgver}.tar.xz" 2>/dev/null || true

  cd "${_realname}-${pkgver}"

  # fails to link for the static build, so skip for now
  patch -p1 -i "${srcdir}/0004-add-option-for-disabling-convert.patch"

  cargo fetch --locked
}

build() {
  # https://github.com/msys2/MINGW-packages/pull/13622#issuecomment-1285820970
  export WINAPI_NO_BUNDLED_LIBRARIES=1

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --default-library=static \
      --buildtype=plain \
      -Dintrospection=disabled \
      -Davif=disabled \
      -Dtests=false \
      -Ddocs=disabled \
      -Dpixbuf-loader=disabled \
      -Drsvg-convert=disabled \
      "build-${MSYSTEM}-static" \
      "${_realname}-${pkgver}"

  meson compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    meson setup \
      --prefix="${MINGW_PREFIX}" \
      --wrap-mode=nodownload \
      --auto-features=enabled \
      --buildtype=plain \
      -Davif=disabled \
      "build-${MSYSTEM}" \
      "${_realname}-${pkgver}"

  meson compile -C "build-${MSYSTEM}"
}

package_librsvg() {
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-2.40")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-2.40")

  meson install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  meson install -C "build-${MSYSTEM}" --destdir "${pkgdir}"

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

  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_librsvg-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
