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

EAPI=8

PYTHON_COMPAT=( python3_{13,14} )

inherit desktop python-single-r1 xdg

DESCRIPTION="Control the computer usage time of users"
HOMEPAGE="https://launchpad.net/timekpr-next"
SRC_URI="https://launchpad.net/${PN}/stable/${PV}/+download/${PN}-${PV}.tar.gz"
S="${WORKDIR}/${PN}"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64 ~x86"
IUSE="appindicator speech"

REQUIRED_USE="${PYTHON_REQUIRED_USE}"

RDEPEND="
  ${PYTHON_DEPS}
  acct-group/timekpr
  $(python_gen_cond_dep '
    dev-python/dbus-python[${PYTHON_USEDEP}]
    dev-python/pygobject:3[${PYTHON_USEDEP}]
    dev-python/psutil[${PYTHON_USEDEP}]
  ')
  x11-libs/gtk+:3
  sys-apps/dbus
  sys-auth/elogind
  sys-auth/polkit
  appindicator? (
    || (
      dev-libs/libayatana-appindicator
      dev-libs/libappindicator
    )
  )
  speech? ( app-accesiblility/espeak )
"

RESTRICT="test"

pkg_setup() {
    python-single-r1_pkg_setup
}

src_prepare() {
    default

    local sitedir="$(python_get_sitedir)"

    sed -i "s|/usr/lib/python3/dist-packages|${sitedir}|g"           \
            bin/timekpra bin/timekprc bin/timekprd                   \
            client/timekpra.py client/timekprc.py server/timekprd.py \
            resource/server/systemd/timekpr.service || die
}

src_install() {
    local sitedir="$(python_get_sitedir)"

    exeinto /usr/bin
    doexe bin/timekpra bin/timekprc bin/timekprd

    insinto "${sitedir}/timekpr"
    doins -r common client server

    insinto /etc/timekpr
    doins resource/server/timekpr.conf

    insinto /etc/dbus-1/system.d
    doins resource/server/dbus/timekpr.conf

    insinto /etc/logrotate.d
    newins resource/server/logrotate.d/timekpr timekpr

    insinto /usr/share/polkit-1/actions
    doins resource/server/polkit/com.ubuntu.timekpr.pkexec.policy

    newinitd "${FILESDIR}"/timekprd.initd timekprd

    keepdir /var/lib/timekpr/config /var/lib/timekpr/work

    newicon -s 48 resource/icons/timekpr-48.png timekpr.png
    newicon -s 64 resource/icons/timekpr-64.png timekpr.png
    newicon -s 128 resource/icons/timekpr-128.png timekpr.png
    newicon -s scalable resource/icons/timekpr.svg timekpr.svg
    newicon -s 48 resource/icons/timekpr-client-48.png timekpr-client.png
    newicon -s 64 resource/icons/timekpr-client-64.png timekpr-client.png
    newicon -s 128 resource/icons/timekpr-client-128.png timekpr-client.png
    newicon -s scalable resource/icons/timekpr-client.svg timekpr-client.svg

    insinto /usr/share/timekpr/icons
    doins resource/icons/timekpr-logo.svg \
          resource/icons/timekpr-client-logo.svg \
          resource/icons/timekpr-padlock-limited-green.svg \
          resource/icons/timekpr-padlock-limited-red.svg \
          resource/icons/timekpr-padlock-limited-yellow.svg \
          resource/icons/timekpr-padlock-limited-uacc.svg \
          resource/icons/timekpr-padlock-unlimited-green.svg

    domenu resource/launchers/timekpr-admin.desktop resource/launchers/timekpr-admin-su.desktop
    insinto /etc/xdg/autostart
    doins resource/launchers/timekpr-client.desktop

    insinto /usr/share/timekpr/client/forms
    doins resource/client/forms/about.glade resource/client/forms/admin.glade resource/client/forms/client.glade

    insinto /usr/share/metainfo
    doins resource/appstream/org.timekpr.timekpr-next.metainfo.xml

    local locale
    for locale in resource/locale/*/; do
        locale="${locale%/}"
        locale="${locale##*/}"
        [[ -f "resource/locale/${locale}/LC_MESSAGES/timekpr.mo" ]] || continue
        insinto "/usr/share/locale/${locale}/LC_MESSAGES"
        newins "resource/locale/${locale}/LC_MESSAGES/timekpr.mo" timekpr.mo
    done

    dodoc README.md

    python_optimize "${ED}${sitedir}/timekpr"
}

pkg_postinst() {
    xdg_pkg_postinst

    elog "Members of the 'timekpr' group can use the timekpr-next admin dbus"
    elog "methods without root privileges. Add trusted admin users to it, e.g.:"
    elog ""
    elog "  gpasswd -a <user> timekpr"
    elog ""
    elog "Start/enable the daemon with:"
    elog "  rc-service timekprd start"
    elog "  rc-update add timekprd default"
    elog ""
    elog "The dbus policy at /etc/dbus-1/system.d/timekpr.conf is picked up on"
    elog "the next dbus-daemon restart/reload."
}

pkg_postrm() {
    xdg_pkg_postrm
}