#!/bin/bash
# Identify all containers using DHCP
# Shows current DHCP-assigned IPs

set -euo pipefail

INVENTORY_CSV=$(ls -t /home/intlc/projects/proxmox/container_inventory_*.csv 2>/dev/null | head -1)

if [ -z "$INVENTORY_CSV" ] || [ ! -f "$INVENTORY_CSV" ]; then
    echo "Error: No inventory CSV file found. Please run scan-all-containers.py first."
    exit 1
fi

OUTPUT_FILE="/home/intlc/projects/proxmox/DHCP_CONTAINERS_$(date +%Y%m%d_%H%M%S).md"

echo "=== Identifying DHCP Containers ==="
echo ""

cat > "$OUTPUT_FILE" << EOF
# DHCP Containers - Complete List

**Generated**: $(date)
**Source**: $INVENTORY_CSV

---

## DHCP Containers

| VMID | Name | Host | Status | Current DHCP IP | Hostname |
|------|------|------|--------|----------------|----------|
EOF

dhcp_count=0

# Read CSV and filter DHCP containers
tail -n +2 "$INVENTORY_CSV" | while IFS=',' read -r vmid name host status ip_config current_ip hostname; do
    # Remove quotes from name
    name=$(echo "$name" | tr -d '"')
    
    if [ "$ip_config" = "dhcp" ] || [ "$ip_config" = "auto" ]; then
        dhcp_count=$((dhcp_count + 1))
        echo "| $vmid | $name | $host | $status | $current_ip | $hostname |" >> "$OUTPUT_FILE"
        echo "  VMID $vmid ($name) on $host - Current IP: $current_ip"
    fi
done

# Count total
dhcp_count=$(tail -n +2 "$INVENTORY_CSV" | grep -c ",dhcp," || echo "0")
if [ "$dhcp_count" -eq 0 ]; then
    dhcp_count=$(tail -n +2 "$INVENTORY_CSV" | grep -c ",auto," || echo "0")
fi

echo ""
echo "=== Summary ==="
echo "Total DHCP containers: $dhcp_count"
echo "Output file: $OUTPUT_FILE"
