#!/usr/bin/python3 -I
# SPDX-FileCopyrightText: 2026 The NazoOS Contributors
# SPDX-License-Identifier: GPL-3.0-or-later
"""Root part of nazoos-update.

The actions live in nazoos_update/root/ (list: root/__init__.py).
Started by nazoos-update-check.service (action "check") and by the GUI
via pkexec ("check", "update", "rollback"); "label" by
nazoos-update-label.service. Only "update" takes an argument:
"clean-flatpaks" also removes unused Flatpak runtimes.
The status of the last check is written to STATUS as JSON, readable by
every user; the GUI and the notifier only read it.
The last output line "@@error {...}" carries a message for the user
(nazoos_common.errors).
"""

import sys
from pathlib import Path

# Same block in nazoos-welcome-helper(-strict) and nazoos-kernel-boot: keep them alike
# Package scripts may start us with the caller's environment: -I keeps
# PYTHONPATH and user site-packages out
# pkexec clears PYTHONPATH: load our modules from the installed location
# (or from the git checkout)
_src = Path(__file__).resolve().parent.parent
for pkg, bases in (("nazoos_update", ("/usr/share/nazoos-update", _src)),
                   ("nazoos_common", ("/usr/share/nazoos-common",
                                      _src.parent / "nazoos-common"))):
    base = next((b for b in map(Path, bases) if (b / pkg).is_dir()), None)
    if base is not None:
        sys.path.insert(0, str(base))
from nazoos_common import helper  # noqa: E402
from nazoos_update.root import ACTIONS  # noqa: E402

if __name__ == "__main__":
    sys.exit(helper.main(ACTIONS, lock_timeout=300, max_args=1))
