#! /usr/bin/env sh

# run this test with either:
# - mise run e2e nushell
# - LD_LIBRARY_PATH=/usr/lib:/home/linuxbrew/.linuxbrew/lib mise run e2e nushell

set -eu

if ! command -v nu >/dev/null 2>&1; then
	# for now, treat these tests as optional if `nu` is unavailable
	exit 0
fi

MISE_NUSHELL_INIT=$(mktemp e2e_nushell_init.XXXXXXXXXX)

# nushell's `source` is a parser keyword:
# https://www.nushell.sh/commands/docs/source.html
# this means the sourced mise init file must exist before we run `nu`

# use `env` to control the environment variables available to `nu`
# - we need LD_LIBRARY_PATH so that mise can find libbz2.so.1.0
# - we need PATH so that this test can find `mise`
env --ignore-environment LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" PATH="${PATH}" mise activate nu >"${MISE_NUSHELL_INIT}"

# equivalent of try/catch so we always cleanup the temporary file,
# even if the test fails
set +e

# use `env` to control the environment variables available to `nu`
# - we need LD_LIBRARY_PATH so that mise can find libbz2.so.1.0
# - we need PATH so that this test can find `nu`
env --ignore-environment LD_LIBRARY_PATH="${LD_LIBRARY_PATH}" PATH="${PATH}" nu --no-config-file --commands "use ${MISE_NUSHELL_INIT}"
NUSHELL_EXIT_CODE=${?}

set -e
rm --force --verbose "${MISE_NUSHELL_INIT}"

exit "${NUSHELL_EXIT_CODE}"
