#!/bin/bash

vmid="$1"
vmbr="$2"
vlan="$3"
quantity="$4"

if [[ -z "$vmid" || -z "$vmbr" || -z "$vlan" || -z "$quantity" ]]; then
    echo "Usage: $0 <vmid> <vmbridge> <vlanid> <quantity>"
    exit 1
fi

# Get the highest existing net index
last_net_index=$(qm config "$vmid" | grep -oP '^net\d+' | sed 's/net//' | sort -n | tail -n1)
next_index=$((last_net_index + 1))

echo "✅ Adding $quantity interfaces to VM $vmid on $vmbr with VLAN $vlan"

for ((i = 0; i < quantity; i++)); do
    net_id=$((next_index + i))
    echo "➕ Adding net${net_id}: virtio,bridge=$vmbr,tag=$vlan"
    
    qm set "$vmid" -net${net_id} "virtio,bridge=${vmbr},tag=${vlan}" >/dev/null || {
        echo "❌ Failed to add net${net_id}"
    }
    
    qm set "$vmid" --ipconfig${net_id} "ip=dhcp"
    
    
done

echo "✅ Done adding interfaces."
