#!/bin/bash
# download-ubuntu-image.sh
# Downloads Ubuntu 22.04 cloud image to Proxmox storage

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'
NC='\033[0m'

STORAGE="${STORAGE:-local}"
NODE1_IP="192.168.11.10"
NODE2_IP="192.168.11.11"
IMAGE_NAME="ubuntu-22.04-server-cloudimg-amd64.img"
IMAGE_URL="https://cloud-images.ubuntu.com/releases/22.04/release/${IMAGE_NAME}"

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

error() {
    echo -e "${RED}[ERROR]${NC} $1" >&2
    exit 1
}

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

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

download_image() {
    local node_ip=$1
    local node_name=$2
    local token=$3
    
    log "Downloading Ubuntu 22.04 cloud image for ${node_name}..."
    
    # Check if image already exists
    local existing=$(curl -k -s -H "Authorization: PVEAPIToken ${token}" \
        "https://${node_ip}:8006/api2/json/storage/${STORAGE}/content" 2>/dev/null | \
        jq -r ".data[]? | select(.volid | contains(\"${IMAGE_NAME}\")) | .volid")
    
    if [ -n "$existing" ]; then
        warn "Image already exists: ${existing}"
        return 0
    fi
    
    info "Image not found. Download instructions:"
    echo ""
    echo "Option 1: Download via SSH (Recommended):"
    echo "  ssh root@${node_ip}"
    echo "  wget ${IMAGE_URL}"
    echo "  mv ${IMAGE_NAME} /var/lib/vz/template/iso/"
    echo ""
    echo "Option 2: Download via Proxmox Web UI:"
    echo "  1. Log in to https://${node_ip}:8006"
    echo "  2. Go to: Datacenter → Storage → ${STORAGE} → Content"
    echo "  3. Click 'Upload' → Select file → Upload"
    echo ""
    echo "Option 3: Use pveam (if template available):"
    echo "  ssh root@${node_ip}"
    echo "  pveam available | grep ubuntu-22.04"
    echo "  pveam download ${STORAGE} ubuntu-22.04-standard_22.04-1_amd64.tar.gz"
    echo ""
}

main() {
    echo ""
    echo "╔══════════════════════════════════════════════════════════════╗"
    echo "║          Ubuntu Image Download Helper                        ║"
    echo "╚══════════════════════════════════════════════════════════════╝"
    echo ""
    
    info "Target Image: ${IMAGE_NAME}"
    info "Storage: ${STORAGE}"
    echo ""
    
    if [ -z "${PROXMOX_TOKEN_ML110_01:-}" ] || [ -z "${PROXMOX_TOKEN_R630_01:-}" ]; then
        warn "Proxmox API tokens not found in .env file"
        warn "Providing manual download instructions instead"
        echo ""
    else
        download_image "${NODE1_IP}" "ML110-01" "${PROXMOX_TOKEN_ML110_01}"
        download_image "${NODE2_IP}" "R630-01" "${PROXMOX_TOKEN_R630_01}"
    fi
    
    echo ""
    info "Image URL: ${IMAGE_URL}"
    info "File Size: ~600MB"
    echo ""
    log "Download complete or instructions provided"
    echo ""
}

main "$@"

