#!/bin/bash

# ============================================
# 使用 tar 打包方式同步文件到 NAS
# 绕过 rsync 权限问题
# ============================================

set -e

# 配置信息
NAS_IP="192.168.2.154"
NAS_USER="admin"
LOCAL_PROJECT="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live"
NAS_TARGET="/volume1/stock/sanguo_vnpy"

# 颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

echo "============================================"
echo "  使用 tar 打包方式同步文件到 NAS"
echo "============================================"
echo ""

cd "$LOCAL_PROJECT"

log_info "1/3: 打包本地项目"
# 打包排除一些文件
tar -czf /tmp/sanguo_quant_live.tar.gz \
    --exclude='.git' \
    --exclude='__pycache__' \
    --exclude='*.log' \
    --exclude='*.pyc' \
    --exclude='.ipynb_checkpoints' \
    --exclude='backtest_results/*.png' \
    --exclude='.DS_Store' \
    .

log_info "✅ 打包完成"
echo ""

log_info "2/3: 上传并解压到 NAS"
# 上传并解压
cat /tmp/sanguo_quant_live.tar.gz | ssh "$NAS_USER@$NAS_IP" "cd '$NAS_TARGET' && tar -xzf - && echo '✅ 解压完成'"

log_info "✅ 同步完成"
echo ""

log_info "3/3: 清理本地临时文件"
rm /tmp/sanguo_quant_live.tar.gz
log_info "✅ 清理完成"
echo ""

log_info "🎉 文件同步完成！"
echo ""
