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

_realname=gtk4
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-media-gstreamer")
pkgver=4.18.6
pkgrel=4
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=(
  "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}-gst-plugins-bad-libs"
             "${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}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-shared-mime-info")
source=("https://download.gnome.org/sources/gtk/${pkgver:0:4}/gtk-${pkgver}.tar.xz"
        "001-fix-font-rendering.patch"
        "https://gitlab.gnome.org/GNOME/gtk/-/commit/8a288dc3a1e338723aa16b545a201b4c9ba0886b.patch")
sha256sums=('e1817c650ddc3261f9a8345b3b22a26a5d80af154630dedc03cc7becefffd0fa'
            'a2c6e3350bd9c1744da6b7714b25cbd419645b731435f7d295a9f99da3c1479f'
            '613a43c31f2e29ce49cbdda0092bc1fd894524d6ee773af2ee23c42862e88c02')

prepare() {
  cd gtk-${pkgver}

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

  # https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/8909
  patch -Np1 -i ../8a288dc3a1e338723aa16b545a201b4c9ba0886b.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=" \
  ${MINGW_PREFIX}/bin/meson.exe 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}

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package_gtk4() {
  ${MINGW_PREFIX}/bin/meson.exe install -C "${srcdir}/build-${MSYSTEM}" --destdir "${pkgdir}"

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

  # split media-gstreamer
  mkdir -p dest${MINGW_PREFIX}/lib
  mv "${pkgdir}${MINGW_PREFIX}"/lib/gtk-4.0 dest${MINGW_PREFIX}/lib/gtk-4.0
}

package_gtk4-media-gstreamer() {
  pkgdesc+=" (GStreamer media plugin)"
  optdepends=(
    "${MINGW_PACKAGE_PREFIX}-gst-plugins-good: plugins required in gtk4-widget-factory"
  )
  depends=("${MINGW_PACKAGE_PREFIX}-gtk4"
           "${MINGW_PACKAGE_PREFIX}-gst-plugins-bad-libs")

  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;
