#!/usr/bin/env bash

cat <<EOF >mise.toml
[env]
mise.file = ['.test-env']
EOF
echo FOO_FROM_FILE=foo_from_file >.test-env
echo TEST_ENV2=foo >.test-env2
assert_contains "mise env 2>&1" "deprecated [config.env.mise]"
assert "mise x -- env | grep FOO_FROM_FILE" "FOO_FROM_FILE=foo_from_file"
assert "MISE_ENV_FILE=.test-env2 mise x -- env | grep TEST_ENV2" "TEST_ENV2=foo"

cat <<EOF >mise.toml
[env]
_.file = 'not_present'
EOF
assert "mise env" # does not error

cat <<EOF >mise.toml
[env]
_.file = 'a'
EOF
echo 'export A=1' >a
assert "mise env | grep -v PATH" "export A=1"

cat <<EOF >mise.toml
[env]
_.file = { value = 'b.json' }
EOF
echo '{"B": 2}' >b.json
assert_contains "mise env 2>&1" "deprecated [config.directive.value]"
assert "mise env | grep -v PATH" "export B=2"

cat <<EOF >mise.toml
[env]
_.file = { values = ['a', 'b.json'] }
EOF
assert_contains "mise env 2>&1" "deprecated [config.directive.values]"
assert "mise env | grep -v PATH" "export A=1
export B=2"

cat <<EOF >mise.toml
[env]
_.file = 'c.toml'
EOF
cat <<EOF >c.toml
C = "see"
D = 2
E = true
EOF
assert "mise env | grep -v PATH" "export C=see
export D=2
export E=true"

cat <<EOF >mise.toml
[env]
_.file = 'nested.toml'
EOF
cat <<EOF >nested.toml
[DATABASE]
HOST = "localhost"
EOF
assert_fail "mise env" "unsupported toml value"

cat <<EOF >mise.toml
[env]
_.file = ['a', 'b.json']
EOF
echo 'export A=1' >a
echo '{"B": 2}' >b.json
assert "mise env | grep -v PATH" "export A=1
export B=2"

# Structured env files are data, so shell-like values must remain literal even
# when matching variables exist in the process environment.
cat <<EOF >mise.toml
[env]
_.file = 'password-hash.json'
EOF
# shellcheck disable=SC2016
echo '{"FOO": "$6$salt$hash"}' >password-hash.json
assert_contains "salt=changed hash=changed mise env -s bash 2>&1" "export FOO='\$6\$salt\$hash'"
assert_not_contains "mise env -s bash 2>&1" "env var 'salt' is not defined"

cat <<EOF >mise.toml
[env]
_.file = ['a', {path = 'b.json', tools = true}]
EOF
echo 'export A=1' >a
echo '{"B": 2}' >b.json
assert "mise env | grep -v PATH" "export A=1
export B=2"

cat <<EOF >mise.toml
[[env]]
_.file = 'a'
[[env]]
_.file = [{ path = ['b.json'], tools = true }]
EOF
echo 'export A=1' >a
echo '{"B": 2}' >b.json
assert "mise env | grep -v PATH" "export A=1
export B=2"

cat <<EOF >mise.toml
[env]
mise.file = ['{{env.HOME}}/.home-test-env']
EOF
echo FOO_FROM_FILE=foo_from_file_home >~/.home-test-env
assert "mise x -- env | grep FOO_FROM_FILE" "FOO_FROM_FILE=foo_from_file_home"

# A byte-order mark is what Notepad, and PowerShell 5.1's `Out-File -Encoding utf8`, leave at the
# front of a file. It is invisible in an editor, and it used to make the whole file unparseable:
# every command that read this config failed, pointing at a character nobody can see.
cat <<TOML >mise.toml
[env]
_.file = 'bom-env'
TOML
printf '\xef\xbb\xbfBOM_FOO=bar\n' >bom-env
assert "mise env | grep '^export BOM_FOO='" "export BOM_FOO=bar"

# Same mark, structured file. serde_json rejects it; yaml and toml were measured to accept it and
# are left alone.
cat <<TOML >mise.toml
[env]
_.file = 'bom.json'
TOML
printf '\xef\xbb\xbf{"BOM_BAZ": "qux"}\n' >bom.json
assert "mise env | grep '^export BOM_BAZ='" "export BOM_BAZ=qux"

# The same shell one step further: Windows PowerShell 5.1's bare `>` and `Out-File` write UTF-16LE,
# not UTF-8 with a mark. `read_to_string` is UTF-8 only, so this file used to be opened, decoded
# unsuccessfully and dropped without a word -- the variable was simply absent. Not a Windows-only
# path: a `.env` written there and committed was skipped by every platform reading it.
cat <<TOML >mise.toml
[env]
_.file = 'utf16-env'
TOML
printf '\xff\xfeU\x00T\x00F\x001\x006\x00_\x00F\x00O\x00O\x00=\x00b\x00a\x00r\x00\n\x00' >utf16-env
assert "mise env | grep '^export UTF16_FOO='" "export UTF16_FOO=bar"

# The control: byte-for-byte the same text as UTF-8, same expectation. The claim is that the
# encoding makes no difference, not that this one file happens to work.
printf 'UTF16_FOO=bar\n' >utf16-env
assert "mise env | grep '^export UTF16_FOO='" "export UTF16_FOO=bar"
