#!/usr/bin/env bash

###############################################################################
#
# Copyright 2022 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="23.10-7.1.8.0"

BF_VER=
K_VER=

tmp_folders=()

create_tmp_folder() {
    folder=$(mktemp -d)
    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
    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
}

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 b:k:s:h -l bluefield:,kernel:,src-archive:,help -- "$@"`
    options=`getopt -n doca-kernel-support -o k:h -l kernel:,help -- "$@"`

    # BLUEFIELD_VERSION=${BLUEFIELD_VERSION}
    KERNEL_VERSION=${KERNEL_VERSION}
    # SRC_ARCHIVE_URL=${SRC_ARCHIVE_URL}
    eval set -- $options
    while [ "$1" != -- ]; do
        case $1 in
            --help|-h) usage; exit 0 ;;
            # --bluefield|-b) shift; BLUEFIELD_VERSION=$1 ;;
            --kernel|-k) shift; KERNEL_VERSION=$1 ;;
            # --src-archive|-s) shift; SRC_ARCHIVE_URL=$1 ;;
        esac
        shift
    done
    shift

    # if [ -z "${BLUEFIELD_VERSION}" ]; then
    #     BLUEFIELD_VERSION="latest"
    #     echo "No BlueField version provided, defaulting to latest BlueField"
    #     echo "Use --bluefield or the environment variable BLUEFIELD_VERSION to provide BlueField version"
    # else
    #     echo "Using BlueField version ${BLUEFIELD_VERSION}"
    # fi

    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() {
    trap remove_tmp_folders EXIT
    create_tmp_folder tmp_folder

    echo_intro
    parse_args "$@"

    compile_install_ofed $tmp_folder $K_VER

    exit 0
}

main "$@"
