#!/bin/bash
set -xe
: "${GRAPHQL_ENDPOINT?GRAPHQL_ENDPOINT is not provided}"
: "${GRAPHQL_WS_ENDPOINT?GRAPHQL_WS_ENDPOINT is not provided}"
: "${MS_AUTHENTICATION_CLIENT_ID?MS_AUTHENTICATION_CLIENT_ID is not provided}"
: "${MS_AUTHENTICATION_AUTHORITY?MS_AUTHENTICATION_AUTHORITY is not provided}"
: "${MS_AUTHENTICATION_REDIRECT_URI?MS_AUTHENTICATION_REDIRECT_URI is not provided}"
: "${MS_AUTHENTICATION_POST_LOGOUT_REDIRECT_URI?MS_AUTHENTICATION_POST_LOGOUT_REDIRECT_URI is not provided}"
: "${OTLP_ENDPOINT?OTLP_ENDPOINT is not provided}"
: "${OTLP_SERVICE_NAME?OTLP_SERVICE_NAME is not provided}"
: "${COLLECTOR_SECRET?COLLECTOR_SECRET is not provided}"

env_vars=("GRAPHQL_ENDPOINT" "GRAPHQL_WS_ENDPOINT" "MS_AUTHENTICATION_CLIENT_ID" "MS_AUTHENTICATION_AUTHORITY" "MS_AUTHENTICATION_REDIRECT_URI" "MS_AUTHENTICATION_POST_LOGOUT_REDIRECT_URI" "OTLP_ENDPOINT" "OTLP_SERVICE_NAME" "COLLECTOR_SECRET")

ls /usr/share/nginx/html/

for var in "${env_vars[@]}"
do
  echo "$var ${!var}"
  sed -i "s~{{$var}}~${!var}~gi" /usr/share/nginx/html/main*.js
done

exec "$@"