# Maintainer: unom <packages@unom.io>
#
# gamescope + punktfunk's `pipewire-hdr` patches, installed as `punktfunk-gamescope`.
#
# Standalone on purpose (its own pkgbase, not part of ../arch/PKGBUILD): it is an OPTIONAL
# companion — punktfunk works without it, just SDR on the gamescope backend — and it is a C++
# meson build with a completely different dependency set from the Rust workspace. Installing it
# does not touch the distro's own `gamescope`; the host resolves `PUNKTFUNK_GAMESCOPE_BIN` >
# `punktfunk-gamescope` > `gamescope`, so the mere presence of this package is what enables HDR.
#
#   makepkg -si
#
# See ../README.md (packaging/gamescope/README.md) for what the patches do and why.
pkgname=punktfunk-gamescope
# `_gsver` is the upstream gamescope this is built from; `pkgrel` moves when only our patches do.
_gsver=3.16.25
_gsrev=8c676c399c761e4540587f61004c957993d12fea
pkgver="${_gsver}.pfhdr1"
pkgrel=1
pkgdesc="gamescope with 10-bit BT.2020/PQ PipeWire capture, for punktfunk HDR streaming"
arch=('x86_64' 'aarch64')
url="https://git.unom.io/unom/punktfunk"
license=('BSD-2-Clause')
# NOT a `provides`/`conflicts` of gamescope: this ships a differently-named binary and is meant to
# sit BESIDE the distro package (a Steam Deck's gaming session keeps using its own gamescope).
optdepends=('punktfunk-host: the streaming host that uses it')
# Mirrors Arch's own `gamescope` package. Keep in step with it when that one moves — this builds
# the same tree, so a dependency it grows is one this needs too.
depends=('glslang' 'libcap' 'libdrm' 'libinput' 'libx11' 'libxcomposite' 'libxdamage'
         'libxext' 'libxkbcommon' 'libxmu' 'libxrender' 'libxres' 'libxtst' 'libxxf86vm'
         'libavif' 'libdecor' 'hwdata' 'luajit' 'pipewire' 'seatd' 'sdl2-compat'
         'vulkan-icd-loader' 'wayland' 'xcb-util-errors' 'xcb-util-wm' 'xorg-xwayland')
makedepends=('git' 'meson' 'ninja' 'cmake' 'glm' 'vulkan-headers' 'wayland-protocols'
             'benchmark' 'libxcursor')
# The submodules are pinned by the rev, so the git source is reproducible; `#commit=` makes
# makepkg check it out and `--recurse-submodules` pulls wlroots/vkroots/libliftoff/… with it.
source=("git+https://github.com/ValveSoftware/gamescope.git#commit=${_gsrev}"
        '0001-pipewire-offer-10-bit-BT.2020-PQ-capture-formats-HDR.patch'
        '0002-punktfunk-stamp-the-version-banner-with-pfhdr1.patch')
# SKIP on the git source: the commit hash above IS the integrity check.
sha256sums=('SKIP' 'SKIP' 'SKIP')
options=('!lto' '!debug')
install="${pkgname}.install"

prepare() {
  cd gamescope
  git submodule update --init --recursive --depth 1
  # `git am` (not `patch`): the patches are format-patch output, and applying them as commits keeps
  # `git describe --dirty` from stamping a `+` into the version banner on top of our `+pfhdr1`.
  git -c user.name=punktfunk -c user.email=packages@unom.io \
      am "${srcdir}"/000*.patch
}

build() {
  arch-meson gamescope build \
    --buildtype=release \
    -Dpipewire=enabled
  meson compile -C build
}

package() {
  # ONLY the compositor, under our own name: a full `meson install` would also lay down
  # gamescopectl / gamescopereaper / gamescopestream and the WSI layer, colliding file-for-file
  # with the distro's `gamescope` package. The host only ever execs the compositor.
  install -Dm755 build/src/gamescope "${pkgdir}/usr/bin/punktfunk-gamescope"
  install -Dm644 gamescope/LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
