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

EAPI=8

LOMIRI_PPA_COMMIT="3.4.1-6"
inherit lomiri

if [[ ${PV} == 9999 ]]; then
    KEYWORDS=""
    EGIT_REPO_URI="https://gitlab.com/ubports/development/core/${PN}.git"
    inherit git-r3
else
    KEYWORDS="~amd64"
    SRC_URI="https://gitlab.com/ubports/development/core/${PN}/-/archive/${PV}/${P}.tar.gz
             ${LOMIRI_PPA_URI}"
fi

DESCRIPTION="Location service aggregating position/velocity/heading updates"
HOMEPAGE="https://gitlab.com/ubports/development/core/${PN}"
LICENSE="LGPL-3 GPL-3"
SLOT="0"
IUSE="gpsd qt6"
RESTRICT="test mirror"

DEPEND="
    dev-cpp/dbus-cpp
    dev-cpp/gflags
    dev-cpp/glog
    dev-cpp/net-cpp
    dev-cpp/process-cpp
    dev-cpp/properties-cpp
    dev-libs/boost
    dev-libs/json-c
    sys-apps/dbus
    sys-libs/libapparmor
    gpsd? ( sci-geosciences/gpsd[cxx] )
    qt6? (
        dev-qt/qtbase:6[gui]
        dev-qt/qtpositioning:6
    )
"
RDEPEND="${DEPEND}"

BDEPEND="
    dev-build/cmake
    dev-build/cmake-extras
    dev-build/samurai
    sys-devel/gettext
    virtual/pkgconfig
"

PATCHES=(
    "${FILESDIR}"/${PN}-optional-qt-plugin.patch
    "${FILESDIR}"/${PN}-pkgconfig-libdir.patch
)

src_prepare() {
    lomiri_src_prepare

    cmake_comment_add_subdirectory examples
    cmake_comment_add_subdirectory tests
}

src_configure() {
    local mycmakeargs=(
        -DBUILD_SHARED_LIBS=OFF
        -DENABLE_TRUST_STORE=OFF
        -DENABLE_QT_PLUGIN=$(usex qt6)
        -DENABLE_QT6=ON
        -DLOCATION_SERVICE_ENABLE_GPS_PROVIDER=OFF
        -DLOCATION_SERVICE_ENABLE_GPSD_PROVIDER=$(usex gpsd)
        -DLOCATION_SERVICE_ENABLE_GEOCLUE_PROVIDER=ON
        -DCMAKE_DISABLE_FIND_PACKAGE_Doxygen=ON
    )

    lomiri_src_configure
}

src_install() {
    lomiri_src_install

    rm "${ED}"/usr/lib/systemd/user/${PN}-trust-stored.service || die
    keepdir /var/lib/${PN}
}

pkg_postinst() {
    lomiri_pkg_postinst

    if [[ -z ${REPLACING_VERSIONS} ]]; then
        elog "Enable the location service with:"
        elog "  systemctl enable --now ${PN}.service"
        if use gpsd; then
            elog "To get positions from gpsd, add '--provider gpsd::Provider' to"
            elog "ExecStart via 'systemctl edit ${PN}.service'."
        fi
    fi
}