#!/usr/bin/env bash
# Legacy T036 VM cluster deployment script.
# This is a manual bare-metal/PXE path. The canonical local VM validation path
# is nix/test-cluster/run-cluster.sh.

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

cd "$REPO_ROOT"

echo "=== T036 VM Cluster Deployment ==="
echo ""
echo "This path is archived. Prefer: nix run ./nix/test-cluster#cluster -- smoke"
echo ""
echo "Prerequisites:"
echo "  - PXE server booted and network configured (192.168.100.1)"
echo "  - Node01 booted and network configured (192.168.100.11)"
echo "  - Node02 booted and network configured (192.168.100.12)"
echo "  - Node03 booted and network configured (192.168.100.13)"
echo ""
echo "Press Enter to start deployment..."
read

echo ""
echo "Step 1: Verify SSH connectivity to all VMs..."
for host in 192.168.100.1 192.168.100.11 192.168.100.12 192.168.100.13; do
    echo -n "  Checking $host... "
    if ssh -o ConnectTimeout=5 -o StrictHostKeyChecking=no root@$host 'echo OK' 2>/dev/null; then
        echo "✓"
    else
        echo "✗ FAILED"
        echo "ERROR: Cannot connect to $host"
        echo "Please verify network configuration via VNC"
        exit 1
    fi
done

echo ""
echo "Step 2: Deploy PXE Server (192.168.100.1)..."
nixos-anywhere --flake .#pxe-server root@192.168.100.1

echo ""
echo "Step 3: Deploy Node01 (192.168.100.11)..."
nixos-anywhere --flake .#node01 root@192.168.100.11

echo ""
echo "Step 4: Deploy Node02 (192.168.100.12)..."
nixos-anywhere --flake .#node02 root@192.168.100.12

echo ""
echo "Step 5: Deploy Node03 (192.168.100.13)..."
nixos-anywhere --flake .#node03 root@192.168.100.13

echo ""
echo "=== Deployment Complete ==="
echo ""
echo "All VMs have been provisioned. Systems will reboot from disk."
echo "Wait 2-3 minutes for boot, then validate cluster..."
echo ""
echo "Legacy next step: baremetal/vm-cluster/legacy/validate-cluster.sh"
echo "Preferred validation path: nix run ./nix/test-cluster#cluster -- smoke"
