# This so that our gamescope builds always happen in lockstep
source ../gamescope/gamescope_common.pkgbuild

pkgname=gamescope
pkgver=${_pkgver:-1}
pkgrel="${_pkgrel:-1}"
pkgdesc="gaming shell based on Xwayland, powered by Vulkan and DRM"
arch=(x86_64 aarch64)
url="https://github.com/ValveSoftware/gamescope"
license=('MIT')
depends=('xorg-xwayland' 'libavif' 'aom' 'rav1e' 'libxres' 'xcb-util-errors' 'freerdp' 'xcb-util-wm' 'libxcomposite' 'pixman' 'libinput' 'seatd' 'pipewire' 'libxmu' 'libxcursor' 'libdecor' 'libei' 'luajit' 'libdrm')
makedepends=(openssh git meson cmake wayland-protocols ninja glslang glm vulkan-headers benchmark)
source=("git+https://github.com/ValveSoftware/gamescope.git#commit=${_commit}"
        "git+https://gitlab.freedesktop.org/wlroots/wlroots.git"
        "git+https://gitlab.freedesktop.org/emersion/libliftoff.git"
        "git+https://github.com/Joshua-Ashton/GamescopeShaders.git#tag=v0.1"
        # FIXME Upstream gamescope is just selecting master branch at build time, so we are arbitrarily snapshotting a
        #       revision when bumping the version here such that the build is reproducible.
        "git+https://github.com/nothings/stb.git#commit=af1a5bc352164740c1cc1354942b1c6b72eacb8a"
        "git+https://github.com/g-truc/glm.git#commit=0af55ccecd98d4e5a8d1fad7de25ba429d60e863"
		'steamos-session-shell')
sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            '03726f2fb44ae79e6a398e8f9aaaf8054800dda9b8298726157522fe5f7296b1'
            'e39e0c91b297bfd707afcda84ecdc15a08c22e2ad4c347fc3533b1ed98fb3f85'
            '85f3e55fb527e9853bd4fc31300253587198831660a743eaa55f6b40fe1b9c90'
            'a34b30f8c58881f29a632ae1b4c8d7fbd7ba8c1ae6509d2b85afe362cad6de93')

install=gamescope.install

prepare() {
	cd "$pkgname"

	# git submodules
	git submodule init
	git config submodule.subprojects/wlroots.url "$srcdir/wlroots"
	git config submodule.subprojects/libliftoff.url "$srcdir/libliftoff"
	git -c protocol.file.allow=always submodule update

	# meson subprojects
	rm -rf subprojects/stb
	git clone "$srcdir/stb" subprojects/stb
	cp -av subprojects/packagefiles/stb/* subprojects/stb/ # patch from the .wrap we elided

	rm -rf subprojects/glm
	git clone "$srcdir/glm" subprojects/glm
	cp -av subprojects/packagefiles/glm/* subprojects/glm/ # patch from the .wrap we elided

	cd "${srcdir}/gamescope"
}

build() {
	cd "$pkgname"

	rm -rf build
	mkdir build
	cd build
	arch-meson --buildtype release --prefix /usr -Denable_tests=false -Denable_zenity=false ..
	ninja
}

package() {
	install -d "$pkgdir"/usr/share/gamescope/reshade
	cp -r "$srcdir"/GamescopeShaders/* "$pkgdir"/usr/share/gamescope/reshade/
	chmod -R 655 "$pkgdir"/usr/share/gamescope

	cd "$pkgname/build"

	DESTDIR="$pkgdir" meson install --skip-subprojects --tags runtime

	install -TDm755 "$srcdir"/steamos-session-shell "$pkgdir/usr/bin/steamos-session-shell"
}
