#!/bin/bash

# 测试VPS快照脚本的应用扫描功能
# 基于 vps-snapshot.sh v3.14 的 detect_apps 函数

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

info() { echo -e "${CYAN}[INFO] $1${NC}"; }

detect_apps() {
    info "🔍 扫描已安装的应用..."
    echo ""
    
    local apps=()
    
    # Docker
    if command -v docker &>/dev/null && docker info &>/dev/null; then
        local containers=$(docker ps -q | wc -l)
        local images=$(docker images -q | wc -l)
        echo -e "  ${GREEN}✓${NC} Docker: $containers 个运行中容器, $images 个镜像"
        apps+=("docker")
    fi
    
    # Docker Compose
    if command -v docker-compose &>/dev/null || docker compose version &>/dev/null 2>&1; then
        local compose_files=$(find /opt /root /home -name "docker-compose*.yml" -o -name "compose*.yml" 2>/dev/null | wc -l)
        echo -e "  ${GREEN}✓${NC} Docker Compose: $compose_files 个配置文件"
        apps+=("compose")
    fi
    
    # Nginx
    if command -v nginx &>/dev/null || [ -d /etc/nginx ]; then
        echo -e "  ${GREEN}✓${NC} Nginx"
        apps+=("nginx")
    fi
    
    # MySQL/MariaDB
    if command -v mysql &>/dev/null || [ -d /var/lib/mysql ]; then
        echo -e "  ${GREEN}✓${NC} MySQL/MariaDB"
        apps+=("mysql")
    fi
    
    # PostgreSQL
    if command -v psql &>/dev/null || [ -d /var/lib/postgresql ]; then
        echo -e "  ${GREEN}✓${NC} PostgreSQL"
        apps+=("postgresql")
    fi
    
    # Redis
    if command -v redis-cli &>/dev/null || [ -d /var/lib/redis ]; then
        echo -e "  ${GREEN}✓${NC} Redis"
        apps+=("redis")
    fi
    
    # MongoDB
    if command -v mongod &>/dev/null || [ -d /var/lib/mongodb ]; then
        echo -e "  ${GREEN}✓${NC} MongoDB"
        apps+=("mongodb")
    fi
    
    # Node.js/NPM
    if command -v node &>/dev/null; then
        local node_ver=$(node -v 2>/dev/null || echo "unknown")
        local npm_global=$(npm list -g --depth=0 2>/dev/null | wc -l)
        echo -e "  ${GREEN}✓${NC} Node.js $node_ver ($npm_global 个全局包)"
        apps+=("nodejs")
    fi
    
    # PM2
    if command -v pm2 &>/dev/null; then
        local pm2_apps=$(pm2 jlist 2>/dev/null | jq length 2>/dev/null || echo "0")
        echo -e "  ${GREEN}✓${NC} PM2: $pm2_apps 个应用"
        apps+=("pm2")
    fi
    
    # Python/Pip
    if command -v python3 &>/dev/null; then
        local py_ver=$(python3 --version 2>/dev/null | cut -d' ' -f2)
        echo -e "  ${GREEN}✓${NC} Python $py_ver"
        apps+=("python")
    fi
    
    # PHP
    if command -v php &>/dev/null; then
        local php_ver=$(php -v 2>/dev/null | head -1 | cut -d' ' -f2)
        echo -e "  ${GREEN}✓${NC} PHP $php_ver"
        apps+=("php")
    fi
    
    # 1Panel
    if [ -d /opt/1panel ] || command -v 1pctl &>/dev/null; then
        echo -e "  ${GREEN}✓${NC} 1Panel"
        apps+=("1panel")
    fi
    
    # 宝塔
    if [ -d /www/server/panel ]; then
        echo -e "  ${GREEN}✓${NC} 宝塔面板"
        apps+=("bt")
    fi
    
    echo ""
    info "检测到的应用: ${apps[*]}"
    echo ""
    info "应用扫描完成！"
}

# 运行应用扫描
detect_apps