#!/bin/bash
# list-proxmox-images.sh
# Lists all ISO and disk images available on Proxmox instances

set -euo pipefail

# Load environment variables
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -f "${SCRIPT_DIR}/../.env" ]; then
    set -a
    source <(grep -v '^#' "${SCRIPT_DIR}/../.env" | grep -v '^$' | sed 's/^/export /')
    set +a
fi

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

NODE1_IP="192.168.11.10"
NODE1_NAME="ML110-01"
NODE1_TOKEN="${PROXMOX_TOKEN_ML110_01:-}"
NODE2_IP="192.168.11.11"
NODE2_NAME="R630-01"
NODE2_TOKEN="${PROXMOX_TOKEN_R630_01:-}"

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

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

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

list_images() {
    local endpoint=$1
    local token=$2
    local node_name=$3
    
    echo ""
    info "=== ${node_name} Available Images ==="
    echo ""
    
    # Get all storage pools
    local storages=$(curl -k -s -H "Authorization: PVEAPIToken ${token}" \
        "${endpoint}/api2/json/storage" 2>/dev/null | \
        jq -r '.data[]? | select(.content | contains("iso") or contains("vztmpl")) | .storage')
    
    if [ -z "$storages" ]; then
        warn "No storage pools found with ISO or template content"
        return
    fi
    
    local has_images=false
    
    for storage in $storages; do
        local content=$(curl -k -s -H "Authorization: PVEAPIToken ${token}" \
            "${endpoint}/api2/json/storage/${storage}/content" 2>/dev/null)
        
        # Check for ISO files
        local isos=$(echo "$content" | jq -r '.data[]? | select(.content == "iso") | "\(.volid) | \(.size | tonumber / 1024 / 1024 / 1024 | floor)GB"')
        
        if [ -n "$isos" ]; then
            has_images=true
            echo "📀 ISO Images in ${storage}:"
            echo "$isos" | while IFS='|' read -r volid size; do
                echo "  • ${volid} (${size})"
            done
            echo ""
        fi
        
        # Check for container templates
        local templates=$(echo "$content" | jq -r '.data[]? | select(.content == "vztmpl") | "\(.volid) | \(.size | tonumber / 1024 / 1024 / 1024 | floor)GB"')
        
        if [ -n "$templates" ]; then
            has_images=true
            echo "📦 Container Templates in ${storage}:"
            echo "$templates" | while IFS='|' read -r volid size; do
                echo "  • ${volid} (${size})"
            done
            echo ""
        fi
        
        # Check for disk images (raw, qcow2, etc.)
        local disk_images=$(echo "$content" | jq -r '.data[]? | select(.volid | contains(".img") or contains(".raw") or contains(".qcow2")) | "\(.volid) | \(.size | tonumber / 1024 / 1024 / 1024 | floor)GB"')
        
        if [ -n "$disk_images" ]; then
            has_images=true
            echo "💾 Disk Images in ${storage}:"
            echo "$disk_images" | while IFS='|' read -r volid size; do
                echo "  • ${volid} (${size})"
            done
            echo ""
        fi
    done
    
    if [ "$has_images" = false ]; then
        warn "No ISO files, templates, or disk images found"
    fi
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║          Proxmox Image Inventory                             ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    if [ -z "$NODE1_TOKEN" ] || [ -z "$NODE2_TOKEN" ]; then
        warn "Proxmox API tokens not found in .env file"
        exit 1
    fi
    
    list_images "https://${NODE1_IP}:8006" "${NODE1_TOKEN}" "${NODE1_NAME}"
    list_images "https://${NODE2_IP}:8006" "${NODE2_TOKEN}" "${NODE2_NAME}"
    
    echo ""
    info "Note: This script lists images available via API."
    info "Some images may require additional permissions to list."
    echo ""
}

main "$@"

