#!/bin/sh
# This script adds the required source files for building PKA kernel module.
# It does so by cloning the upstream git repo for libpka and copying the necessary files.
# When "clean" argument is passed to this script, it removes the files copied above.
# The clean functionality is useful/used while doing a clean rebuilding of debian package.

set -e

# Mellanox Github URL
MLX_GHB_URL="https://github.com/Mellanox"

# PKA repository.
PKA_REPO="pka"

# PKA source directory
PKA_SRC="$PKA_REPO"

# Files to be included from PKA libbrary.
PKA_LIB_FILES=" \
  lib/pka_dev.c \
  lib/pka_dev.h \
  lib/pka_ring.h \
  include/pka_firmware.h \
  include/pka_utils.h \
  include/pka_addrs.h \
  include/pka_config.h \
  include/pka_common.h \
  include/pka_debug.h \
  include/pka_types.h \
  include/pka_mmio.h \
  include/pka_ioctl.h \
  include/pka_cpu.h \
"

usage()
{
cat << EOF

Usage: `basename $0` [clean]

EOF
}

# Perform "clean" if the argument is passed.
if [ "x$1" = "xclean" ]; then
	for sf in $PKA_LIB_FILES
	do
		rm -f $(basename $sf)
	done
	exit 0
fi

# Check for proper arguments for this script.
if [ ! "x$1" = "x" ]; then
	usage
	exit 1
fi

# Check if the source directory exists.
# If not, clone it.
if [ ! -d "$PKA_REPO" ]; then
	git clone $MLX_GHB_URL/$PKA_REPO.git
fi

# Copy all the required files from the source directory.
for sf in $PKA_LIB_FILES
do
	cp -f $PKA_SRC/$sf ./
done

# Remove the cloned source reposotiry.
if [ -d "$PKA_REPO" ]; then
	rm -rf $PKA_REPO
fi
