#!/usr/bin/env sh

if [[ -n "$VITE_DOMAIN" ]]
then
    VITE_API_URL="$VITE_DOMAIN/api/v1/internal"
fi

if [[ -n "$VITE_API_KEY" ]]
then
    VITE_API_KEY="$VITE_API_KEY"
fi

if [[ -n "$VITE_AUTH_ENABLED" ]]
then
    VITE_AUTH_ENABLED="$VITE_AUTH_ENABLED"
fi

if [[ -n "$VITE_MOCK_SERVER" ]]
then
    VITE_MOCK_SERVER="$VITE_MOCK_SERVER"
fi

if [[ -n "$VITE_POLLING_INTERVAL" ]]
then
    VITE_POLLING_INTERVAL="$VITE_POLLING_INTERVAL"
fi

if [[ -n "$VITE_ASSIGNMENT_POLLING_INTERVAL" ]]
then
    VITE_ASSIGNMENT_POLLING_INTERVAL="$VITE_ASSIGNMENT_POLLING_INTERVAL"
fi

if [[ -n "$VITE_FETCH_SIGNED_URL" ]]
then
    VITE_FETCH_SIGNED_URL="$VITE_FETCH_SIGNED_URL"
fi

cat << EOF > /usr/share/nginx/html/config.js
globalThis.env = {
  VITE_API_URL: "$VITE_API_URL",
  VITE_API_KEY: "$VITE_API_KEY",
  VITE_AUTH_ENABLED: "$VITE_AUTH_ENABLED",
  VITE_MOCK_SERVER: "$VITE_MOCK_SERVER",
  VITE_POLLING_INTERVAL: "$VITE_POLLING_INTERVAL",
  VITE_ASSIGNMENT_POLLING_INTERVAL: "$VITE_ASSIGNMENT_POLLING_INTERVAL",
  VITE_FETCH_SIGNED_URL: "$VITE_FETCH_SIGNED_URL",
  VITE_ENVIRONMENT_NAME: "local",
  MODE: "production"
}
EOF

# Handle CMD command
exec "$@"
