#!/bin/bash
#
# Copyright (c) 2021-2024 NVIDIA CORPORATION & AFFILIATES, ALL RIGHTS RESERVED.
#
# This software product is a proprietary product of NVIDIA CORPORATION &
# AFFILIATES (the "Company") and all right, title, and interest in and to the
# software product, including all associated intellectual property rights, are
# and shall remain exclusively with the Company.
#
# This software product is governed by the End User License Agreement
# provided with the software product.
#
# Bring up DOCA BlueMan service
# example: ./bring_up_doca_blueman_service.sh

cd ${0%*/*}

doca_blueman_fe_image=`/bin/ls doca_blueman_fe_service*_arm64.tar`
retcode=$?
if [[ "$retcode" != "0" ]]; then
    echo "ERROR: DOCA BlueMan fe container image not exist"
    exit $retcode
fi


doca_blueman_conv_image=`/bin/ls doca_blueman_conv_service*_arm64.tar`
retcode=$?
if [[ "$retcode" != "0" ]]; then
    echo "ERROR: DOCA BlueMan conv container image not exist"
    exit $retcode
fi


doca_blueman_standalone_yaml=`/bin/ls doca_blueman_standalone.yaml`
retcode=$?
if [[ "$retcode" != "0" ]]; then
    echo "ERROR: DOCA BlueMan standalone yaml file not exist"
    exit $retcode
fi


ex()
{
	eval "$@"
	rc=$?
	if [ $rc -ne 0 ]; then
		echo "ERROR: Failed executing $@ RC: $rc"
		exit $rc
	else
	  echo "$@ executed successfully"
	fi
}

echo "Generate BlueMan keys"
ex ./generate_blueman_keys.sh

echo "Start DPE service"
ex systemctl start dpe.service

for blueman_image in $doca_blueman_fe_image $doca_blueman_conv_image
do
  echo "Import image $blueman_image to crictl"
  ex ctr --namespace k8s.io image import $blueman_image
done

echo "Copy $doca_blueman_standalone_yaml to /etc/kubelet.d/"
ex cp $doca_blueman_standalone_yaml /etc/kubelet.d/

exit 0