# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit cmake desktop xdg

DESCRIPTION="Advanced cross-platform rhythm game focused on keyboard play"
HOMEPAGE="https://etternaonline.com"
SRC_URI="https://github.com/etternagame/etterna/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

S="${WORKDIR}/etterna-${PV}"
LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="alsa jack pulseaudio"

RDEPEND="
dev-libs/openssl:=
media-libs/libglvnd
media-libs/libogg
net-misc/curl
x11-libs/libX11
x11-libs/libXrandr
alsa? ( media-libs/alsa-lib )
jack? ( virtual/jack )
pulseaudio? ( media-libs/libpulse )
"

DEPEND="${RDEPEND}"

src_prepare() {
	default

	sed -i \
		-e 's/\(set(OPENSSL_USE_STATIC_LIBS\) ON/\1 OFF/' \
		CMakeLists.txt || die

	sed -i \
		-e 's/if (ANDROID AND (\${CMAKE_SYSTEM_VERSION} LESS 21))/if (ANDROID)/' \
		extern/luajit/src/CMakeLists.txt || die

	sed -i \
		-e '1i #include <stdlib.h>' \
		extern/mad-0.15.1b/global.h || die

	cmake_src_prepare
}

src_configure() {
	local mycmakeargs=(
		-DWITH_CRASHPAD=OFF
	)
	cmake_src_configure
}

src_install() {
	local bin
	local dir
	bin=$(find "${S}" \
		-maxdepth 1 \
		-type f \
		-name 'Etterna*' \
		-perm -111 \
		-print -quit) || die
	[[ -n ${bin} ]] || die "Unable to locate Etterna executable"
	exeinto /opt/etterna
	newexe "${bin}" Etterna
	insinto /opt/etterna
	for dir in \
		Announcers \
		Assets \
		BackgroundEffects \
		BackgroundTransitions \
		BGAnimations \
		Data \
		NoteSkins \
		Scripts \
		Songs \
		Themes
do
	cp -a "${S}/${dir}" "${T}/Default_${dir}" || die
	doins -r "${T}/Default_${dir}"
done
dobin "${FILESDIR}/etterna"
newicon \
	"${S}/Themes/_fallback/Graphics/Common window icon.png" \
	etterna.png
make_desktop_entry \
	etterna \
	Etterna \
	etterna \
	"Game;ArcadeGame;"
}

pkg_postinst() {
	xdg_pkg_postinst
	elog "Etterna stores user data in ~/.etterna."
	elog "Default assets are copied from /opt/etterna"
	elog "to ~/.etterna the first time the launcher is started."
}

pkg_postrm() {
	xdg_pkg_postrm
}