#!/bin/sh

echo "Staring NVIDIA IMEX installation"

echo "Checking for running IMEX service"

STATUS=`systemctl is-active nvidia-imex`
  if [ ${STATUS} = 'active' ]; then
    echo "IMEX service is running, stopping the same....."
    systemctl stop nvidia-imex
  else
    echo "IMEX service is not running....."
  fi

# copy all the files
echo "Copying files to desired location"
# choose lib location based on system arch
ARCH_TYPE_CMD=`uname -m`
LIB_LOC="/usr/lib/x86_64-linux-gnu"
if [ ${ARCH_TYPE_CMD} = 'aarch64' ]; then
    echo "detected aarch64 and changing lib location to /usr/lib/aarch64-linux-gnu...."
    LIB_LOC="/usr/lib/aarch64-linux-gnu"
fi

cp ${PWD}/nvidia-imex  /usr/bin
cp ${PWD}/nvidia-imex-ctl /usr/bin
cp ${PWD}/nvidia-imex.service  /lib/systemd/system

mkdir /etc/nvidia-imex > /dev/null 2>&1
cp ${PWD}/config.cfg  /etc/nvidia-imex/config.cfg

mkdir -p /usr/share/nvidia/imex/doc > /dev/null 2>&1
cp ${PWD}/LICENSE  /usr/share/nvidia/imex/doc/
cp ${PWD}/third-party-notices.txt /usr/share/nvidia/imex/doc/


# let the user start IMEX service manually.
echo "IMEX installation completed."
echo "Note: IMEX service is not started. Start it using systemctl commands (like systemctl start nvidia-imex)"

