# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=gtk4
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.20.3
pkgrel=1
pkgdesc="GObject-based multi-platform GUI toolkit (v4) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gtk.org"
msys2_repository_url="https://gitlab.gnome.org/GNOME/gtk/"
msys2_references=(
  'anitya: 13942'
  'gentoo: gui-libs/gtk'
  "cpe: cpe:/a:gnome:gtk"
  "cpe: cpe:/a:gtk:gtk%2b"
)
license=('spdx:LGPL-2.1-or-later')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-sassc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-shaderc")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-adwaita-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-graphene"
         "${MINGW_PACKAGE_PREFIX}-json-glib"
         "${MINGW_PACKAGE_PREFIX}-libepoxy"
         "${MINGW_PACKAGE_PREFIX}-pango"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
conflicts=("${MINGW_PACKAGE_PREFIX}-gtk4-media-gstreamer")
provides=("${MINGW_PACKAGE_PREFIX}-gtk4-media-gstreamer")
replaces=("${MINGW_PACKAGE_PREFIX}-gtk4-media-gstreamer")
optdepends=(
  "${MINGW_PACKAGE_PREFIX}-gst-plugins-good: plugins required in gtk4-widget-factory"
)
source=("https://download.gnome.org/sources/gtk/${pkgver:0:4}/gtk-${pkgver}.tar.xz"
        "001-fix-font-rendering.patch"
        "002-fix-fatal-no-dcomp.patch"
        "003-default-dcomp-off.patch")
sha256sums=('2873f2903088a66c71173ea2ed85ffae266a66b972c3a4842bbb2f6f187ec153'
            'a2c6e3350bd9c1744da6b7714b25cbd419645b731435f7d295a9f99da3c1479f'
            '074e5ba434a844b4e0a22bf261916513b2d09122b04caf158a1b7dfcb450dce2'
            'dda6bbab33b12ce50cb8706e7b9970f040dde4ba7cf2fe89c6f3842809fc0e87')

prepare() {
  cd gtk-${pkgver}

  # Re-enables font hinting
  patch -Np1 -i ../001-fix-font-rendering.patch

  # Disable broken dcomp, and fix a missing guard for when it is disabled
  patch -Np1 -i ../002-fix-fatal-no-dcomp.patch
  patch -Np1 -i ../003-default-dcomp-off.patch
}

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

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    "${extra_config[@]}" \
    -Dbuild-examples=false \
    -Dbuild-tests=false \
    -Dbuild-testsuite=false \
    -Dman-pages=true \
    -Dmacos-backend=false \
    -Dmedia-gstreamer=enabled \
    -Dvulkan=enabled \
    -Dx11-backend=false \
    -Dwayland-backend=false \
    -Dwin32-backend=true \
    -Dintrospection=enabled \
    ../gtk-${pkgver}

  meson compile
}

package() {
  meson install -C "${srcdir}/build-${MSYSTEM}" --destdir "${pkgdir}"

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