#!/usr/bin/env bash

if [[ "$(uname -s)" != "Linux" ]]; then
  echo "Skipping Linux-specific test on non-Linux OS"
  exit 0
fi

cat <<'EOF' >mise.toml
[settings.erlang]
compile = false
EOF

if output="$(MISE_FRIENDLY_ERROR=1 RUST_BACKTRACE=0 ImageOS=nobara-43 mise install erlang@28.5 2>&1)"; then
  fail "expected erlang install to fail when precompiled binaries are unavailable"
fi

[[ $output == *"precompiled erlang is not available: unsupported OS version: nobara-43"* ]] ||
  fail "expected unsupported OS error, got: $output"

[[ $output != *"build-install"* ]] ||
  fail "erlang.compile=false should not fall back to kerl build-install: $output"

export MISE_LOCKFILE=1
detect_platform

cat <<'EOF' >mise.toml
[tools]
erlang = "28.5"

[settings.erlang]
compile = false
precompiled_os = "ubuntu-22.04"
EOF

mise lock --platform "$MISE_PLATFORM"
assert_contains "cat mise.lock" 'precompiled_os = "ubuntu-22.04"'
assert_matches "cat mise.lock" 'url = "https://builds\.hex\.pm/builds/otp/(amd64|arm64)/ubuntu-22\.04/OTP-28\.5\.tar\.gz"'

rm -f mise.lock
sed -i 's/ubuntu-22\.04/ubuntu-26.04/' mise.toml
mise lock --platform "$MISE_PLATFORM"
assert_contains "cat mise.lock" 'precompiled_os = "ubuntu-26.04"'
assert_matches "cat mise.lock" 'url = "https://builds\.hex\.pm/builds/otp/(amd64|arm64)/ubuntu-26\.04/OTP-28\.5\.tar\.gz"'
