#!/bin/bash
set -euo pipefail
# AgentsMeeting — Linux 安装脚本
# 以 hmo 用户身份运行

echo "=== AgentsMeeting Linux Installation ==="

# 1. Install systemd units
echo "[1/4] Installing systemd units..."
sudo cp deploy/linux/hermes-gateway@.service /etc/systemd/system/
sudo cp deploy/linux/xmpp-bot@.service /etc/systemd/system/
sudo systemctl daemon-reload

# 2. Create bot directories
echo "[2/4] Creating bot directories..."
for bot in mohe xiaoguo zhiwei; do
    mkdir -p /home/hmo/bots/${bot}
done

# 3. Copy bot scripts
echo "[3/4] Copying bot scripts..."
cp src/bots/mohe/bot.py /home/hmo/bots/mohe/
cp src/bots/xiaoguo/bot.py /home/hmo/bots/xiaoguo/
cp src/bots/zhiwei/bot.py /home/hmo/bots/zhiwei/

# 4. Enable services
echo "[4/4] Enabling services..."
for profile in main mohe position-analyst xiaoguo; do
    if [ -f "/home/hmo/.hermes/profiles/${profile}/config.yaml" ]; then
        sudo systemctl enable --now hermes-gateway@${profile}
        echo "  hermes-gateway@${profile} enabled"
    else
        echo "  hermes-gateway@${profile}: SKIP (no config)"
    fi
done

for bot in mohe xiaoguo zhiwei; do
    if [ -f "/home/hmo/bots/${bot}/bot.py" ]; then
        sudo systemctl enable --now xmpp-bot@${bot}
        echo "  xmpp-bot@${bot} enabled"
    fi
done

echo "=== Done ==="
echo "Check: systemctl status hermes-gateway@main xmpp-bot@mohe"
