#!/bin/sh
# ^ Note that we use sh, not bash, for alpine compatibility
set -e

# Find the path to this file:
case "$0" in
  */*)
    # If the invocation contained a path, "dirname" is reliable:
    SCRIPT_DIR=$(dirname -- "$0")
    ;;
  *)
    # If not, we search $PATH:
    self_path=$(command -v -- "$0")
    if [ -z "$self_path" ]; then
        echo "Fatal: Could not find script '$0' in PATH." >&2
        exit 1
    fi
    SCRIPT_DIR=$(dirname -- "$self_path")
    ;;
esac

# Exclude ourselves from PATH, find the real node, then reset PATH
PATH="$(printf '%s\n' "$PATH" | sed "s:${SCRIPT_DIR}\:::g")"
    # ^ This is made more complicated by sh, since we can't use variable expansion, but this
    # should be equivalent. We use : as a safe sed delim here, though it is confusing!

real_node=$(command -v -- node)
if [ -z "$real_node" ]; then
    echo "Fatal: Could not find the real 'node' executable in the modified PATH." >&2
    exit 1
fi

# Reset PATH back to include us again
PATH="$SCRIPT_DIR:$PATH"

# Define the path to our helper script using the reliable SCRIPT_DIR
PREPEND_PATH="$SCRIPT_DIR/../js/prepend-node.js"

case "$NODE_OPTIONS" in
  *"$HTTP_TOOLKIT_OVERRIDE_PATH"*)
    # If our config is already inside NODE_OPTIONS, we don't need to do anything:
    if command -v winpty >/dev/null 2>&1; then
        winpty "$real_node" "$@"
    else
        "$real_node" "$@"
    fi
    ;;
  *)
    # Otherwise, we need to inject --require $PREPEND into the node CLI:
    if command -v winpty >/dev/null 2>&1; then
        winpty "$real_node" --require "$PREPEND_PATH" "$@"
    else
        "$real_node" --require "$PREPEND_PATH" "$@"
    fi
    ;;
esac