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

EAPI=8
PYTHON_COMPAT=( python3_{9..11} )
PYTHON_REQ_USE='sqlite(+)'

inherit python-single-r1 systemd

DESCRIPTION="A python based web application for monitoring your Plex Media Server."
HOMEPAGE="https://tautulli.com"
MY_PV="${PV/_beta/-beta}"
MY_PN="Tautulli"
MY_P="${MY_PN}-${MY_PV}"
SRC_URI="https://github.com/${MY_PN}/${MY_PN}/archive/v${MY_PV}.tar.gz -> ${MY_P}.tar.gz"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""
RESTRICT="bindist strip test"

DEPEND="
	${PYTHON_DEPS}
"

RDEPEND="
	acct-user/tautulli
	acct-group/tautulli
	dev-python/pyopenssl
	${DEPEND}
"

S="${WORKDIR}/${MY_P}"

src_install() {
	dodoc API.md CHANGELOG.md CONTRIBUTING.md README.md
	newinitd "${FILESDIR}/${PN}.init" ${PN}
	newconfd "${FILESDIR}/${PN}.conf" ${PN}

	keepdir /var/lib/${PN}
	insinto "/var/lib/${PN}"
	doins -r contrib data lib plexpy pylintrc PlexPy.py Tautulli.py || die
	fowners -R ${PN}:${PN} /var/lib/${PN}

	insinto /etc/${PN}
	fowners -R ${PN}:${PN} /etc/${PN}
	dodir "/etc/${PN}"
	dosym "${EPREFIX}/var/lib/${PN}/config.ini" "/etc/${PN}/config.ini"

	systemd_dounit  "${FILESDIR}/${PN}.service"
	systemd_newunit "${FILESDIR}/${PN}.service" "${PN}@.service"
}

pkg_postinst() {
	elog "Tautulli is now installed but requires the config.ini file"
	elog "be generated in /etc/${_SHORTNAME}/${_APPNAME}"
	elog "To create the initial config.ini interactively run"
	elog "python /var/lib/${PN}/Tautulli.py then ctrl+c"
	elog "before starting the system service"
	elog "you will then be able to access tautulli at"
	elog "http://<ip>:8181/"
	elog ".........................................................."
	elog "Tautulli requires plex-media-server from plex-overlay"
	elog "eselect repository enable plex-overlay"
	elog "to add the required repository"
}

# Adds the precompiled tautulli libraries to the revdep-rebuild's mask list
# so it doesn't try to rebuild libraries that can't be rebuilt.
_mask_tautulli_libraries_revdep() {
	dodir /etc/revdep-rebuild/

	# Bug: 659702. The upstream plex binary installs its precompiled package to /usr/lib.
	# Due to profile 17.1 splitting /usr/lib and /usr/lib64, we can no longer rely
	# on the implicit symlink automatically satisfying our revdep requirement when we use $(get_libdir).
	# Thus we will match upstream's directory automatically. If upstream switches their location,
	# then so should we.
	echo "SEARCH_DIRS_MASK=\"${EPREFIX}/usr/lib/tautulli\"" > "${ED}"/etc/revdep-rebuild/80tautulli
}