#!/usr/bin/env bash
set -euo pipefail

INSTALL_DIR="${HOME}/.local/bin"
BIN_NAME="theseus"
PROJECT_ROOT="$(cd "$(dirname "$0")" && pwd)"

echo "=== Theseus Install ==="

# 确保安装目录存在
mkdir -p "$INSTALL_DIR"

# Release 构建
echo "[1/3] Building release..."
cargo build --release 2>&1 | tail -3

# 拷贝 binary
echo "[2/3] Installing to $INSTALL_DIR ..."
cp "$PROJECT_ROOT/target/release/$BIN_NAME" "$INSTALL_DIR/$BIN_NAME"
chmod +x "$INSTALL_DIR/$BIN_NAME"

# PATH 检查
echo "[3/3] Done."
echo ""
echo "Installed:"
echo "  $INSTALL_DIR/$BIN_NAME"
echo ""

if ! echo "$PATH" | grep -q "$INSTALL_DIR"; then
    echo "⚠️  $INSTALL_DIR not in PATH."
    echo "   Add this to your ~/.zshrc or ~/.bashrc:"
    echo ""
    echo "   export PATH=\"\$HOME/.local/bin:\$PATH\""
    echo ""
else
    echo "✅ $INSTALL_DIR is in PATH"
fi

echo "Try: theseus --help"
