# Copyright 2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

inherit wrapper

MY_PN="${PN%-bin}"
MY_P="${MY_PN}-${PV}"
S="${WORKDIR}/${MY_PN}"

DESCRIPTION="A small, fast and powerful modular synthesizer/tracker"
HOMEPAGE="http://www.warmplace.ru/soft/sunvox/"
SRC_URI="http://www.warmplace.ru/soft/sunvox/${MY_P}.zip"

LICENSE="freedist"
SLOT="0"
KEYWORDS="-* ~amd64 ~x86"   # there are arm and arm64 builds too, but I can't test those
IUSE="examples opengl abi_x86_32 abi_x86_64"

RESTRICT="mirror"

QA_PREBUILT="opt/sunvox/bin/sunvox*"

archesdepend="abi_x86_32(-)?,abi_x86_64(-)?"

SDL_DEPEND="=media-libs/libsdl2-2*[X,video,${archesdepend}]"
RDEPEND="
	media-libs/alsa-lib[${archesdepend}]
	x11-libs/libX11[${archesdepend}]
	opengl? (
		abi_x86_64? (
			media-libs/libglvnd[${archesdepend}]
			x11-libs/libXext[${archesdepend}]
			x11-libs/libXi[${archesdepend}]
		)
	)
	!opengl? ( ${SDL_DEPEND} )
	abi_x86_32? ( ${SDL_DEPEND} )"

src_install() {
	local dir="/opt/sunvox"
	local arches=( )

	exeinto "${dir}/bin"
	insinto "${dir}"
	if use abi_x86_32; then
		if use opengl; then
			ewarn "OpenGL is not supported for x86_32; installing regular version instead"
		fi
		newexe sunvox/linux_x86/sunvox sunvox32
		arches+=('32')
	fi
	if use abi_x86_64; then
		if use opengl; then
			newexe sunvox/linux_x86_64/sunvox_opengl sunvox64_opengl
			arches+=('64_opengl')
		else
			newexe sunvox/linux_x86_64/sunvox sunvox64
			arches+=('64')
		fi
	fi
	if [[ "${#arches[@]}" == "1" ]]; then
		make_wrapper "${MY_PN}" "${dir}/bin/sunvox${arches[0]}"
	else
		local arch
		for arch in "${arches[@]}"; do
			make_wrapper "${MY_PN}${arch}" "${dir}/bin/sunvox${arch}"
		done
	fi

	if use examples; then
		doins -r effects
		doins -r examples
		doins -r instruments
		doins -r curves
	fi

	dodoc -r docs/.
}