# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=evince
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=48.1
pkgrel=5
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="Document (PostScript, PDF) viewer (mingw-w64)"
url="https://wiki.gnome.org/Apps/Evince"
msys2_repository_url="https://gitlab.gnome.org/GNOME/evince"
msys2_references=(
  "cpe: cpe:/a:gnome:evince"
)
license=("spdx:GPL-2.0-or-later")
depends=("${MINGW_PACKAGE_PREFIX}-appstream-glib"
         "${MINGW_PACKAGE_PREFIX}-atk"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas"
         "${MINGW_PACKAGE_PREFIX}-gspell"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libgxps"
         "${MINGW_PACKAGE_PREFIX}-libhandy"
         "${MINGW_PACKAGE_PREFIX}-libspectre"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-nss"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-desktop-file-utils"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-itstool"
             "${MINGW_PACKAGE_PREFIX}-yelp-tools"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libgxps")
optdepends=("${MINGW_PACKAGE_PREFIX}-libtiff: TIFF backend"
            "${MINGW_PACKAGE_PREFIX}-libgxps: XPS backend")
source=(https://download.gnome.org/sources/${_realname}/${pkgver%%.*}/${_realname}-${pkgver}.tar.xz
        0003-mingw-dont-have-fcntl.patch
        0008-disable-pdf-document-load-fd.patch
        0009-evince-pe-icon.patch::https://gitlab.gnome.org/GNOME/evince/-/merge_requests/701.diff
        evince.ico::https://gitlab.gnome.org/GNOME/evince/-/raw/24967ecf7f658bb69e512d6e09287c1ccc02c11c/shell/evince.ico?inline=false
        0010-no-console.patch
        011-no-portal.patch)
sha256sums=('7d8b9a6fa3a05d3f5b9048859027688c73a788ff6e923bc3945126884943fa10'
            '5c011fb1dec0564dcea06d145a2cc3572641dcd021477eb1172ecddd9cacbffd'
            'f9e2010366f9185e1f6a18b9ac7207c792d970fd1636b3f3bd329f58259cf89e'
            '6b5a22b5b7ad8bd964c579ccd31a4776eecf0d33b3f10ffacb64774dfb70ff10'
            '0b7493eef41f0907cb523d9165c7a67a13f05c7ee9fb5cce42f7ff3342bc4e4e'
            'db40297acd4a5eaf7cc94464737020a22c2f9a952683f55adfc24a392de1af24'
            '4006652530d41ee10f879e6fa3c1ab7675d1e85900e57f2f0600928d2f64166a')
noextract=(${_realname}-${pkgver}.tar.xz)

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

prepare() {
  tar -xJf "${srcdir}"/${_realname}-${pkgver}.tar.xz || true
  cd ${_realname}-${pkgver}

  apply_patch_with_msg \
    0003-mingw-dont-have-fcntl.patch \
    0008-disable-pdf-document-load-fd.patch \
    0009-evince-pe-icon.patch \
    0010-no-console.patch \
    011-no-portal.patch

  cp "${srcdir}/evince.ico" "shell/evince.ico"
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
   meson setup \
     --prefix=${MINGW_PREFIX} \
     --wrap-mode=nodownload \
     --auto-features=enabled \
     --buildtype=plain \
     -Dplatform=win32 \
     -Ddbus=false \
     -Dnautilus=false \
     -Dthumbnail_cache=disabled \
     -Ddvi=disabled \
     -Dgtk_doc=false \
     -Dgtk_unix_print=disabled \
     "../${_realname}-${pkgver}"

  meson compile
}

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

  DESTDIR="${pkgdir}" meson install

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