#!/usr/bin/env bash

###############################################################################
#
# Copyright 2024 NVIDIA Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
###############################################################################

set -e

RES_FOLDER="/opt/mellanox/doca/tools/resources"

OFED_VER="24.04-0.6.6.0"

BF_VER=
K_VER=

tmp_folders=()

create_tmp_folder() {
    folder=$(mktemp -d /tmp/DOCA.XXXXXXXXXX)
    tmp_folders+=( "$folder" )
    printf -v "$1" -- "$folder"
}

remove_tmp_folders() { rm -rf -- "${tmp_folders[@]}"; }


compile_install_ofed() {

    TMP_DIR=$1
    KVER=$2

    pushd $TMP_DIR > /dev/null
    TGZ_FILE=$(ls ${RES_FOLDER}/MLNX_OFED*${OFED_VER}*tgz)
    cp $TGZ_FILE ./
    tar xzf ${TGZ_FILE}
    cd MLNX_OFED_SRC*${OFED_VER}
    ./install.pl -k ${KVER} --kernel-sources /lib/modules/${KVER}/build \
        --kernel-only

    cd ..
    find ./ -type f '(' \( -name '*.rpm' -a '!' -name '*.src.rpm' \)  -o -name '*.deb' ')' -exec mv '{}' ./ \;
    rm -rf MLNX_OFED_SRC*${OFED_VER}*
}

echo_intro() {
    base64 -d <<<"H4sIAAAAAAAAA9VSQQ7DIAy78wofuPODvSQSH+HxgyRu3W7ltMsiVXOCMbE19CjM2iOcrQxyiocqI4EBbYu8yD4GnF76q36NssnaIColu4L25remXewJcP24d0ewQL1nn/ohPGhv7bDO1waH20Gw0089xH3p3cx83VoopyOI25zZTn8uKnloPo5f69ex85MdL6eRWks4bmAWipZ3vqn5eLItljVwJ7IhRmqRBOwTfdOPZAeJFhufbHDg+zOpNHZHxhbHxTy6nJiylVce/re/qn/XfwM00Tm0fwQAAA==" | gunzip
    echo ""
    echo "For more information visit:"
    echo "- https://github.com/Mellanox/bfb-build"
    echo "- https://developer.nvidia.com/networking/doca"
    echo ""

    if [[ $EUID -ne 0 ]]; then
        echo "Note:"
        echo "You might want to run the script as root"
        echo "This script will attempt to install some dependencies to compile DOCA base drivers (OFED)"
        echo ""
    fi

}

parse_args() {

    options=`getopt -n doca-kernel-support -o k:h -l kernel:,help -- "$@"`

    KERNEL_VERSION=${KERNEL_VERSION}
    eval set -- $options
    while [ "$1" != -- ]; do
        case $1 in
            --help|-h) usage; exit 0 ;;
            --kernel|-k) shift; KERNEL_VERSION=$1 ;;
        esac
        shift
    done
    shift

    if [ -z "${KERNEL_VERSION}" ]; then
        KERNEL_VERSION=$(uname -r)
        echo "No kernel version provided, defaulting to current running kernel ${KERNEL_VERSION}"
        echo "Use --kernel or the environment variable KERNEL_VERSION to override default kernel version"
    else
        echo "Using kernel version ${KERNEL_VERSION}"
    fi

    # BF_VER="${BLUEFIELD_VERSION}"
    K_VER="${KERNEL_VERSION}"
}

main() {

    create_tmp_folder tmp_folder

    echo_intro
    parse_args "$@"

    compile_install_ofed $tmp_folder $K_VER

    echo "Binaries available here:"
    echo $tmp_folder

    exit 0
}

main "$@"
