#!/bin/sh

BACKEND_HOST="${BACKEND_HOST:-http://localhost:5000}"
FRONTEND_HOST="${FRONTEND_HOST:-http://localhost:80}"
KC_CLIENT_ID="${KC_CLIENT_ID:-labdev}"
KC_HOST="${KC_HOST:-https://login.hangman-lab.top}"
KC_REALM="${KC_REALM:-Hangman-Lab}"
# Note: ${DEBUG:-false} (correct default syntax). The old ${DEBUG:false}
# produced an empty value when DEBUG was unset -> invalid config.json.
DEBUG="${DEBUG:-false}"
# DEBUG is emitted unquoted as a JSON boolean — guarantee it is exactly
# true/false so config.json can never be invalid JSON.
case "$DEBUG" in true|false) ;; *) DEBUG=false ;; esac
rm -f /usr/share/nginx/html/config.js



cat <<EOL > /usr/share/nginx/html/config.json
{
    "BACKEND_HOST": "${BACKEND_HOST}",
    "FRONTEND_HOST": "${FRONTEND_HOST}",
    "KC_CLIENT_ID": "${KC_CLIENT_ID}",
    "OIDC_CONFIG": {
        "authority": "${KC_HOST}/realms/${KC_REALM}",
        "client_id": "${KC_CLIENT_ID}",
        "redirect_uri": "${FRONTEND_HOST}/callback",
        "post_logout_redirect_uri": "${FRONTEND_HOST}",
        "response_type": "code",
        "scope": "openid profile email roles",
        "popup_redirect_uri": "${FRONTEND_HOST}/popup_callback",
        "silent_redirect_uri": "${FRONTEND_HOST}/silent_callback"
    },
    "DEBUG": ${DEBUG}
}
EOL

exec "$@"