# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License V2

EAPI=8

inherit desktop

DESCRIPTION="Voxel sandbox game with a large render distance, procedurally generated content and some cool graphical effects."
HOMEPAGE="https://github.com/PixelGuys/Cubyz"
SRC_URI="
	amd64? ( https://github.com/PixelGuys/Cubyz/releases/download/${PV}/Linux-x86_64.tar.gz -> ${PN}-x86_64-${PV}.tar.gz )
	arm64? ( https://github.com/PixelGuys/Cubyz/releases/download/${PV}/Linux-aarch64.tar.gz -> ${PN}-aarch64-${PV}.tar.gz )
"

S="${WORKDIR}"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="amd64 arm64"

RDEPEND="
    media-libs/openal
    media-libs/libpng:0=
    net-misc/curl
    virtual/opengl
    x11-libs/libX11
    x11-libs/libXi
"
DEPEND=""

src_install() {
	insinto /opt/cubyz
	doins -r *

	local exe_path=$(find "${D}/opt/cubyz" -type f -name "Cubyz" ! -name "*.png" ! -name "*.zon" | head -n 1)
	if [[ -n "${exe_path}" ]]; then
		chmod +x "${exe_path}"
		local rel_path="${exe_path#${D}/opt/cubyz/}"
		local bin_dir=$(dirname "/opt/cubyz/${rel_path}")
		
		cat <<EOF > "${T}/cubyz"
#!/bin/sh
USER_DIR="\${XDG_DATA_HOME:-\$HOME/.local/share}/cubyz"
mkdir -p "\$USER_DIR/logs"

if [ ! -f "\$USER_DIR/launchConfig.zon" ] && [ -f "${bin_dir}/launchConfig.zon" ]; then
    cp "${bin_dir}/launchConfig.zon" "\$USER_DIR/"
fi

if [ ! -e "\$USER_DIR/assets" ] && [ -d "${bin_dir}/assets" ]; then
    ln -s "${bin_dir}/assets" "\$USER_DIR/assets"
fi

cd "\$USER_DIR" || exit 1
exec "${bin_dir}/Cubyz" "\$@"
EOF
		dobin "${T}/cubyz"
	fi

	local icon_path=$(find "${D}/opt/cubyz" -type f -name "logo.png" | head -n 1)
	if [[ -n "${icon_path}" ]]; then
		newicon -s 128 "${icon_path}" cubyz.png
	fi

	make_desktop_entry cubyz "Cubyz" cubyz "Game;ActionGame;"
}