#!/usr/bin/env sh
set -eu
# Use the id returned by POST /emails. No recipient or body is returned.
# accepted means provider handoff. Do not resend an ambiguous message blindly.
: "${MAILCORE_API_KEY:?Set the operator-issued server key}"
: "${MAILCORE_EMAIL_ID:?Set the returned eml_ message ID}"
case "$MAILCORE_EMAIL_ID" in
  eml_*) mailcore_id_hex="${MAILCORE_EMAIL_ID#eml_}" ;;
  *) printf '%s\n' 'MAILCORE_EMAIL_ID must be a returned eml_ message ID' >&2; exit 1 ;;
esac
case "$mailcore_id_hex" in
  *[!a-f0-9]*) printf '%s\n' 'Invalid message ID' >&2; exit 1 ;;
esac
if [ "${#mailcore_id_hex}" -ne 24 ]; then
  printf '%s\n' 'Message ID must contain eml_ followed by 24 lowercase hex digits' >&2
  exit 1
fi
curl --silent --show-error --fail-with-body \
  --connect-timeout 10 --max-time 30 \
  "https://mailcore.uz/api/email/v1/emails/${MAILCORE_EMAIL_ID}" \
  -H "Authorization: Bearer ${MAILCORE_API_KEY}"
