#!/bin/bash

# Ensure we're in the Grafana root directory
cd "$(dirname "${BASH_SOURCE[0]}")/.." || exit 1

. scripts/grafana-server/variables

if [ -n "$STRICT_CSP" ]; then
    # Opt-in: override the e2e CSP template to drop 'unsafe-eval' (enforcing) so a blocked
    # new Function/eval throws and fails the test. $NONCE / $ROOT_PATH are escaped so bash
    # leaves them literal — Grafana substitutes them per request, not the shell.
    export GF_SECURITY_CONTENT_SECURITY_POLICY_TEMPLATE="require-trusted-types-for 'script'; script-src 'self' 'unsafe-inline' 'strict-dynamic' \$NONCE;object-src 'none';font-src 'self';style-src 'self' 'unsafe-inline' blob:;img-src * data:;base-uri 'self';connect-src 'self' grafana.com ws://\$ROOT_PATH wss://\$ROOT_PATH;manifest-src 'self';media-src 'none';form-action 'self';"
fi

LICENSE_PATH=""

if [ "$1" = "enterprise" ]; then
    if [ "$2" != "dev" ] && [ "$2" != "debug" ]; then
      LICENSE_PATH=$2/license.jwt
    else
      LICENSE_PATH=$3/license.jwt
    fi
fi

if [ "$BASE_URL" != "" ]; then
    echo -e "BASE_URL set, skipping starting server"
else
  ./scripts/grafana-server/start-server $LICENSE_PATH > scripts/grafana-server/server.log
fi

