#!/bin/bash
# =============================================================================
# 步骤 1：从原数据机拉取指定舰船号的文件到迁移目标机（按舰版）
# 在【迁移目标机】上执行本脚本
# 配置项（TARGET_SHIPS 等）请修改 migration_config.sh
# =============================================================================

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/migration_config.sh"

set -e

if [ -z "$TARGET_SHIPS" ]; then
    echo "错误：migration_config.sh 中 TARGET_SHIPS 未设置。"
    echo "如需拉取全部文件，请使用 01_copy_files.sh。"
    exit 1
fi

# 构造 rsync 舰船过滤参数
SHIP_FILTERS=()
for ship in $TARGET_SHIPS; do
    SHIP_FILTERS+=("--include=${ship}-*")
done
SHIP_FILTERS+=("--exclude=*")

echo "=========================================="
echo "  kgrag 文件迁移脚本 - 步骤 1（按舰版）"
echo "  原数据机: ${SRC_SERVER}:${SRC_APP_DIR}"
echo "  目标代码目录: ${DST_CODE_DIR}"
echo "  目标文件目录: ${DST_FILES_DIR}"
echo "  目标舰船号: ${TARGET_SHIPS}"
echo "=========================================="

mkdir -p "${DST_CODE_DIR}"
mkdir -p "${DST_FILES_DIR}/uploads_v1"
mkdir -p "${DST_CODE_DIR}/kg_output"
mkdir -p "${DST_CODE_DIR}/fault_wenjian"

echo ""
echo "[1/6] 迁移原始文档 → ${DST_FILES_DIR}/ （仅 ${TARGET_SHIPS}）..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    --exclude="uploads_v1/" \
    "${SHIP_FILTERS[@]}" \
    "${SRC_SERVER}:${SRC_APP_DIR}/files/" \
    "${DST_FILES_DIR}/"

echo ""
echo "[2/6] 迁移高亮注释 PDF → ${DST_FILES_DIR}/uploads_v1/ （全量）..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    "${SRC_SERVER}:${SRC_APP_DIR}/files/uploads_v1/" \
    "${DST_FILES_DIR}/uploads_v1/"

echo ""
echo "[3/6] 迁移抽取结果 JSON → ${DST_CODE_DIR}/kg_output/ （仅 ${TARGET_SHIPS}）..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    "${SHIP_FILTERS[@]}" \
    "${SRC_SERVER}:${SRC_APP_DIR}/kg_output/" \
    "${DST_CODE_DIR}/kg_output/"

echo ""
echo "[4/6] 迁移 kg_snapshot.json → ${DST_CODE_DIR}/ （全量）..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    "${SRC_SERVER}:${SRC_APP_DIR}/kg_snapshot.json" \
    "${DST_CODE_DIR}/kg_snapshot.json"

echo ""
echo "[5/6] 迁移 fault_wenjian/ → ${DST_CODE_DIR}/fault_wenjian/ （仅 ${TARGET_SHIPS}）..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    "${SHIP_FILTERS[@]}" \
    "${SRC_SERVER}:${SRC_APP_DIR}/fault_wenjian/" \
    "${DST_CODE_DIR}/fault_wenjian/"

echo ""
echo "[6/6] 迁移代码文件 → ${DST_CODE_DIR}/ ..."
rsync -avz --progress ${RSYNC_EXTRA} \
    -e "ssh -p ${SRC_SSH_PORT}" \
    --exclude="__pycache__/" \
    --exclude="*.pyc" \
    --exclude="*.log" \
    --exclude="files/" \
    --exclude="kg_output/" \
    --exclude="kg_snapshot.json" \
    --exclude="fault_wenjian/" \
    --exclude="mineru_output/" \
    "${SRC_SERVER}:${SRC_APP_DIR}/" \
    "${DST_CODE_DIR}/"

echo ""
echo "=========================================="
echo "  文件迁移完成！已拉取舰船号: ${TARGET_SHIPS}"
echo ""
echo "  下一步："
echo "  1. python split_by_ship.py"
echo "  2. bash 02_load_neo4j_by_ship.sh"
echo "=========================================="
