#!/bin/sh

#
# This script is used to handle the problems introduced by MinGW's
# (otherwise nice) feature of normalizing command-line parameters and
# environment variables that it passes into native applications.
#
# The things to remember about this script are as follows:
#
#   1) This script only works with the PATH environment variable.  If
#      you want to modify a different environment variable, you'll
#      need to customize this script.
#
#   2) Any literal path needs to be passed into pathmgr through an XML
#      configuration file.
#

##
# Print an error and exit if the executable <code>$1</code> cannot be
# found.
#
# @param[in] $1 name of executable
verify_exec () {
    type "$1" 1>/dev/null 2>&1
    if [ $? -ne 0 ] ; then
        echo "***" 1>&2
        echo "*** $progname: Error: Unable to find \"$1\" in your path." 1>&2
        echo "***" 1>&2
        exit 1
    fi
}

#
# Script starts here.
#

# Set up the program name for error reporting.
progname="pathmgr"
verify_exec "basename"
progname=`basename "$0"`

# Make sure the other executables are available.
verify_exec "dirname"
verify_exec "printf"

PYTHON_EXEC="python"
for i in python3 python python2 python2.7 \
         python3.17 python3.16 python3.15 python3.14 \
         python3.13 python3.12 python3.11 python3.10 \
         python3.9 python3.8 python3.7 python3.6
do
    if type "$i" 1>/dev/null 2>&1 ; then
        PYTHON_EXEC="$i"
        break
    fi
done
verify_exec "$PYTHON_EXEC"

# Generate the path to the pathmgr python script.
top=`dirname "$0"`
script="$top/pathmgr.py"

# Execute the script.
printf "$PATH" | exec python "$script" --origin=- --sep=: --uds=off ${1+"$@"}
