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

EAPI=8

inherit desktop xdg-utils

DESCRIPTION="A free-to-win rhythm game (Upstream binary builds)"
HOMEPAGE="https://osu.ppy.sh/
	https://github.com/ppy/osu/"

# 2 conditions must be true before this can be uncommented:
# - this is the tachyon ebuild
# - there is a tachyon release newer than the latest stable (lazer) release
#
# uncommenting this will use tachyon tagged stuff
#tachyon_release="true"

if [ -n "$tachyon_release" ]; then
	# use ${PV} with manual ${PN} for consistency with below
	SRC_URI="
		https://github.com/ppy/osu/releases/download/${PV}-tachyon/osu.AppImage
			-> osu-lazer-tachyon-bin-${PV}.AppImage
	"
else
	# match filename with lazer ebuild to reduce duplication
	SRC_URI="
		https://github.com/ppy/osu/releases/download/${PV}-lazer/osu.AppImage
			-> osu-lazer-bin-${PV}.AppImage
	"
fi

# "all-rights-reserved" - ships a copy of proprietary BASS lib - https://www.un4seen.com
LICENSE="Apache-2.0 BSD-2 LGPL-2.1 LGPL-3+ MIT all-rights-reserved"
# https://github.com/ppy/osu-resources
# CC BY-NC 4.0 + fonts
# https://github.com/ppy/osu-resources/blob/master/osu.Game.Resources/Fonts/Inter/OFL.txt
# https://github.com/ppy/osu-resources/blob/master/osu.Game.Resources/Fonts/Noto/LICENSE.txt - Apache 2.0
# https://github.com/ppy/osu-resources/blob/master/osu.Game.Resources/Fonts/Torus-Alternate/LICENCE - commercial license for distribution
# https://github.com/ppy/osu-resources/blob/master/osu.Game.Resources/Fonts/Torus/LICENCE           - commercial license for distribution
# https://github.com/ppy/osu-resources/blob/master/osu.Game.Resources/Fonts/Venera/LICENCE          - commercial license for distribution
LICENSE+=" CC-BY-NC-4.0 OFL-1.1 Apache-2.0 all-rights-reserved"

SLOT="0"
KEYWORDS="-* ~amd64"
IUSE="force-wayland"
# from ebuildtester
RDEPEND="dev-libs/icu"

# do not allow simultaneous installation of non-tachyon and tachyon
# releases. while it's possible to have both, usage flow hasn't been thought
# out, and constantly switching versions by accident (via desktop
# environment search picking things differently sometimes) could cause
# issues.
RDEPEND="${RDEPEND} !games-arcade/osu-lazer-tachyon-bin"

# because redistribution is not allowed without a commercial license, binary
# packages should not exist either
RESTRICT="bindist"
# also it's an upstream binary we don't have control over, so don't bother
RESTRICT+=" binchecks"
# let's also not touch the binaries
RESTRICT+=" strip"
# not source code-based ebuild
RESTRICT+=" test"
# non-redistributable things too, also no mirrors exist for this repo
RESTRICT+=" mirror"

# TODO: mimetype icons

unsupported_warning() {
	elog "AppImages provided by osu!lazer developers can be found at"
	if [ -n "$tachyon_release" ]; then
		elog "https://github.com/ppy/osu/releases" # github has no pre-release static direct link AFAIK
	else
		elog "https://github.com/ppy/osu/releases/latest"
	fi
	elog ""
	elog "If you're sure or unsure whether an issue is caused by this ebuild, please"
	elog "report it to https://todo.sr.ht/~jacksonchen666/gentoo-overlay or"
	elog "https://codeberg.org/jacksonchen666/gentoo-overlay/issues"
	if use force-wayland; then
		elog ""
		ewarn "You are forcing osu!lazer to use Wayland instead of X11. If you have issues with"
		ewarn "input handling, window sizing, or more, it's highly recommended to test without"
		ewarn "the force-wayland USE flag enabled."
	fi
	if [ -n "$tachyon_release" ]; then
		elog ""
		ewarn "A Tachyon release is being installed. This can potentially be unstable."
	fi
}

pkg_pretend() {
	unsupported_warning
}

src_unpack() {
	if [ -n "$tachyon_release" ]; then
		cp "${DISTDIR}/${P}.AppImage" "${WORKDIR}" || \
			die "can't copy sources"
	else
		# has lazer distfile name compatibility (to reduce duplication)
		cp "${DISTDIR}/osu-lazer-bin-${PV}.AppImage" "${WORKDIR}/${P}.AppImage" || \
			die "can't copy sources"
	fi

	chmod +x "${WORKDIR}/${P}.AppImage" || die "couldn't make appimage executable"
	"${WORKDIR}/${P}.AppImage" --appimage-extract || die "couldn't extract appimage"
	mv squashfs-root "${S}" || die "couldn't rename extracted"
}

src_compile() {
	#cd "${S}" || die
	mkdir opt/ || die
	mv "usr/bin" "opt/${P}" || die

	# rename for clarity
	sed -i 's~^Name=.*~Name=osu!(lazer)~' osu\!.desktop || die "sed for .desktop name failed"

	# wrapper
	# probably not supported by devs
	if use force-wayland; then
		cp "${FILESDIR}/wayland-osu-lazer-bin" "./osu-lazer-bin" || die "cp failed"
	else
		cp "${FILESDIR}/osu-lazer-bin" . || die "cp failed"
	fi
	sed -i -e "s/%P%/${P}/" "osu-lazer-bin" || die "sed for wrapper failed"
	chmod +x osu-lazer-bin || die "chmod failed"
	sed -i "s~^Exec=.*~Exec=/usr/bin/osu-lazer-bin %u~" osu\!.desktop || die "sed for .desktop exec failed"
}

src_install() {
	# wrapper
	dobin osu-lazer-bin

	# actual binaries and libraries and etc.
	insinto /usr/share/icons/
	doins -r "usr/share/icons/hicolor/"
	insinto /opt/
	doins -r "opt/${P}/"

	# does not match upstream with a bunch of executable .dll files (some
	# excluded), which is probably done by dotnet
	fperms 0755 "/opt/${P}/osu!"

	domenu osu\!.desktop
}

pkg_postinst() {
	unsupported_warning
	xdg_icon_cache_update
	xdg_desktop_database_update
}

pkg_postrm() {
	xdg_icon_cache_update
	xdg_desktop_database_update
}