# Contributor: Bart Ribbers <bribbers@disroot.org>
# Contributor: Antoni Aloy <aaloytorrens@gmail.com>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=supertuxkart
pkgver=1.4
pkgrel=0
# s390x mainframes don't have GPUs
arch="all !s390x"
url="https://supertuxkart.net/"
pkgdesc="Kart racing game featuring Tux and his friends"
license="GPL-3.0-or-later"
depends="$pkgname-data"
makedepends="
	bluez-dev
	cmake
	curl-dev
	freetype-dev
	fribidi-dev
	glew-dev
	harfbuzz-dev
	jpeg-dev
	libpng-dev
	libvorbis-dev
	libxkbcommon-dev
	libxrandr-dev
	openal-soft-dev
	openssl-dev>3
	samurai
	sdl2-dev
	sqlite-dev
	wayland-dev
	"
source="https://github.com/supertuxkart/stk-code/releases/download/$pkgver/SuperTuxKart-$pkgver-src.tar.xz
	server_config.xml
	64bittype.patch
	no-install-libs.patch
	gcc-13.patch
	"
subpackages="$pkgname-server $pkgname-data::noarch"
builddir="$srcdir/SuperTuxKart-$pkgver-src"

build() {
	case "$CARCH" in
		arm*|aarch64) GLES2_RENDERER=ON ;;
		* ) GLES2_RENDERER=OFF ;;
	esac

	# Game
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_RECORDER=0 \
		-DUSE_GLES2=$GLES2_RENDERER
	cmake --build build

	# Server only
	cmake -B build-server -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_RECORDER=0 \
		-DSERVER_ONLY=ON
	cmake --build build-server
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build
}

server() {
	depends="$pkgname-data"
	cd "$builddir"
	DESTDIR="$subpkgdir" cmake --install build-server
	mv "$subpkgdir"/usr/bin/supertuxkart \
		"$subpkgdir"/usr/bin/supertuxkart-server

	# Remove desktop app, icons and pixmaps
	# Data is included in data() subpackage
	rm -r "${subpkgdir:?}"/usr/share/

	install -Dm644 "$srcdir"/server_config.xml \
		"$subpkgdir"/usr/share/supertuxkart/data/server_config.xml
}

data() {
	pkgdesc="$pkgdesc (data files)"

	amove usr/share/supertuxkart/data
}

sha512sums="
e88f3fb5ccdb38d0ef75e92d73b43c5cc7a1f6c76ac9502033df099a00d6dc3e86b2c5442b806237d2420c44ada7dd23a2c90ecf94785a44d2348c933770a5ab  SuperTuxKart-1.4-src.tar.xz
104a0b6e6c7abca53b76895d8889edde7745e420e1579c46c63fbfba87c12080f248a1871a92e268f0010ec05ea3d7756072da49784a66156403050f6ec0056e  server_config.xml
75ae2a2a5ab0992cc870fba18f262838f9c2ca04a5a03f2439f77ff352eac43c4fc1338444a53cad40f63341f8eb7543770550f8a17338c1b57f4678a5eb8386  64bittype.patch
7ab08b69a030c8adff17a7bea12509c1119672e65fe645c16b60e91cbdf1f6ab20083c192f4db5290400ba9076b686859b8a7d2ec411552bd7bce1351019f184  no-install-libs.patch
7b5849366263b2b3094ad03c08528c97df25e961ddb6639e2957ec33199039e028e18279c2577283d34381067540cba433b952004323aaa534f797feda7737f2  gcc-13.patch
"
