#!/usr/bin/env python3
"""cloudpanel -- the CloudPanel command line interface.

Installed as ``/usr/local/bin/cloudpanel`` (a symlink to this file, made by
``install.py``'s ``SystemdServices`` stage) so it works from anywhere without
touching ``PATH``.  Every subcommand is documented in ``docs/CLI.md`` and in
the project README's "Command line" section.

This file is a thin shim on purpose: everything it does is implemented, and
unit tested, in :mod:`install.clicommands`.  Keeping this file tiny means the
one thing that can go wrong with *it specifically* -- finding the CloudPanel
source tree from wherever it was invoked -- is the only thing it has to get
right.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

# ``bin/cloudpanel`` lives at <prefix>/bin/cloudpanel; its parent's parent is
# the CloudPanel source tree, which must be on sys.path for `install` (and, in
# turn, `install.clicommands`) to import.
_PREFIX = Path(__file__).resolve().parent.parent
if str(_PREFIX) not in sys.path:
    sys.path.insert(0, str(_PREFIX))

# install.clicommands.install_prefix() reads this; keep it in step with where
# this script actually lives rather than trusting a stale compiled-in default
# -- but ONLY when this copy lives in an installed tree (one with a venv).
# The release installer runs the NEW release's copy straight out of the
# unpacked archive (/usr/local/src/cloudpanel-X.Y.Z/CloudPanel, which has no
# venv) to upgrade /usr/local/CloudPanel; defaulting the prefix to the
# unpacked tree there would point `upgrade` at the wrong directory entirely.
# packaging/install.sh exports CLOUDPANEL_PREFIX explicitly in that case, and
# install_prefix() falls back to /usr/local/CloudPanel otherwise.
if (_PREFIX / "venv").is_dir():
    os.environ.setdefault("CLOUDPANEL_PREFIX", str(_PREFIX))

from install.clicommands import main  # noqa: E402 - path fixup must run first

if __name__ == "__main__":
    raise SystemExit(main(sys.argv[1:]))
