#!/bin/bash

# @PREAMBLE_START@

# This section is copied from preamble.sh (automatically, during build) and
# should not be edited in place.

# turn on echo for debuging.
if [ ! -z "${VERT_DEBUG_PREAMBLE:-}" ]; then set -x; else set +x; fi
: '# Preamble begin'
: '# # Standardizing shell'
unset IFS # default word splitting on whitespace
set +o history # disable history. Should be default, but some reports otherwise
set -u # disallow unset variables

# note: these two vars are reset by vars.bash, but necessary to get there.
VERT_D=${VERT_VERT_D:-'/opt/vertica'}
BINLIB_D=${VERT_BINLIB_D:-"$VERT_D/share/binlib"}
source "$BINLIB_D/include/vars.bash"
source "$BINLIB_D/include/common_func.bash"
source "$BINLIB_D/include/self_vars.bash"

: '# Preamble end'
set +x # turn echo off
# @PREAMBLE_END@

function usage() {
	v_here_echo << HEREDOC
$SELF_SHORTNAME: Look for a binary by any of the names provided.

Usage: ./$SELF_SHORTNAME [-f] NAME [NAME ...]

-f: If no match is found, try again using a built-in PATH value which includes
    all of the common linux bin directories.

Names starting with a leading slash are absolute while others will be found on
\$PATH.  Relative locations are unsupported at this time.

Prints the first instance of the binary found (like the 'which' command).  The
return code indicates whether or not the binary was found.  Matches are ignored
if they are not executable by the current user.
HEREDOC
}

USE_FULL_PATH=""
while getopts :hf OPTVAL; do
    case "$OPTVAL" in
        f )
            USE_FULL_PATH="true"
            ;;
        h )
            usage
            exit 0
            ;;
        \? )
            echo "Invalid option: -$OPTARG" >&2
            exit 1
            ;;
    esac
done

shift $(( $OPTIND - 1 ))

if [ "$#" = "0" ]; then
    echo "No command name (NAME) provided." >&2
    exit 1
fi

# Given a $PATH value in the first parameter, searches the PATH for any command
# by the name of any of the remaining parameters.  If one is found, prints the
# full path to the command and exits the script.
#
# Searches the entire PATH for the first command name before moving on to the
# next command name.
function do_find() {
    SEARCHPATH="$1"
    shift

    for BINNAME in "$@"; do
        if [ -z "$BINNAME" ]; then
            echo "Empty name provided. Ignoring" >&2
            continue
        fi

        if [ "${BINNAME:0:1}" = "/" ]; then
            # absolute
            if [ -x "$BINNAME" ]; then
                echo "$BINNAME"
                exit 0
            fi
        else
            while IFS= read -r -d ':' PATHPART; do
                if [ -z "$PATHPART" ]; then
                    continue
                fi
                if [ -x "$PATHPART/$BINNAME" ]; then
                    echo "$PATHPART/$BINNAME"
                    exit 0
                fi
            done <<< "$SEARCHPATH:"
        fi
    done
}

# exits if successful
do_find "$PATH" "$@"

if [ -n "$USE_FULL_PATH" ]; then
    # A one-off environment variable for testing.
    if [ -n "${VERT_STANDARD_LINUX_PATH:-}" ]; then
        FULL_PATH="$VERT_STANDARD_LINUX_PATH"
    else
        FULL_PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
    fi
    # exits if successful
    do_find "$FULL_PATH" "$@"
fi

exit 1
