# Copyright 2017-2021 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# Auto-Generated by cargo-ebuild 0.3.1

EAPI=8

CRATES=""

inherit cargo systemd

DESCRIPTION="An open source client library for Spotify, with support for Spotify Connect"
# Double check the homepage as the cargo_metadata crate
# does not provide this value so instead repository is used
HOMEPAGE="https://github.com/librespot-org/librespot"
if [[ ${PV} == 9999 ]] ; then
	inherit git-r3
	EGIT_REPO_URI="https://github.com/librespot-org/librespot.git"
	EGIT_BRANCH="dev"
	SRC_URI="${CARGO_CRATE_URIS}"
else
	SRC_URI="https://github.com/librespot-org/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${PN}-${PV}.tar.gz
		https://github.com/Tatsh/tatsh-overlay/releases/download/__distfiles__/${P}-crates.tar.xz"
	KEYWORDS="~amd64 ~arm64"
fi

# License set may be more restrictive as OR is not respected
# use cargo-license for a more accurate license picture
LICENSE="MIT"
SLOT="0"
IUSE="alsa pulseaudio portaudio jack sdl"
RESTRICT="mirror"

get_features() {
	local out=( )
	if use alsa; then
   		out+=( "alsa-backend" )
	fi
	if use pulseaudio; then
		out+=( "pulseaudio-backend" )
	fi
	if use portaudio; then
		out+=( "portaudio-backend" )
	fi
	if use jack; then
		out+=( "jackaudio-backend" )
	fi
	if use sdl; then
		out+=( "sdl-backend" )
	fi

	printf "%s," "${out[@]}" | sed 's/,$//g'
}

DEPEND="
	media-libs/alsa-lib
	pulseaudio? ( media-libs/libpulse )
	portaudio? ( media-libs/portaudio )
	jack? ( virtual/jack )
	sdl? ( media-libs/libsdl2 )
"

src_unpack() {
	if [[ ${PV} == 9999 ]] ; then
		git-r3_src_unpack
		cargo_live_src_unpack
	else
		cargo_src_unpack
	fi
}

src_compile() {
	debug-print-function "${FUNCNAME[*]}" "$@"

	local features
	features="$(get_features)"
	if [[ "${features}" ]]; then
		cargo_src_compile --features "${features}"
	else
		cargo_src_compile
	fi
}

src_install() {
	debug-print-function "${FUNCNAME[*]}" "$@"

	local features
	features="$(get_features)"
	if [[ "${features}" ]]; then
		cargo_src_install --features "${features}"
	else
		cargo_src_install
	fi

	systemd_dounit "${FILESDIR}"/"${PN}".service
	systemd_install_serviced "${FILESDIR}"/"${PN}".service.conf
}