#!/bin/sh

# Check if SENTRY_DSN is set
if [ -z "$SENTRY_DSN" ]; then
    echo "Error: SENTRY_DSN environment variable is not set."
    exit 1
fi

# Check if the placeholder exists without logging the files being searched
if grep -r "__SENTRY_DSN__" ./.next/static/ > /dev/null 2>&1; then
    echo "Found __SENTRY_DSN__ placeholder, proceeding with replacement."
    
    # Perform the replacement silently (suppress output)
    find ./.next/static/ -type f -exec sed -i "s|__SENTRY_DSN__|${SENTRY_DSN}|g" {} + > /dev/null 2>&1 || { echo "Error replacing __SENTRY_DSN__."; exit 1; }

    echo "SENTRY_DSN injected successfully."
else
    echo "Error: __SENTRY_DSN__ placeholder not found in ./.next/static."
    exit 1
fi

# Proceed to the CMD command (start the app)
exec "$@"
