#!/bin/sh
echo "Replacing environment variables in index.html..."

keys="MAX_AUDIENCE_SPLIT_COUNT"

# Initialize an empty string to hold the object in JavaScript format
env_object="{"

# Loop through the keys and build the object
for key in $keys; do
    eval value=\$$key
    echo "Processing key: $key with value: $value"  # Debug statement

    # Append each key-value pair to the env_object
    env_object="${env_object}\"${key}\": \"${value}\","
done

# Remove the last comma and close the object
env_object="${env_object%,}}"

echo "Final env_object: $env_object"  # Debug statement

# Inject the object into index.html inside a <script> tag
sed -i "s|__ENV_OBJECT__|$env_object|g" "$BASE_DIR/index.html"

# Start Nginx
exec nginx -g "daemon off;"
