#!/usr/bin/env bash

assert "mise generate bootstrap -w"
# shellcheck disable=SC2016
assert_contains "cat ./bin/mise" 'exec -a "$0" "$MISE_INSTALL_PATH" "$@"'
assert "./bin/mise version"

assert "mise tasks add xxx -- echo 'running xxx'"
assert "mise generate task-stubs --mise-bin ./bin/mise"
assert "./bin/xxx" "running xxx"

assert "mise generate bootstrap -l -w"
# shellcheck disable=SC2016
assert_contains "cat ./bin/mise" 'exec -a "$0" "$MISE_INSTALL_PATH" "$@"'

# ensure that the commands don't rely on the global mise bin
original_mise="$(which mise)"
mise_dir="$(dirname "$original_mise")"
OLD_PATH="$PATH"
PATH="$(echo "$PATH" | tr ':' '\n' | grep -v "^${mise_dir}$" | tr '\n' ':')"
PATH="${PATH%:}" # remove trailing colon

assert_contains "./bin/mise tasks ls" "xxx"

assert_not_contains "MISE_IGNORED_CONFIG_PATHS=$(pwd) ./bin/mise tasks ls" "xxx"

echo '
[tasks.other_task]
run = "echo running other_task"

[tasks.my_task]
run = ["{{mise_bin}} run other_task"]
' >mise.toml

assert_contains "./bin/mise run my_task" "running other_task"

PATH="$OLD_PATH"

# ./bin/mise is the localized script at this point, so generate a plain one too
assert "mise generate bootstrap -w ./bin/mise-plain"

# a stub at the resolved install path proves which path the script picked: it finds
# a binary there, skips the install, and execs it. Nothing is downloaded.
stub() {
  mkdir -p "$(dirname "$1")"
  echo '#!/usr/bin/env bash' >"$1"
  echo "echo \"$2 \$*\"" >>"$1"
  chmod +x "$1"
}

# a caller-provided MISE_INSTALL_PATH wins over the path baked into the script
stub "$PWD/custom/mise" custom-stub
assert "MISE_INSTALL_PATH=$PWD/custom/mise ./bin/mise-plain hello" "custom-stub hello"
assert "MISE_INSTALL_PATH=$PWD/custom/mise ./bin/mise hello" "custom-stub hello"

# MISE_VERSION keys the install path, otherwise changing it silently reuses the
# binary cached for the version the script was generated with. 9999.1.2 never exists.
stub "$HOME/.cache/mise/mise-9999.1.2" version-stub
assert "MISE_VERSION=v9999.1.2 ./bin/mise-plain hello" "version-stub hello"
assert "MISE_VERSION=9999.1.2 ./bin/mise-plain hello" "version-stub hello"

stub "$PWD/.mise/mise-9999.1.2" localized-version-stub
assert "MISE_VERSION=v9999.1.2 ./bin/mise hello" "localized-version-stub hello"
