#!/usr/bin/env bash
# Source smom-dbis-138 dotenv with RPC CR/LF trimming (load_deployment_env).
# Uses repo-root .env unless ENV_FILE is set. Prefer this over raw `source .env` in new scripts.
# Usage: source scripts/load-env.sh   (from smom-dbis-138)
#        ENV_FILE=/path/to/.env source scripts/load-env.sh
# Do not execute; source so variables apply to the current shell.

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
export ENV_FILE="${ENV_FILE:-${SCRIPT_DIR}/../.env}"
if [[ -f "$SCRIPT_DIR/lib/deployment/dotenv.sh" ]]; then
  # shellcheck disable=SC1090
  source "$SCRIPT_DIR/lib/deployment/dotenv.sh"
  load_deployment_env --repo-root "$(cd "$SCRIPT_DIR/.." && pwd)"
  echo "Loaded via load_deployment_env: ${ENV_FILE:-${SCRIPT_DIR}/../.env}"
else
  if [[ -f "$ENV_FILE" ]]; then
    set -a
    # shellcheck disable=SC1090
    source "$ENV_FILE"
    set +a
    echo "Loaded: $ENV_FILE (dotenv.sh missing — no RPC trim)"
  else
    echo "WARN: $ENV_FILE not found" >&2
  fi
fi
