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

# CI step: upload sourcemaps to Sentry
# Called from .gitea/workflows/deploy.yml
# Required env: SENTRY_AUTH_TOKEN (passed from workflow secrets)

# Read .env
set -o allexport
. ./.env
set +o allexport

# Skip gracefully if no token is configured
if [ -z "${SENTRY_AUTH_TOKEN:-}" ]; then
    echo "SENTRY_AUTH_TOKEN not set, skipping sourcemap upload"
    exit 0
fi

echo "SENTRY_URL=${SENTRY_URL:-}"
echo "SENTRY_ORG=${SENTRY_ORG:-}"
echo "SENTRY_PROJECT=${SENTRY_PROJECT:-}"
echo "PROJECT_RELEASE=${PROJECT_RELEASE:-}"

echo "Injecting debug IDs..."
npx sentry-cli sourcemaps --log-level info inject frontend/dist

echo "Creating release and uploading sourcemaps..."
npx sentry-cli sourcemaps --log-level info upload \
    --release="${PROJECT_RELEASE:-${SENTRY_PROJECT}.dirty}" \
    --url-prefix='~/dist' \
    --ext js --ext mjs --ext map \
    frontend/dist
