# Maintainer: Developer <dev@opalos.org>
# Maintainer: Developer <dev@opalos.org>

pkgbase=gtk4
pkgname=(
  gtk4
  gtk4-demos
  gtk4-docs
  gtk-update-icon-cache
)
pkgver=%version%
pkgrel=%release%
epoch=1
pkgdesc="GObject-based multi-platform GUI toolkit"
url="https://www.gtk.org/"
arch=(x86_64)
license=(LGPL-2.0-only)
depends=(
  adwaita-icon-theme
  cairo
  cantarell-fonts
  dconf
  desktop-file-utils
  fontconfig
  fribidi
  gdk-pixbuf2
  glib2
  graphene
  gst-plugins-bad-libs
  harfbuzz
  iso-codes
  libcloudproviders
  libcolord
  libcups
  libegl
  libepoxy
  libgl
  libjpeg
  libpng
  librsvg
  libtiff
  libx11
  libxcursor
  libxdamage
  libxext
  libxfixes
  libxi
  libxinerama
  libxkbcommon
  libxrandr
  libxrender
  pango
  shared-mime-info
  tracker3
  wayland
)
makedepends=(
  docbook-xsl
  gi-docgen
  git
  gobject-introspection
  meson
  python-docutils
  python-gobject
  sassc
  shaderc
  vulkan-headers
  wayland-protocols
)
checkdepends=(
  gst-plugin-pipewire
  gst-plugins-base
  mutter
  noto-fonts
  pipewire
  python-pydbus
  vulkan-swrast
  weston
  wireplumber
)
source=(
  "git+https://gitlab.gnome.org/GNOME/gtk.git#tag=$pkgver"
  gtk-update-icon-cache.{hook,script}
  gtk4-querymodules.{hook,script}
  0001-HACK-Don-t-use-objcopy-for-resource-embedding.patch
)
b2sums=('4e7ddb5200d45c99479adce9541fe088edb99e29400f6d0d6c06a0b3959767ec90252ced37f987cdf5d5ae727d88c27d20711eabf601ee68b63878ce5d56bfaa'
        '136bdb410c46daf769175e8e8837286576391797a4762b8cf388217e893dd6c5087c5c91c347cbdf7d3e9dcd2c978c2fb275b5af1f3425c9f7979fbc65a81324'
        '6bcd839ef82296d864587e0cc7acc0145bdea8e5235af304747cf3c0e564c2757cc67c0373dc044bec83dccfc57dc899546c2fccea96cff2bba22f09978a3814'
        'dd589bd1ad2b13f0e06f6899776a083f20a1aac24d4308d666ffd0d1cff38457b8257b8366f92e767b4233b3d86b6b54fa50339faf84c4801a824986366dce30'
        '4b90eb8d582509b09aab401313d4399cc139ad21b5dd7d45d79860d0764c7494c60714e0794e09823e51d1894ac032a994f27d79d1499abf24ee6f59bdb0c243'
        'ace2f8be09d5e20c3a694e6a9a397d8d87b61516601a96d998e89f7500874e98fee380b66c44fd5c8a717d3d89f99026e42f79df18a7a32feac724044e5b7341')

prepare() {
  cd gtk

  # Allow -fcf-protection to work
  # https://gitlab.gnome.org/GNOME/gtk/-/issues/6153
  git apply -3 ../0001-HACK-Don-t-use-objcopy-for-resource-embedding.patch
}

build() {
  local meson_options=(
    -D broadway-backend=true
    -D cloudproviders=enabled
    -D colord=enabled
    -D documentation=true
    -D man-pages=true
    -D tracker=enabled
  )

  CFLAGS+=" -DG_DISABLE_CAST_CHECKS"
  arch-meson gtk build "${meson_options[@]}"
  meson compile -C build
}

check() (
  export XDG_RUNTIME_DIR="$PWD/runtime-dir" WAYLAND_DISPLAY=wayland-5

  mkdir -p -m 700 "$XDG_RUNTIME_DIR"
  weston --backend=headless-backend.so --socket=$WAYLAND_DISPLAY --idle-time=0 &
  _w=$!

  trap "kill $_w; wait" EXIT

  # Test: GTK / templates fails - ignore it for now.
  sed -i "s/{ 'name': 'templates' }/#{ 'name': 'templates' }/g" gtk/testsuite/gtk/meson.build

  # Comparison tests fail, because thes depend on driver and how mesa was build
  # https://gitlab.gnome.org/GNOME/gtk/-/issues/6383
  meson test -C build --print-errorlogs --no-suite=headless --timeout-multiplier=3 ||:
)

_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_gtk4() {
  depends+=(
    bash
    glibc
    gst-plugins-base-libs
    gstreamer
    gtk-update-icon-cache
    vulkan-icd-loader
    )
  optdepends=('evince: Default print preview command')
  provides=(libgtk-4.so)

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

  install -Dm644 /dev/stdin "$pkgdir/usr/share/gtk-4.0/settings.ini" <<END
[Settings]
gtk-icon-theme-name = Adwaita
gtk-theme-name = Adwaita
gtk-font-name = Cantarell 11
END

  install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 gtk4-querymodules.hook
  install -D gtk4-querymodules.script "$pkgdir/usr/share/libalpm/scripts/gtk4-querymodules"

  cd "$pkgdir"

  _pick demo usr/bin/gtk4-{demo,demo-application,icon-browser,node-editor,print-editor,widget-factory}
  _pick demo usr/share/applications/org.gtk.{Demo4,IconBrowser4,PrintEditor4,WidgetFactory4,gtk4.NodeEditor}.desktop
  _pick demo usr/share/glib-2.0/schemas/org.gtk.Demo4.gschema.xml
  _pick demo usr/share/icons/hicolor/*/apps/org.gtk.{Demo4,IconBrowser4,PrintEditor4,WidgetFactory4,gtk4.NodeEditor}[-.]*
  _pick demo usr/share/man/man1/gtk4-{demo,demo-application,icon-browser,node-editor,widget-factory}.1
  _pick demo usr/share/metainfo/org.gtk.{Demo4,IconBrowser4,PrintEditor4,WidgetFactory4,gtk4.NodeEditor}.appdata.xml

  _pick docs usr/share/doc

  # Built by GTK 4, shared with GTK 3
  _pick guic usr/bin/gtk4-update-icon-cache
  _pick guic usr/share/man/man1/gtk4-update-icon-cache.1
}

package_gtk4-demos() {
  pkgdesc+=" (demo applications)"
  depends=(
    cairo
    dconf
    gdk-pixbuf2
    glib2
    glibc
    graphene
    gtk4
    harfbuzz
    hicolor-icon-theme
    libepoxy
    librsvg
    pango
    )
  mv demo/* "$pkgdir"
}

package_gtk4-docs() {
  pkgdesc+=" (documentation)"
  depends=()
  mv docs/* "$pkgdir"
}

package_gtk-update-icon-cache() {
  pkgdesc="GTK icon cache updater"
  depends=(
    bash
    gdk-pixbuf2
    glib2
    glibc
    hicolor-icon-theme
    librsvg
    )

  mv guic/* "$pkgdir"
  ln -s gtk4-update-icon-cache "$pkgdir/usr/bin/gtk-update-icon-cache"
  ln -s gtk4-update-icon-cache.1 "$pkgdir/usr/share/man/man1/gtk-update-icon-cache.1"

  install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 gtk-update-icon-cache.hook
  install -D gtk-update-icon-cache.script "$pkgdir/usr/share/libalpm/scripts/gtk-update-icon-cache"
}

# vim:set sw=2 sts=-1 et:
