#!/bin/bash
###############################################################################
# add-datanet-nics.sh
#
# Adds a second virtio NIC (net1) to all k8s + ultix VMs, bridged to the
# storage/datanet network (VLAN 1000). Records the assigned IP in phpIPAM.
#
# Runs from the workstation — uses tests/remote.sh to reach each hypervisor.
# Hot-adds the NIC (no VM downtime). Guest-side IP config must be done
# separately (the guest OS needs the IP configured on the new interface).
#
# Usage:
#   bash add-datanet-nics.sh             # add NICs (hot-add, no reboot)
#   bash add-datanet-nics.sh --status    # show current state only
#
# IP allocations (phpIPAM VPTechOps, subnet 10.100.100.0/24):
#   .10 = cnode1    (102/tsys1)
#   .11 = cnode2    (705/tsys7)
#   .12 = cnode3    (603/tsys6)
#   .13 = wnode-tsys3 (313/tsys3)
#   .14 = wnode-tsys5 (500/tsys5)
#   .15 = wnode-tsys6 (601/tsys6)
#   .16 = wnode-tsys7 (701/tsys7)
#   .17 = wnode-tsys9 (905/tsys9)
#   .18 = ultix-streaming (5111/tsys5)
#   .19 = ultix-offstage  (5112/tsys5)
#
# Related: Redmine [#396]
###############################################################################
set -euo pipefail

# VM definitions: VMID HOST BRIDGE IP HOSTNAME
VMS=(
    "102  tsys1  datanet      10.100.100.10  pfv-k8s-cnode1"
    "705  tsys7  datanet      10.100.100.11  pfv-k8s-cnode2"
    "603  tsys6  storagenet   10.100.100.12  pfv-k8s-cnode3"
    "313  tsys3  datanet      10.100.100.13  pfv-k8s-wnode-tsys3"
    "500  tsys5  datanet      10.100.100.14  pfv-k8s-wnode-tsys5"
    "601  tsys6  storagenet   10.100.100.15  pfv-k8s-wnode-tsys6"
    "701  tsys7  datanet      10.100.100.16  pfv-k8s-wnode-tsys7"
    "905  tsys9  datanet      10.100.100.17  pfv-k8s-wnode-tsys9"
    "5111 tsys5  datanet      10.100.100.18  ultix-streaming"
    "5112 tsys5  datanet      10.100.100.19  ultix-offstage"
)

ACTION="${1:-add}"

echo "==================================================================="
echo " add-datanet-nics — [#396]"
echo " mode: ${ACTION}"
echo "==================================================================="
echo ""

for entry in "${VMS[@]}"; do
    read -r vmid host bridge ip hostname <<< "$entry"
    prox_host="pfv-${host}"

    echo "--- ${hostname} (VM ${vmid} on ${prox_host}) ---"

    if [ "$ACTION" = "--status" ]; then
        # Show current NIC state
        PROX_HOST="$prox_host" bash tests/remote.sh prox \
            "qm config ${vmid} 2>/dev/null | grep -E '^net|^name'" 2>&1
        echo ""
        continue
    fi

    # Check if net1 already exists
    existing=$(PROX_HOST="$prox_host" bash tests/remote.sh prox \
        "qm config ${vmid} 2>/dev/null | grep '^net1'" 2>&1 || true)

    if [ -n "$existing" ]; then
        echo "  net1 already exists: ${existing}"
        echo "  Skipping."
        echo ""
        continue
    fi

    # Hot-add net1 bridged to the storage network
    echo "  Adding net1 (bridge=${bridge}, IP=${ip})..."
    if PROX_HOST="$prox_host" bash tests/remote.sh prox \
        "qm set ${vmid} -net1 virtio,bridge=${bridge}" 2>&1; then
        echo "  NIC added. Verify with: qm config ${vmid} | grep net1"
    else
        echo "  FAILED — check error above"
    fi
    echo ""
done

if [ "$ACTION" != "--status" ]; then
    echo "==================================================================="
    echo " NICs added. Guest-side IP config still needed."
    echo " Each guest needs the IP configured on the new interface."
    echo " IPs are allocated in phpIPAM (VPTechOps / 10.100.100.0/24)."
    echo "==================================================================="
fi
