#!/usr/bin/env sh
# Finds the absolute path of a system binary by checking places in most-likely order.
# Helpful for when overriding a binary with a script, meaning the script takes higher
# precedence in the path, but then the script needs to call the actual binary.

if [ -z "$1" ]; then
    # No program prvided
    exit 1
fi

# Split the custom path into an array based on ":" (path separator)
IFS=":" read -ra path_dirs <<< "$PATH"

# Iterate through the directories in PATH_2
for dir in "${path_dirs[@]}"; do
    if [ "$(echo "$dir" | cut -c 1)" = "~" ] || [ "$(echo "$dir" | cut -c 1-5)" = "/home" ]; then
        # We are looking in a user's home, skip.
        continue
    fi

    potential_path="$dir/$1"
    if [ -x "$potential_path" ]; then
        "$dir/$@"
        exit 0
    fi
done

echo "command not found: $1"
exit 2 # Not found

