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

# shellcheck disable=SC2034
EAPI=8

inherit desktop xdg-utils

DESCRIPTION="Rename your TV-Series using TheTVDB (QT5 version)"
HOMEPAGE="https://www.tweaking4all.com/home-theatre/rename-my-tv-series-v2"

PN_PRE="${PN%-bin}"
PN_POST="${PN_PRE#*-}"
PN_PRE="${PN_PRE%-*}"

MY_PN="RenameMyTVSeries"
MY_PN_POST="${PN_POST^^}"

if [[ "${PV}" =~ beta ]]; then
	SRC_URI="https://www.tweaking4all.com/downloads/betas/${MY_PN}-$(ver_cut 1-3)-${MY_PN_POST}-beta-Linux-64bit-shared-ffmpeg.tar.gz -> ${P}.tar.gz"
else
	SRC_URI="https://www.tweaking4all.com/downloads/${MY_PN}-${PV}-${MY_PN_POST}-Linux-64bit-shared-ffmpeg.tar.gz -> ${P}.tar.gz"
fi

LICENSE="RenameMyTVSeries"
SLOT="0"
KEYWORDS="-* ~amd64"

RDEPEND="
	!media-video/renamemytvseries-gtk-bin
	dev-db/sqlite:3
	dev-libs/glib:2
	dev-libs/qt5pas
	media-video/ffmpeg
	x11-libs/gdk-pixbuf:2[introspection]
	x11-libs/libnotify[introspection]
	x11-libs/libX11"

QA_PREBUILT="opt/${PN_PRE}/${PN_PRE}"

S="${WORKDIR}"

src_prepare() {
	local exe_path

	eapply_user

	exe_path="${S}/${MY_PN}"
	patchelf --set-rpath "./:" "${exe_path}" \
		|| die "patchelf failed"
}

src_install() {
	local sizes="16 32 64 128 256 512"

	exeopts -m755
	exeinto "/opt/${PN_PRE}"
	newexe "${MY_PN}" "${PN_PRE}"
	dosym ../../"opt/${PN_PRE}/${PN_PRE}" "/usr/bin/${PN_PRE}"

	for size in ${sizes}; do
		newicon -s "${size}" "icons/${size}x${size}.png" "${PN_PRE}.png"
	done
	domenu "${FILESDIR}/${MY_PN}.desktop"

	dostrip -x ${QA_PREBUILT}
}

pkg_postinst() {
	xdg_desktop_database_update
	xdg_icon_cache_update
}

pkg_postrm() {
	xdg_desktop_database_update
	xdg_icon_cache_update
}