#!/usr/bin/env bash
# /usr/local/bin/uaml — host-wide UAML CLI shim.
#
# Why a shim:
#   - The `uaml` console entry-point from pyproject.toml only lands on
#     PATH if `pip install` writes to a site-packages bin/ that's on PATH.
#     Smart-install lays the package under /opt/uaml-package/ and does
#     not run `pip install` host-wide, so without this shim users get
#     "uaml: command not found" after a successful install (B7).
#
#   - The shim also sources /etc/uaml/uaml.env so that
#     `sudo uaml license activate KEY` reads installations.db from
#     /home/uaml/.uaml/ instead of /root/.uaml/ (B8).
#
# Usage: uaml <subcommand> [args]

set -e

UAML_PACKAGE_DIR="${UAML_PACKAGE_DIR:-/opt/uaml-package}"

# Source host-wide environment if present.
if [[ -f /etc/uaml/uaml.env ]]; then
  set -a
  # shellcheck disable=SC1091
  . /etc/uaml/uaml.env
  set +a
fi

# Ensure the package on disk is importable even when not pip-installed.
if [[ -d "$UAML_PACKAGE_DIR" ]]; then
  export PYTHONPATH="${UAML_PACKAGE_DIR}${PYTHONPATH:+:$PYTHONPATH}"
fi

exec /usr/bin/python3 -m uaml.cli.main "$@"
