#!/usr/bin/env bash
#
# This script load the compiled xdma driver (xdma.ko) as kernel module

# Enable unofficial bash strict mode.
set -euo pipefail

script_directory="$( (builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) )"

# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Remove the existing xdma kernel module
if lsmod | grep xdma; then
   rmmod xdma
fi

echo -n "Loading driver..."

if ! insmod ${script_directory}/xdma.ko; then
  echo "Error: Kernel module did not load properly."
  echo " FAILED"
  exit 1
fi

# Check to see if the xdma devices were recognized
echo ""
cat /proc/devices | grep xdma > /dev/null
returnVal=$?
if [ $returnVal == 0 ]; then
  # Installed devices were recognized.
  echo "The Kernel module installed correctly and the xmda devices were recognized."
else
  # No devices were installed.
  echo "Error: The Kernel module installed correctly, but no devices were recognized."
  echo " FAILED"
  exit 1
fi

echo " DONE"
