#!/bin/bash
# Docker Compose配置编辑器
# 用法: ./docker-config-editor.sh <服务器> <应用> <操作> [参数]

set -e

SERVER="$1"
APP="$2"
ACTION="$3"
shift 3

# 根据服务器获取应用路径
get_app_path() {
    local server="$1"
    local app="$2"
    
    case "$server" in
        macmini)
            echo "/Users/weishen/docker/$app"
            ;;
        ubuntu1)
            echo "/home/shenwei/Docker/$app"
            ;;
        ubuntu2)
            echo "/home/shenwei/docker/$app"
            ;;
        nas)
            echo "/volume1/docker/$app"
            ;;
        *)
            echo "未知服务器: $server" >&2
            exit 1
            ;;
    esac
}

# 备份docker-compose.yml文件
backup_config() {
    local app_path="$1"
    local timestamp=$(date +%Y%m%d%H%M%S)
    
    echo "📁 创建备份..."
    ssh "$SERVER" "cd '$app_path' && cp docker-compose.yml docker-compose.yml.backup.$timestamp"
    
    # 清理旧备份，只保留最近5个
    ssh "$SERVER" "cd '$app_path' && ls -t docker-compose.yml.backup.* 2>/dev/null | tail -n +6 | xargs -r rm -f"
    
    echo "✅ 备份创建完成: docker-compose.yml.backup.$timestamp"
}

# 验证docker-compose配置格式
validate_config() {
    local app_path="$1"
    
    echo "🔍 验证配置格式..."
    if ssh "$SERVER" "cd '$app_path' && docker compose config >/dev/null 2>&1"; then
        echo "✅ 配置格式验证通过"
        return 0
    else
        echo "❌ 配置格式验证失败"
        return 1
    fi
}

# 显示当前配置
show_config() {
    local app_path="$1"
    
    echo "📄 当前docker-compose.yml内容:"
    echo "─────────────────────────────"
    ssh "$SERVER" "cat '$app_path/docker-compose.yml'"
    echo "─────────────────────────────"
}

# 显示修改前后的差异
show_diff() {
    local app_path="$1"
    local temp_file="$2"
    
    echo "📊 修改前后差异:"
    echo "─────────────────────────────"
    # 获取原文件内容
    local original_content=$(ssh "$SERVER" "cat '$app_path/docker-compose.yml'")
    
    # 使用diff比较
    diff -u <(echo "$original_content") "$temp_file" || true
    echo "─────────────────────────────"
}

# 应用修改
apply_changes() {
    local app_path="$1"
    local temp_file="$2"
    
    echo "🔄 应用修改..."
    cat "$temp_file" | ssh "$SERVER" "cat > '$app_path/docker-compose.yml'"
    
    if validate_config "$app_path"; then
        echo "✅ 修改已应用并验证通过"
        return 0
    else
        echo "❌ 修改后配置验证失败，已回滚"
        # 回滚到最新备份
        local latest_backup=$(ssh "$SERVER" "cd '$app_path' && ls -t docker-compose.yml.backup.* 2>/dev/null | head -1")
        if [ -n "$latest_backup" ]; then
            ssh "$SERVER" "cd '$app_path' && cp '$latest_backup' docker-compose.yml"
            echo "🔄 已回滚到备份: $latest_backup"
        fi
        return 1
    fi
}

# 重启应用
restart_app() {
    local app_path="$1"
    
    echo "🔄 重启应用..."
    ssh "$SERVER" "cd '$app_path' && docker compose down && docker compose up -d"
    
    # 等待应用启动
    sleep 5
    
    echo "📊 应用状态:"
    ssh "$SERVER" "cd '$app_path' && docker compose ps"
}

# 主函数
main() {
    if [ $# -lt 3 ]; then
        echo "用法: $0 <服务器> <应用> <操作> [参数]"
        echo "示例: $0 ubuntu1 portainer show"
        exit 1
    fi
    
    APP_PATH=$(get_app_path "$SERVER" "$APP")
    echo "🎯 目标: $SERVER -> $APP ($APP_PATH)"
    
    case "$ACTION" in
        show)
            show_config "$APP_PATH"
            ;;
        backup)
            backup_config "$APP_PATH"
            ;;
        validate)
            validate_config "$APP_PATH"
            ;;
        restart)
            restart_app "$APP_PATH"
            ;;
        edit)
            # 编辑模式需要交互
            echo "⚠️  编辑模式需要交互式操作，请使用其他工具"
            ;;
        *)
            echo "未知操作: $ACTION"
            echo "可用操作: show, backup, validate, restart"
            exit 1
            ;;
    esac
}

main "$@"