#!/bin/bash

#
# SPDX-License-Identifier: Apache-2.0
#
# Copyright (C) 2020-2021 Xilinx, Inc.
#
# xbmgmt loader which automatically switches between legacy and next-gen
# xbmgmt utility. Starting with 2021.1 the default is next-gen xbmgmt utility.
#

# -- Detect a Windows environment and automatically switch to the .bat file
if [[ "`uname`" == windows32* ]] || [[ "`uname`" == CYGWIN* ]] ; then
    trap "" INT
    "$0.bat" "$@"
    exit $?
fi

# Working variables
DEFAULT=xbmgmt2
XBMGMT=xbmgmt
XBMGMT2=xbmgmt2
XRT_PROG=""

# Table to resolve reset sub-command based on switch passed to reset
declare -A RSTOPT=( \
        ["--hot"]="XBMGMT" \
        ["--kernel"]="XBMGMT" \
        ["--ecc"]="XBMGMT" \
        ["--softkernel"]="XBMGMT" \
        ["--ert"]="XBMGMT" \
        ["--card"]="XBMGMT" \
        ["--force"]="XBMGMT" \
        ["--device"]="XBMGMT2" \
        ["--type"]="XBMGMT2" \
        ["--help"]="XBMGMT2" \
        ["-d"]="XBMGMT2" \
        ["-r"]="XBMGMT2" \
        ["-h"]="XBMGMT2" \
    )

# In the following table "reset" points to a second table by name.
# Note that reset is valid both for legacy and next-gen tool and hence
# requires a second lookup to resolve between xbmgmt and xbmgmt2
declare -A COMMANDS=( \
        ["help"]="XBMGMT" \
        ["version"]="XBMGMT" \
        ["--version"]="XBMGMT" \
        ["scan"]="XBMGMT" \
        ["flash"]="XBMGMT" \
        ["-lash"]="XBMGMT" \
        ["reset"]="RSTOPT" \
        ["clock"]="XBMGMT" \
        ["partition"]="XBMGMT" \
        ["config"]="XBMGMT" \
        ["nifd"]="XBMGMT" \
        ["hotplug"]="XBMGMT" \
        ["examine"]="XBMGMT2" \
        ["program"]="XBMGMT2" \
        ["dump"]="XBMGMT2" \
        ["advanced"]="XBMGMT2" \
        ["--help"]="XBMGMT2" \
        ["--verbose"]="XBMGMT2" \
        ["--batch"]="XBMGMT2" \
        ["--force"]="XBMGMT2" \
    )


# Examine the options and look for -new/--new
XRTWARP_PROG_ARGS_size=0
XRTWRAP_PROG_ARGS=()
while [ $# -gt 0 ]; do
    case "$1" in
	# Indicates that the new version of xbmgmt (e.g., xbmgmt2) should be used
	-new|--new)
	    XRT_PROG=xbmgmt2
	    shift
	    ;;
	# Indicates that the legacy version of xbutil should be used
	-legacy|--legacy)
	    XRT_PROG=xbmgmt
	    shift
	    ;;
	# Copy the options the remaining options
	*)
	    XRTWRAP_PROG_ARGS[$XRTWARP_PROG_ARGS_size]="$1"
	    XRTWARP_PROG_ARGS_size=$(($XRTWARP_PROG_ARGS_size + 1))
	    shift
	    ;;
    esac
done

# Determine which version of xbmgmt should be used
# Check to see environment variable is NOT set
if [ "${XRT_TOOLS_NEXTGEN,,}" = "true" ]; then
    XRT_PROG=xbmgmt2
fi

# If user did not specify -legacy/--legacy then attempt to determine if
# user wants to use legacy or next-gen by peeking inside the command line.
# Ideally with bash 'nameref' the case/esac would not be necessary and
# we should be able to deref in one step using helper declaration like
# declare -n PROG=${COMMANDS[${XRTWRAP_PROG_ARGS[0]}]}
# However RHEL 7.X still uses bash 4.2 which does not support nameref :-(

if [[ -z ${XRT_PROG} ]] && [[ ${#XRTWRAP_PROG_ARGS[@]} -gt 0 ]]; then
    PROG=${COMMANDS[${XRTWRAP_PROG_ARGS[0]}]}
    case ${PROG} in
	XBMGMT | XBMGMT2)
	# Non ambiguous command line argument, utility already resolved
	;;
	RSTOPT)
	    # We have an ambiguous sub-command, use the next token/switch to
	    # disambiguate by a second look up
	    PROG="DEFAULT"
	    if [ ${#XRTWRAP_PROG_ARGS[@]} -gt 1 ]; then
		PROG=${RSTOPT[${XRTWRAP_PROG_ARGS[1]}]:-${DEFAULT}}
	    fi
	    ;;
	*)
	    # Bogus sub-command which failed lookup
	    PROG="DEFAULT"
	    ;;
    esac
    # Get named reference to the value pointed by the string returned
    XRT_PROG=${!PROG}
fi

# Default to the new gen tool if we still could not disambuguate the choice
if [[ -z ${XRT_PROG} ]]; then
    XRT_PROG=${DEFAULT}
fi

# Friendly message if running in a terminal
if [[ ${XRT_PROG} == "xbmgmt" ]] && [[ -t 1 ]]; then
    echo "---------------------------------------------------------------------"
    echo "Deprecation Warning:
    The given legacy sub-command and/or option has been deprecated
    to be obsoleted in the next release.
 
    Further information regarding the legacy deprecated sub-commands
    and options along with their mappings to the next generation
    sub-commands and options can be found on the Xilinx Runtime (XRT)
    documentation page:
    
    https://xilinx.github.io/XRT/master/html/xbtools_map.html

    Please update your scripts and tools to use the next generation
    sub-commands and options."
    echo "---------------------------------------------------------------------"
fi

# -- Find and call the loader
XRT_LOADER="`dirname \"$0\"`/unwrapped/loader"

if [ ! -f "$XRT_LOADER" ]; then
    echo "ERROR: Could not find 64-bit loader executable."
    echo "ERROR: ${XRT_LOADER} does not exist."
    exit 1
fi

"${XRT_LOADER}" -exec ${XRT_PROG} "${XRTWRAP_PROG_ARGS[@]}"
