#!/usr/bin/env bash
set -euo pipefail

rust_bin="${DETMIR_AUTO_RUST_BIN:-/usr/local/bin/detmir-auto-rust}"
legacy_bin="${DETMIR_AUTO_LEGACY_BIN:-/usr/local/bin/detmir-auto.legacy-shell}"
env_file="${DETMIR_AUTO_ENV_FILE:-/etc/detmir/detmir-check.env}"

if [[ -r "$env_file" ]]; then
  set -a
  # shellcheck disable=SC1090
  source "$env_file"
  set +a
fi

if [[ -x "$rust_bin" ]]; then
  exec "$rust_bin" "$@"
fi

if [[ -x "$legacy_bin" ]]; then
  exec "$legacy_bin" "$@"
fi

echo "detmir-auto: neither $rust_bin nor $legacy_bin is executable" >&2
exit 127
