#!/bin/bash

get_script_path() (
    script="$1"
    while [ -L "$script" ] ; do
        # combining next two lines fails in zsh shell
        script_dir=$(command dirname -- "$script")
        script_dir=$(cd "$script_dir" && command pwd -P)
        script="$(readlink "$script")"
        case $script in
            (/*) ;;
            (*) script="$script_dir/$script" ;;
        esac
    done
    # combining next two lines fails in zsh shell
    script_dir=$(command dirname -- "$script")
    script_dir=$(cd "$script_dir" && command pwd -P)
    printf "%s" "$script_dir"
)

_vars_get_proc_name() {
    if [ -n "${ZSH_VERSION:-}" ] ; then
        script="$(ps -p "$$" -o comm=)"
    else
        script="$1"
        while [ -L "$script" ] ; do
            script="$(readlink "$script")"
        done
    fi
    basename -- "$script"
}

_vars_this_script_name="setvars.sh"
if [ "$_vars_this_script_name" = "$(_vars_get_proc_name "$0")" ] ; then
    echo "   ERROR: Incorrect usage: this script must be sourced."
    echo "   Usage: . path/to/${_vars_this_script_name}"
    return 255 2>/dev/null || exit 255
fi

vars_script_name=""
vars_script_shell="$(ps -p "$$" -o comm=)"

if [ -n "${ZSH_VERSION:-}" ] && [ -n "${ZSH_EVAL_CONTEXT:-}" ] ; then
    case $ZSH_EVAL_CONTEXT in (*:file*) vars_script_name="${(%):-%x}" ;; esac ;
elif [ -n "${KSH_VERSION:-}" ] ; then
    if [ "$(set | grep -Fq "KSH_VERSION=.sh.version" ; echo $?)" -eq 0 ] ; then
        vars_script_name="${.sh.file}" ;
    else
        vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
        vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: \(.*\)\[[0-9]*\]:')" ;
    fi
elif [ -n "${BASH_VERSION:-}" ] ; then
    (return 0 2>/dev/null) && vars_script_name="${BASH_SOURCE}" ;
elif [ "dash" = "$vars_script_shell" ] ; then
    vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
    vars_script_name="$(expr "${vars_script_name:-}" : '^.*dash: [0-9]*: \(.*\):')" ;
elif [ "sh" = "$vars_script_shell" ] ; then
    vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
    if [ "$(printf "%s" "$vars_script_name" | grep -Eq "sh: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then
        vars_script_name="$(expr "${vars_script_name:-}" : '^.*sh: [0-9]*: \(.*\):')" ;
    fi
else
    vars_script_name="$( (echo "${.sh.file}") 2>&1 )" || : ;
    if [ "$(printf "%s" "$vars_script_name" | grep -Eq "^.+: [0-9]+: .*vars\.sh: " ; echo $?)" -eq 0 ] ; then # dash
        vars_script_name="$(expr "${vars_script_name:-}" : '^.*: [0-9]*: \(.*\):')" ;
    else
        vars_script_name="" ;
    fi
fi

if [ "" = "$vars_script_name" ] ; then
    >&2 echo "   ERROR: Unable to proceed: possible causes listed below."
    >&2 echo "   This script must be sourced. Did you execute or source this script?" ;
    >&2 echo "   Unrecognized/unsupported shell (supported: bash, zsh, ksh, m/lksh, dash)." ;
    >&2 echo "   May fail in dash if you rename this script (assumes \"setvars.sh\")." ;
    >&2 echo "   Can be caused by sourcing from ZSH version 4.x or older." ;
    return 255 2>/dev/null || exit 255
fi

WORK_DIR=$(get_script_path "${vars_script_name:-}")
CCL_ROOT="$(cd "${WORK_DIR}"/../; pwd -P)"; export CCL_ROOT
export I_MPI_ROOT="${CCL_ROOT}"

source ${CCL_ROOT}/env/vars.sh ${1:-}

if [ -z "${PATH}" ]
then
    PATH="${CCL_ROOT}/bin"; export PATH
else
    PATH="${CCL_ROOT}/bin:${PATH}"; export PATH
fi
LD_LIBRARY_PATH=$(prepend_path "${I_MPI_ROOT}/opt/mpi/libfabric/lib" "${LD_LIBRARY_PATH:-}") ; export LD_LIBRARY_PATH
FI_PROVIDER_PATH="${I_MPI_ROOT}/opt/mpi/libfabric/lib/prov:/usr/lib64/libfabric"; export FI_PROVIDER_PATH
