#!/bin/sh
set -euf

ALLOY_BIN="${ALLOY_BIN:-/usr/bin/alloy}"

otel_mode=""
case "$(printf '%s' "${ALLOY_OTEL_MODE:-}" | tr '[:upper:]' '[:lower:]')" in
  # check against truthy values
  1 | true | yes | on) otel_mode="1" ;;
esac

# OTEL_CUSTOM_ARGS is intentionally unquoted so a file with multiple arguments
# word-splits into separate argv entries.
if [ -n "$otel_mode" ]; then
  echo "Alloy starting in OTel Engine mode" >&2
  OTEL_CONFIG_FILE="${OTEL_CONFIG_FILE:-/etc/alloy/config.yaml}"
  # shellcheck disable=SC2086
  exec "$ALLOY_BIN" otel --config="$OTEL_CONFIG_FILE" ${OTEL_CUSTOM_ARGS:-}
else
  echo "Alloy starting in Default Engine mode" >&2
  # If operator sets CONFIG_FILE then use this, else use the default /etc/alloy/config.alloy file
  CONFIG_FILE="${CONFIG_FILE:-/etc/alloy/config.alloy}"
  # shellcheck disable=SC2086
  exec "$ALLOY_BIN" run ${CUSTOM_ARGS:-} --storage.path=/var/lib/alloy/data "$CONFIG_FILE"
fi
