#!/usr/bin/env bash

fake_bin="$HOME/wrapper-bin"
mkdir -p "$fake_bin"
cat >"$fake_bin/fakeshow" <<'SH'
#!/usr/bin/env bash
printf 'home:%s args:%s\n' "$FOO_HOME" "$*"
SH
cat >"$fake_bin/foo" <<'SH'
#!/usr/bin/env bash
echo unwrapped
SH
chmod +x "$fake_bin/fakeshow" "$fake_bin/foo"
export PATH="$fake_bin:$PATH"

cat >mise.toml <<'TOML'
[vars]
mode = "fast"

[env]
PROJECT_DIR = "/opt/foo"

[wrappers.foo]
command = "fakeshow"
args = ["--mode={{ vars.mode }}", "{{ env.PROJECT_DIR }}"]
env = { FOO_HOME = "{{ [env.HOME, 'foo'] | join_path }}" }
TOML

mise reshim
eval "$(mise activate bash --shims)"
assert "foo run" "home:$HOME/foo args:--mode=fast /opt/foo run"

# `config_root` and relative paths come from the declaring file, wherever the shim runs
echo token >token.txt
echo fakeshow >command.txt
mkdir -p sub
cat >mise.toml <<'TOML'
[wrappers.foo]
command = "{{ read_file(path='command.txt') | trim }}"
args = ["{{ config_root }}", "{{ read_file(path='token.txt') | trim }}"]
TOML

mise reshim
assert "foo run" "home: args:$PWD token run"
assert "cd sub && foo run" "home: args:$PWD token run"
