# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Maintainer: Fabian Bornschein <fabiscafe@archlinux.org>
# Contributor: Ionut Biru <ibiru@archlinux.org>
# Contributor: Michael Kanis <mkanis_at_gmx_dot_de>

pkgbase=mutter-kimiblock-unstable
pkgname=(
	mutter-kimiblock-unstable
)
pkgver=51.beta
pkgrel=4
pkgdesc="Window manager and compositor for GNOME, unstable version with patches picked from Kimiblock"
url="https://gitlab.gnome.org/GNOME/mutter"
arch=(x86_64)
provides+=(mutter)
conflicts+=(mutter)
license=(GPL-2.0-or-later)
depends=(
  at-spi2-core
  cairo
  colord
  dconf
  egl-wayland
  fontconfig
  fribidi
  gdk-pixbuf2
  glib2
  glibc
  glycin
  gnome-desktop-4
  gnome-settings-daemon
  graphene
  gsettings-desktop-schemas
  gtk4
  harfbuzz
  iio-sensor-proxy
  lcms2
  libcanberra
  libcolord
  libdisplay-info
  libdrm
  libei
  libgcc
  libgirepository
  libglvnd
  libgudev
  libinput
  libpipewire
  libsysprof-capture
  libwacom
  libx11
  libxau
  libxcb
  libxcomposite
  libxcursor
  libxdamage
  libxext
  libxfixes
  libxi
  libxinerama
  libxkbcommon
  libxrandr
  mesa
  pango
  pipewire
  pixman
  python
  python-argcomplete
  python-dbus
  python-gobject
  startup-notification
  systemd-libs
  wayland
  xorg-xwayland
)
makedepends=(
  bash-completion
  gi-docgen
  git
  glib2-devel
  gobject-introspection
  meson
  python-docutils
  sysprof
  wayland-protocols
  cmake
)
source=(
  # Mutter tags use SSH signatures which makepkg doesn't understand
  "git+$url.git#tag=${pkgver}"
  "git+https://gitlab.gnome.org/GNOME/gvdb.git#commit=b54bc5da25127ef416858a3ad92e57159ff565b3"

  5129.patch::https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/5129.patch

  5157.patch::https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/5157.patch

  4921.patch::https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/4921.patch

  5220.patch::https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/5220.patch

  5219.patch::https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/5219.patch
)
b2sums=('e0cfc1c074ff82f7a9a72acb73dfe2a3c95e84b1735afd308a325e9c3ad5b91da3b20f64a842faa45e903eff111a13f75b1ccde4a707c946666ac31000599420'
        'f989bc2ceb52aad3c6a23c439df3bbc672bc11d561a247d19971d30cc85ed5d42295de40f8e55b13404ed32aa44f12307c9f5b470f2e288d1c9c8329255c43bf'
        'aa7daebbc3308e40560ddd891232e4a6aafdf788fd3ac835eff5e04603d69e36a1b841c86c5fc9e2107ff8e90c467ed394ffabe55751068fca2ca18996886386'
        'a8d6401e79f416ee488af71575de3a7fafbfce484183532ebece03a8810538b19e90e4e54e59723e96813a826ee2f9aee34f4e593000f57f3ed706d506068a39'
        'bebaf419a75aba14642fb054f144e2d26c844f14c38abc4ee5034b3a489af28de7e757747ec6cb8220a88d097588e4606af37849003818362ab8130b30c8c578'
        'aa5b88dea0b51a044578a91474c86ca37630e1accbbfef860c17e622549db71caa005c3155d7c40e5b5ea62da7e70163f0059207187c786ab7107e696fa5ffc4'
        '07719d572e015b88fbd6d61087bfadb40c4c99a053d5646645cf4a0df3dcdb47cff526a81d45c0326c4f1f570475bd9ae6c8c6b89872faf38ba782a565ef4693')

prepare() {
  cd mutter

  #git apply ../5133.patch

	echo 5157 # Merged but not yet in 50.beta
	git apply ../5157.patch

	#echo 4921 # FBOs for NVIDIA
	#git apply ../4921.patch

	echo 5129 # defer HW cursor support initialization
	git apply ../5129.patch

	echo 5220 # Fix blur
	git apply ../5220.patch

	echo 5219 # Handle cross GPU buffer scanout
	git apply ../5219.patch
}

build() {
  local meson_options=(
    -D docs=false
    -D installed_tests=false
    -D tests=disabled
  )

  CFLAGS="${CFLAGS/-O2/-O3} -fno-semantic-interposition"
  LDFLAGS+=" -Wl,-Bsymbolic-functions"

  # Inject gvdb
  export MESON_PACKAGE_CACHE_DIR="$srcdir"

  arch-meson mutter build "${meson_options[@]}"
  meson compile -C build
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_mutter-kimiblock-unstable() {
  optdepends=(
    'bash-completion: Bash completions for gdctl'
    'mutter-devkit: Mutter SDK, "MDK"'
  )

  meson install -C build --destdir "$pkgdir"

  _pick devkit "$pkgdir"/usr/lib/mutter-devkit
  _pick devkit "$pkgdir"/usr/share/applications/org.gnome.Mutter.Mdk.desktop
  _pick devkit "$pkgdir"/usr/share/icons/hicolor/scalable/apps/org.gnome.Mutter.Mdk.Devel.svg
  _pick devkit "$pkgdir"/usr/share/icons/hicolor/scalable/apps/org.gnome.Mutter.Mdk.svg
  _pick devkit "$pkgdir"/usr/share/icons/hicolor/symbolic/apps/org.gnome.Mutter.Mdk-symbolic.svg

	provides+=($(ls "${pkgdir}/usr/lib" | grep 'libmutter-' | grep '.so'))
}
