#!/bin/bash

# ============================================
# NAS 管理工具
# 提供挂载、卸载、状态检查、日志查看等功能
# ============================================

NAS_IP="192.168.2.154"
NAS_USER="cfdaily"
NAS_PASS="Ccf7561523"
NAS_SHARE="stock"
MOUNT_POINT="/Users/chufeng/nas/stock"
LAUNCH_DAEMON_LABEL="com.user.nasmount"
LOG_DIR="/Users/chufeng/.openclaw/workspace-jiangwei/logs"
MOUNT_LOG="${LOG_DIR}/nas_mount.log"
ERROR_LOG="${LOG_DIR}/nas_mount_error.log"

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

print_header() {
    echo -e "${BLUE}============================================${NC}"
    echo -e "${BLUE}   NAS 管理工具${NC}"
    echo -e "${BLUE}============================================${NC}"
    echo ""
}

check_mounted() {
    if mount | grep -q "$MOUNT_POINT"; then
        return 0
    else
        return 1
    fi
}

check_network() {
    ping -c 1 -W 2 "$NAS_IP" &> /dev/null
}

show_status() {
    print_header
    echo "【状态检查】"
    echo ""
    
    # 网络状态
    echo -n "网络连接: "
    if check_network; then
        echo -e "${GREEN}✅ 正常 ($NAS_IP)${NC}"
    else
        echo -e "${RED}❌ 无法连接${NC}"
    fi
    
    # 挂载状态
    echo -n "NAS 挂载: "
    if check_mounted; then
        echo -e "${GREEN}✅ 已挂载${NC}"
        echo -e "   挂载点: $MOUNT_POINT"
        echo ""
        echo "【挂载点内容】"
        ls -lh "$MOUNT_POINT" 2>/dev/null || echo "无法读取挂载点"
    else
        echo -e "${RED}❌ 未挂载${NC}"
    fi
    
    echo ""
    echo "【Launch Daemon 状态】"
    if launchctl list | grep -q "$LAUNCH_DAEMON_LABEL"; then
        echo -e "${GREEN}✅ 正在运行${NC}"
    else
        echo -e "${YELLOW}⚠️  未运行${NC}"
    fi
    
    echo ""
    echo "【磁盘使用情况】"
    if check_mounted; then
        df -h "$MOUNT_POINT"
    else
        echo "NAS 未挂载，无法显示"
    fi
}

mount_nas() {
    print_header
    echo "【挂载 NAS】"
    echo ""
    
    if check_mounted; then
        echo -e "${YELLOW}NAS 已经挂载${NC}"
        return 0
    fi
    
    if ! check_network; then
        echo -e "${RED}错误: 无法连接到 NAS ($NAS_IP)${NC}"
        return 1
    fi
    
    echo "正在挂载..."
    
    mkdir -p "$MOUNT_POINT"
    NAS_URL="smb://${NAS_USER}:${NAS_PASS}@${NAS_IP}/${NAS_SHARE}"
    
    if /sbin/mount_smbfs "$NAS_URL" "$MOUNT_POINT"; then
        echo -e "${GREEN}✅ NAS 挂载成功！${NC}"
        echo "挂载点: $MOUNT_POINT"
        
        # 创建标记文件
        touch "${MOUNT_POINT}/.mounted"
        
        # 创建目录结构
        echo ""
        echo "创建目录结构..."
        create_dir_structure
        
        return 0
    else
        echo -e "${RED}❌ NAS 挂载失败${NC}"
        return 1
    fi
}

umount_nas() {
    print_header
    echo "【卸载 NAS】"
    echo ""
    
    if ! check_mounted; then
        echo -e "${YELLOW}NAS 未挂载${NC}"
        return 0
    fi
    
    echo "正在卸载..."
    
    if umount "$MOUNT_POINT"; then
        echo -e "${GREEN}✅ NAS 卸载成功${NC}"
        return 0
    else
        echo -e "${YELLOW}强制卸载..."
        if umount -f "$MOUNT_POINT"; then
            echo -e "${GREEN}✅ NAS 强制卸载成功${NC}"
            return 0
        else
            echo -e "${RED}❌ NAS 卸载失败${NC}"
            return 1
        fi
    fi
}

create_dir_structure() {
    cd "$MOUNT_POINT" || return
    
    mkdir -p "A股数据/日线数据" "A股数据/分钟线数据" "A股数据/财务数据"
    mkdir -p "回测结果/策略回测" "回测结果/性能报告"
    mkdir -p "代码库/策略代码" "代码库/工具脚本"
    mkdir -p "临时文件/下载缓存" "临时文件/临时数据"
    
    chown -R chufeng:staff "$MOUNT_POINT" 2>/dev/null || true
}

show_logs() {
    print_header
    echo "【日志查看】"
    echo ""
    
    if [ ! -f "$MOUNT_LOG" ]; then
        echo -e "${YELLOW}日志文件不存在${NC}"
        return
    fi
    
    echo "最近 50 条日志:"
    echo "----------------------------------------"
    tail -50 "$MOUNT_LOG"
}

follow_logs() {
    print_header
    echo "【实时日志】"
    echo "按 Ctrl+C 退出"
    echo "----------------------------------------"
    
    if [ ! -f "$MOUNT_LOG" ]; then
        touch "$MOUNT_LOG"
    fi
    
    tail -f "$MOUNT_LOG"
}

restart_daemon() {
    print_header
    echo "【重启 Launch Daemon】"
    echo ""
    
    echo "停止守护进程..."
    sudo launchctl stop "$LAUNCH_DAEMON_LABEL" 2>/dev/null
    
    sleep 2
    
    echo "启动守护进程..."
    sudo launchctl start "$LAUNCH_DAEMON_LABEL"
    
    echo -e "${GREEN}✅ Launch Daemon 已重启${NC}"
}

show_help() {
    print_header
    echo "使用方法: $0 [命令]"
    echo ""
    echo "命令列表:"
    echo "  status    - 显示 NAS 状态"
    echo "  mount     - 手动挂载 NAS"
    echo "  umount    - 卸载 NAS"
    echo "  restart   - 重启 Launch Daemon"
    echo "  logs      - 显示最近日志"
    echo "  follow    - 实时跟踪日志"
    echo "  help      - 显示帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 status    # 查看状态"
    echo "  $0 follow    # 实时查看日志"
}

# 主逻辑
case "${1:-status}" in
    status)
        show_status
        ;;
    mount)
        mount_nas
        ;;
    umount)
        umount_nas
        ;;
    restart)
        restart_daemon
        ;;
    logs)
        show_logs
        ;;
    follow)
        follow_logs
        ;;
    help)
        show_help
        ;;
    *)
        echo -e "${RED}未知命令: $1${NC}"
        echo ""
        show_help
        exit 1
        ;;
esac
