#!/bin/bash

# -- 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
XRT_PROG=xbutil

# Determine which version of xbutil should be used
# Check to see environment variable is NOT set
if [ -z ${XRT_TOOLS_NEXTGEN+x} ]; then 
  XRT_PROG=xbutil
elif [ "${XRT_TOOLS_NEXTGEN,,}" = "true" ]; then
     XRT_PROG=xbutil2
fi

# -- Examine the options
XRTWARP_PROG_ARGS_size=0
XRTWRAP_PROG_ARGS=()
while [ $# -gt 0 ]; do
  case "$1" in
    # Indicates that the new version of xbutil (e.g., xbutil2) should be used
    -new|--new)
      XRT_PROG=xbutil2
      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

# Friendly message
if [ ${XRT_PROG} == "xbutil2" ]; then
  echo "-----------------------------------------------------"
  echo " Invoking next generation of the xbutil application "
  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[@]}"
