#!/bin/sh

if [ -z "$VITE_PUBLIC_API_URL" ] || [ -z "$VITE_PUBLIC_BASE_URL" ] || [ -z "$VITE_PUBLIC_MAPBOX_ACCESS_TOKEN" ] || [ -z "$VITE_PUBLIC_ENABLE_ANALYTICS" ] || [ -z "$VITE_PUBLIC_KEYCLOAK_URL" ] || [ -z "$VITE_PUBLIC_KEYCLOAK_CLIENT_ID" ] || [ -z "$VITE_PUBLIC_KEYCLOAK_REALM" ] || [ -z "$VITE_PUBLIC_CHECK_LOGIN_IFRAME" ]; then
  echo "Error: VITE_PUBLIC_API_URL, VITE_PUBLIC_BASE_URL, VITE_PUBLIC_MAPBOX_ACCESS_TOKEN, VITE_PUBLIC_ENABLE_ANALYTICS, VITE_PUBLIC_KEYCLOAK_URL, VITE_PUBLIC_KEYCLOAK_CLIENT_ID, VITE_PUBLIC_KEYCLOAK_REALM, VITE_PUBLIC_CHECK_LOGIN_IFRAME is not defined" >&2
  exit 1
fi

find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_API_URL#$VITE_PUBLIC_API_URL#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_BASE_URL#$VITE_PUBLIC_BASE_URL#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_MAPBOX_ACCESS_TOKEN#$VITE_PUBLIC_MAPBOX_ACCESS_TOKEN#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_ENABLE_ANALYTICS#$VITE_PUBLIC_ENABLE_ANALYTICS#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_ANALYTICS_SCRIPT_URL#$VITE_PUBLIC_ANALYTICS_SCRIPT_URL#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_KEYCLOAK_URL#$VITE_PUBLIC_KEYCLOAK_URL#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_KEYCLOAK_CLIENT_ID#$VITE_PUBLIC_KEYCLOAK_CLIENT_ID#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_KEYCLOAK_REALM#$VITE_PUBLIC_KEYCLOAK_REALM#g"
find /app/frontend/dist \( -type d -name .git -prune \) -o -type f -print0 | xargs -0 sed -i "s#PLACEHOLDER_VITE_PUBLIC_CHECK_LOGIN_IFRAME#$VITE_PUBLIC_CHECK_LOGIN_IFRAME#g"

exec "$@"
