#!/usr/bin/env bash

export MISE_SHIMS_DIR="$HOME/.local/share/mise/shims"
export MISE_SYSTEM_SHIMS_DIR="$HOME/.local/share/mise/system-shims"
export PATH="$MISE_SHIMS_DIR:$PATH"
mkdir -p "$MISE_CONFIG_DIR" "$MISE_SHIMS_DIR"

assert_shims_present() {
  case ":$PATH:" in
    *":$MISE_SHIMS_DIR:"*) ;;
    *)
      echo "FAIL: expected user shims in PATH: $PATH"
      exit 1
      ;;
  esac
}

assert_shims_absent() {
  case ":$PATH:" in
    *":$MISE_SHIMS_DIR:"*)
      echo "FAIL: expected user shims to be absent from PATH: $PATH"
      exit 1
      ;;
    *) ;;
  esac
}

# Preserve pre-lazy behavior for users who disabled general auto-install.
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[settings]
not_found_auto_install = false
EOF
eval "$(mise activate bash --no-hook-env)"
eval "$(mise hook-env --force --shell bash)"
assert_shims_absent

# An explicit lazy declaration opts into retaining the fallback boundary.
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[settings]
not_found_auto_install = false

[tools]
dummy = { version = "1.0.0", lazy = true, lazy_bins = ["dummy"] }
EOF
eval "$(mise hook-env --force --shell bash)"
assert_shims_present

# Removing the lazy declaration removes the boundary from the active shell.
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[settings]
not_found_auto_install = false
EOF
eval "$(mise hook-env --force --shell bash)"
assert_shims_absent

# General not-found auto-install independently retains the existing behavior.
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[settings]
not_found_auto_install = true
EOF
eval "$(mise hook-env --force --shell bash)"
assert_shims_present

# The explicit opt-out overrides auto-install and lazy declarations, and removes both farms.
mkdir -p "$MISE_SYSTEM_SHIMS_DIR"
cat >"$MISE_CONFIG_DIR/config.toml" <<'EOF'
[settings]
activate_shims = false
not_found_auto_install = true

[tools]
dummy = { version = "1.0.0", lazy = true, lazy_bins = ["dummy"] }
EOF
export PATH="$MISE_SYSTEM_SHIMS_DIR:$PATH"
eval "$(mise activate bash --no-hook-env)"
assert_shims_absent
case ":$PATH:" in
  *":$MISE_SYSTEM_SHIMS_DIR:"*) exit 1 ;;
esac
eval "$(mise hook-env --force --shell bash)"
assert_shims_absent
case ":$PATH:" in
  *":$MISE_SYSTEM_SHIMS_DIR:"*) exit 1 ;;
esac

# Fish activation and its subsequent hook honor the same setting.
assert "fish -c 'mise activate fish | source; mise hook-env --force --shell fish | source; contains -- \"\$MISE_SHIMS_DIR\" \$PATH; echo \$status'" "1"

# Explicit shim-only activation still means what it says.
eval "$(mise activate bash --shims)"
assert_shims_present

# An environment override can re-enable normal activation without editing config.
export MISE_ACTIVATE_SHIMS=true
eval "$(mise hook-env --force --shell bash)"
assert_shims_present
unset MISE_ACTIVATE_SHIMS
