# Maintainer: Pierre-Loup A. Griffais <pgriffais@valvesoftware.com>

pkgname=gamescope
_srctag=3.16.30
pkgver=${_srctag//-/.}
pkgrel=1
pkgdesc="gaming shell based on Xwayland, powered by Vulkan and DRM"
arch=(x86_64)
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')
makedepends=(openssh git meson cmake wayland-protocols ninja glslang glm vulkan-headers benchmark catch2)
source=("galileo-mura-setup.service"
        "gamescope-session"
        "gamescope-wayland.desktop"
        "gamescope-mimeapps.list"
        "gamescope-session.service"
        "gamescope-session.target"
        "gamescope-portals.conf"
        "gamescope-mangoapp.service"
        "ibus-gamescope.service"
        "start-gamescope-session"
        "steam-launcher"
        "steam-launcher.service"
        "steam-notif-daemon.service"
        "steam-short-session-tracker"
        "steam_http_loader.desktop"
        "steam-http-loader"
        "git+https://github.com/ValveSoftware/gamescope.git#tag=$_srctag"
        "git+https://gitlab.freedesktop.org/wlroots/wlroots.git#tag=0.20.2"
        "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")
sha256sums=('2cfacf10f311a02d1c94ab5e927094639dfbb57bd322acc628374fef048a12bb'
            'da4e61a38edc3568229a8def2bcf24480db80d85c3507ea6a7d73d774f639abb'
            'fe515fce8f151a6c03a89e043044bfddf8cd6ee89027d2cfbcf6f6706c78ca76'
            'e37ba6107f3a84cf47c2799b537a88583e6cb8951167a9c6a48fa1d85996206b'
            '78f9bb2a97ff5cb08e5e648bed9984a6ffba68a7fff9d214b0d28a532ee66601'
            '6fbac560ef24747c3e45621e0dc188fc031418a2f60dde9a1cd0efeb0c3827c5'
            'eae6b5cbd0735d6732c8b7de99ed3362f24f6aa76bbba0950a14b99040f4e6d0'
            '81da49a733a757a4848a5c95011c3f9d3e584373244722ed9b26687ae0126793'
            'd010a36f52f2122b51f8a0bdc983560a553759f85c2eb5a9ccb2b1a6de367059'
            '10462d319e9d2a97a8fc9265628c03819bd6fd86dc13cee6e38151fc9b270407'
            'bc2e16bbff2357091f04b7049b9fa40d3fc8d75e909c09310ebe39acc5c09621'
            'db9a4802fc1e4829130e3a69b42b836cfabf81021c0e9dcb1a2787d875f34201'
            '609da4097f9addc83f07647cc6e5c75493ddc8372853868c033d1c71c5c0cce9'
            '674367927b9d0665a1e1c57ebcd7373683659b1dc1020d2bffd4ab50e4af73fa'
            '525060896abef2da9db8d8294253b7444d60e48cf6cc0496ca48fc7084cc8590'
            'f57fcba25b211381a9402d3d0c4723301afaef185e9ecffc75839e5af2aee4c8'
            '674db3b4b9399279e38de7c334bda89362a125a27f0d054c2f92287c4884648f'
            '6c8595da579a8df74bc307294c2485e90f5379b84390381f500da33ff3f276d2'
            'SKIP'
            '03726f2fb44ae79e6a398e8f9aaaf8054800dda9b8298726157522fe5f7296b1'
            'e39e0c91b297bfd707afcda84ecdc15a08c22e2ad4c347fc3533b1ed98fb3f85'
            '85f3e55fb527e9853bd4fc31300253587198831660a743eaa55f6b40fe1b9c90')

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
}

build() {
	cd "$pkgname"
	rm -rf build
	arch-meson --buildtype release --prefix /usr build
	meson compile -C build
}

check() {
	cd "$pkgname"
	meson test -C build --suite gamescope -v
}

package() {
	install -D -m 755 gamescope-session "$pkgdir"/usr/lib/steamos/gamescope-session
	install -D -m 755 steam-launcher "$pkgdir"/usr/lib/steamos/steam-launcher
	install -D -m 755 steam-short-session-tracker "$pkgdir"/usr/lib/steamos/steam-short-session-tracker

	install -D -m 755 start-gamescope-session "$pkgdir"/usr/bin/start-gamescope-session
	install -D -m 644 gamescope-wayland.desktop "$pkgdir"/usr/share/wayland-sessions/gamescope-wayland.desktop

	# url handling
	install -D -m 644 steam_http_loader.desktop "$pkgdir"/usr/share/applications/steam_http_loader.desktop
	install -D -m 644 gamescope-mimeapps.list "$pkgdir"/usr/share/applications/gamescope-mimeapps.list
	install -D -m 755 steam-http-loader "$pkgdir"/usr/bin/steam-http-loader

	# systemd
	install -D -m 644 galileo-mura-setup.service "$pkgdir"/usr/lib/systemd/user/galileo-mura-setup.service
	install -D -m 644 gamescope-session.service "$pkgdir"/usr/lib/systemd/user/gamescope-session.service
	install -D -m 644 gamescope-session.target "$pkgdir"/usr/lib/systemd/user/gamescope-session.target
	install -D -m 644 gamescope-mangoapp.service "$pkgdir"/usr/lib/systemd/user/gamescope-mangoapp.service
	install -D -m 644 ibus-gamescope.service  "$pkgdir"/usr/lib/systemd/user/ibus-gamescope.service
	install -D -m 644 steam-launcher.service "$pkgdir"/usr/lib/systemd/user/steam-launcher.service
	install -D -m 644 steam-notif-daemon.service "$pkgdir"/usr/lib/systemd/user/steam-notif-daemon.service

	# portals
	install -D -m 644 gamescope-portals.conf "$pkgdir"/usr/share/xdg-desktop-portal/gamescope-portals/gamescope-portals.conf

	install -d "$pkgdir"/usr/share/gamescope/reshade
	cp -r "$srcdir"/GamescopeShaders/* "$pkgdir"/usr/share/gamescope/reshade/
	chmod -R 755 "$pkgdir"/usr/share/gamescope

	cd "$pkgname"

	meson install -C build --destdir "$pkgdir" --skip-subprojects
}
