#!/bin/bash
# M2.1 设备端部署脚本 — 部署 AI 推理二进制 + 模型文件
# 用法: bash scripts/deploy_ai.sh
# 前置: ~/ai_spike/ 存在 (spike 2026-07-03 保留)

set -e
SPIKE_DIR="$HOME/ai_spike"
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
TOOLS_DIR="$PROJECT_DIR/tools"
MODEL_STORE="$PROJECT_DIR/model_store"

echo "=== M2.1 AI 部署 ==="
echo "项目: $PROJECT_DIR"
echo "spike: $SPIKE_DIR"

# 检查 spike 目录
if [ ! -d "$SPIKE_DIR" ]; then
    echo "错误: spike 目录不存在 ($SPIKE_DIR)"
    echo "请先完成 spike (参考 TEAM_CHAT.md 2026-07-03 张明远报告)"
    exit 1
fi

# 1. 部署二进制到 tools/
echo "--- 部署二进制到 tools/ ---"
mkdir -p "$TOOLS_DIR"

# whisper-cli (依赖 libggml*.so，需设 LD_LIBRARY_PATH 或 RPATH)
ln -sf "$SPIKE_DIR/whisper.cpp/build/bin/whisper-cli" "$TOOLS_DIR/whisper-cli"
echo "  ✓ whisper-cli"

# llama-cli
ln -sf "$SPIKE_DIR/llama.cpp/build/bin/llama-cli" "$TOOLS_DIR/llama-cli"
echo "  ✓ llama-cli"

# piper
ln -sf "$SPIKE_DIR/piper/piper" "$TOOLS_DIR/piper"
echo "  ✓ piper"

# 2. 部署模型到 model_store/
echo "--- 部署模型到 model_store/ ---"
mkdir -p "$MODEL_STORE/llm" "$MODEL_STORE/asr" "$MODEL_STORE/tts"

# LLM: Qwen2.5-0.5B (默认推荐)
ln -sf "$SPIKE_DIR/models/qwen2.5-0.5b-instruct-q4_k_m.gguf" "$MODEL_STORE/llm/qwen2.5-0.5b-q4_k_m.gguf"
echo "  ✓ llm/qwen2.5-0.5b-q4_k_m.gguf (469M, 推荐默认)"

# LLM: Gemma3-1B (可选档)
ln -sf "$SPIKE_DIR/models/gemma-3-1b-it-Q4_K_M.gguf" "$MODEL_STORE/llm/gemma3-1b-q4_k_m.gguf"
echo "  ✓ llm/gemma3-1b-q4_k_m.gguf (769M, 可选档)"

# ASR: whisper tiny
ln -sf "$SPIKE_DIR/models/ggml-tiny.bin" "$MODEL_STORE/asr/ggml-tiny.bin"
echo "  ✓ asr/ggml-tiny.bin (75M)"

# ASR: whisper base (备选)
ln -sf "$SPIKE_DIR/models/ggml-base.bin" "$MODEL_STORE/asr/ggml-base.bin"
echo "  ✓ asr/ggml-base.bin (142M, 备选)"

# TTS: piper zh_CN-huayan-medium
ln -sf "$SPIKE_DIR/piper/zh_CN-huayan-medium.onnx" "$MODEL_STORE/tts/zh_CN-huayan-medium.onnx"
ln -sf "$SPIKE_DIR/piper/zh_CN-huayan-medium.onnx.json" "$MODEL_STORE/tts/zh_CN-huayan-medium.onnx.json"
echo "  ✓ tts/zh_CN-huayan-medium.onnx (63M)"

# 3. 设置 whisper LD_LIBRARY_PATH (whisper-cli 依赖 libggml*.so)
WHISPER_LIB="$SPIKE_DIR/whisper.cpp/build/bin"
echo "--- whisper 依赖库路径: $WHISPER_LIB ---"

# 4. 验证
echo "=== 验证 ==="
echo "tools/:"
ls -la "$TOOLS_DIR"
echo "model_store/:"
ls -laR "$MODEL_STORE"

# 测试 llama-cli 能跑
echo "--- 测试 llama-cli ---"
"$TOOLS_DIR/llama-cli" --version 2>&1 | head -1 || echo "  (llama-cli 版本检查失败, 可能仍可用)"

# 测试 piper 能跑
echo "--- 测试 piper ---"
"$TOOLS_DIR/piper" --help 2>&1 | head -1 || echo "  (piper 帮助检查失败, 可能仍可用)"

echo "=== 部署完成 ==="
echo ""
echo "注意: whisper-cli 运行时需要 LD_LIBRARY_PATH 包含:"
echo "  $WHISPER_LIB"
echo "建议在 systemd service 或启动脚本中设置此环境变量"
