#!/usr/bin/env sh
# Wrapper gọi engine ui_intel. Tìm UID_HOME theo thứ tự:
#   1. biến môi trường UID_HOME
#   2. thư mục cha chứa taxonomy/kinds.yaml (skill nằm trong repo)
#   3. file .uid-home cạnh script (do `npx webapp-ui-design init` ghi)
set -e
here="$(cd "$(dirname "$0")" && pwd)"
if [ -z "$UID_HOME" ]; then
  d="$here"
  while [ "$d" != "/" ]; do
    if [ -f "$d/taxonomy/kinds.yaml" ]; then UID_HOME="$d"; break; fi
    d="$(dirname "$d")"
  done
fi
if [ -z "$UID_HOME" ] && [ -f "$here/.uid-home" ]; then UID_HOME="$(cat "$here/.uid-home")"; fi
if [ -z "$UID_HOME" ]; then
  echo "uid: không tìm thấy UID_HOME. Đặt UID_HOME=/đường/dẫn/webapp-ui-design" >&2; exit 2
fi
export UID_HOME
if command -v uid >/dev/null 2>&1 && [ "$(command -v uid)" != "$0" ]; then exec uid "$@"; fi
# .uid-python do `uid install-skill` (bản pip) ghi: Python đã cài engine
if [ -z "$PYTHON" ] && [ -f "$here/.uid-python" ]; then PYTHON="$(cat "$here/.uid-python")"; fi
PY="${PYTHON:-python3}"; command -v "$PY" >/dev/null 2>&1 || PY=python
PYTHONPATH="$UID_HOME/engine${PYTHONPATH:+:$PYTHONPATH}" exec "$PY" -m ui_intel "$@"
