#!/bin/bash
# Modified by Travis234 from Ghost OS revision 991aa4831295aaff6beef04cc809d0f0b53dc024.
# ghost-vision — Launcher for Ghost OS Vision Sidecar
#
# Runs only from the fixed Travis234 vision environment and package assets.
#
# Usage:
#   ghost-vision                    # Start server (default port 9876)
#   ghost-vision --port 9877        # Custom port
#   ghost-vision --health-check     # Test model loading, then exit
#   ghost-vision --version          # Print version

set -euo pipefail

VERSION="2.2.1"
STATE_ROOT="$HOME/.travis234/ghost-mcp"
VENV_PYTHON="$STATE_ROOT/vision-venv/bin/python3"
SERVER_PY="$(cd "$(dirname "$0")" && pwd)/server.py"

# Handle --version before anything else
if [[ "${1:-}" == "--version" || "${1:-}" == "-v" ]]; then
    echo "ghost-vision $VERSION"
    exit 0
fi

if [[ ! -f "$SERVER_PY" ]]; then
    echo "ERROR: Cannot find vision sidecar server.py" >&2
    echo "Run: ghost setup" >&2
    exit 1
fi

if [[ ! -x "$VENV_PYTHON" ]] || ! "$VENV_PYTHON" -c "import mlx_vlm" 2>/dev/null; then
    echo "ERROR: Cannot find Python with mlx_vlm installed" >&2
    echo "Run 'ghost setup --vision' to create the vision environment." >&2
    exit 1
fi

exec "$VENV_PYTHON" "$SERVER_PY" "$@"
