#!/usr/bin/env bash
#
# Plug in all framegrabbers (rescan PCIe bus).

# Enable unofficial bash strict mode.
set -euo pipefail
IFS=$'\n\t'

# Check root permissions.
if [[ "$EUID" -ne 0 ]]; then
  echo "Please run with sudo" >&2
  exit 1
fi

# Check motherboard.
board="$(cat /sys/class/dmi/id/board_name)"
if [[ "${board}" != "X11DPG-QT" && "${1:-}" != "--force" ]]; then
  echo "Unsupported motherboard: Hot swapping may damage hardware (use --force to override)" >&2
  exit 1
fi

# Rescan PCI & check for Xilinx devices.
echo "Detected '${board}', attempting to rescan pci bus."
echo 1 > /sys/bus/pci/rescan
sleep 1
if ! plugged="$(lspci | grep Xilinx)"; then
  echo "Could not detect any Xilinx devices." >&2
  exit 1
fi

# Show Xilinx devices.
echo "Plugged in:"
echo "$plugged"
