#!/bin/bash
# 系统信息查询脚本（跨平台支持）
# 用法: system-info [option]
# 选项: all (默认), cpu, memory, disk, network, system

OPTION=${1:-all}

echo "=========================================="
echo "         System Info Report - $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
echo ""

# 系统基本信息
show_system() {
    echo "[System Info]"
    echo "  Hostname: $(hostname)"
    echo "  OS: $(uname -s)"
    echo "  Kernel: $(uname -r)"
    echo "  Arch: $(uname -m)"
    echo ""
}

# CPU 信息
show_cpu() {
    echo "[CPU Info]"
    if [ -f /proc/cpuinfo ]; then
        echo "  Model: $(grep 'model name' /proc/cpuinfo | head -1 | cut -d':' -f2 | xargs)"
        echo "  Cores: $(grep -c ^processor /proc/cpuinfo)"
    elif command -v powershell &> /dev/null; then
        cpu_info=$(powershell -NoProfile -Command "
            \$cpu = Get-CimInstance Win32_Processor | Select-Object -First 1
            \$cores = (Get-CimInstance Win32_Processor | Measure-Object -Property NumberOfCores -Sum).Sum
            \$threads = (Get-CimInstance Win32_Processor | Measure-Object -Property NumberOfLogicalProcessors -Sum).Sum
            Write-Output \"\$(\$cpu.Name)|\$cores|\$threads\"
        ")
        cpu_name=$(echo "$cpu_info" | cut -d'|' -f1)
        cpu_cores=$(echo "$cpu_info" | cut -d'|' -f2)
        cpu_threads=$(echo "$cpu_info" | cut -d'|' -f3)
        echo "  Model: $cpu_name"
        echo "  Cores: $cpu_cores"
        echo "  Threads: $cpu_threads"
    else
        echo "  Model: $(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo 'Unknown')"
    fi
    echo ""
}

# 内存信息
show_memory() {
    echo "[Memory Info]"
    if [ -f /proc/meminfo ]; then
        mem_total=$(grep MemTotal /proc/meminfo | awk '{printf "%.2f GB", $2/1024/1024}')
        mem_free=$(grep MemFree /proc/meminfo | awk '{printf "%.2f GB", $2/1024/1024}')
        mem_avail=$(grep MemAvailable /proc/meminfo 2>/dev/null | awk '{printf "%.2f GB", $2/1024/1024}')
        echo "  Total: $mem_total"
        echo "  Free: $mem_free"
        echo "  Available: ${mem_avail:-N/A}"
    elif command -v powershell &> /dev/null; then
        mem_info=$(powershell -NoProfile -Command "
            \$os = Get-CimInstance Win32_OperatingSystem
            \$total = [math]::Round(\$os.TotalVisibleMemorySize / 1MB, 2)
            \$free = [math]::Round(\$os.FreePhysicalMemory / 1MB, 2)
            \$used = [math]::Round(\$total - \$free, 2)
            \$pct = [math]::Round((\$used / \$total) * 100, 1)
            Write-Output \"\$total|\$used|\$free|\$pct\"
        ")
        total=$(echo "$mem_info" | cut -d'|' -f1)
        used=$(echo "$mem_info" | cut -d'|' -f2)
        free=$(echo "$mem_info" | cut -d'|' -f3)
        pct=$(echo "$mem_info" | cut -d'|' -f4)
        echo "  Total: ${total} GB"
        echo "  Used: ${used} GB (${pct}%)"
        echo "  Free: ${free} GB"
    else
        echo "  Unable to get memory info"
    fi
    echo ""
}

# 磁盘信息
show_disk() {
    echo "[Disk Info]"
    if command -v powershell &> /dev/null; then
        # Windows PowerShell - 使用 UTF8 编码输出
        powershell -NoProfile -Command "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-CimInstance Win32_LogicalDisk -Filter \"DriveType=3\" | Format-Table -AutoSize | Out-String"
    elif [ -f /proc/mounts ] && command -v df &> /dev/null; then
        df -h | grep -E '^/dev/' | awk '{
            printf "  %s: Total %s, Used %s (%s), Free %s\n", $1, $2, $3, $5, $4
        }'
    else
        echo "  Unable to get disk info"
    fi
    echo ""
}

# 网络信息
show_network() {
    echo "[Network Info]"
    echo "  IP Addresses:"
    if command -v ip &> /dev/null; then
        ip addr show | grep inet | grep -v '127.0.0.1' | awk '{printf "    %s\n", $2}'
    elif command -v ifconfig &> /dev/null; then
        ifconfig | grep 'inet ' | grep -v '127.0.0.1' | awk '{printf "    %s\n", $2}'
    elif command -v powershell &> /dev/null; then
        powershell -NoProfile -Command "Get-NetIPAddress -AddressFamily IPv4 | Where-Object { \$_.IPAddress -ne '127.0.0.1' } | ForEach-Object { Write-Output \"    \$(\$_.IPAddress)\" }"
    fi
    echo ""
}

# 执行选项
case $OPTION in
    all)
        show_system
        show_cpu
        show_memory
        show_disk
        show_network
        ;;
    cpu)    show_cpu ;;
    memory) show_memory ;;
    disk)   show_disk ;;
    network) show_network ;;
    system) show_system ;;
    *)
        echo "Usage: system-info [all|cpu|memory|disk|network|system]"
        exit 1
        ;;
esac

echo "=========================================="
