#!/bin/sh
set -e

CONFIG_FILE="/usr/share/nginx/html/config/gbconfig.json"
mkdir -p "$(dirname "$CONFIG_FILE")"

# ── Option 1: volume-mounted file wins ───────────────────────────────────────
if [ -f "/run/gbconfig/gbconfig.json" ]; then
  echo "[GB] Using volume-mounted gbconfig.json"
  cp /run/gbconfig/gbconfig.json "$CONFIG_FILE"

# ── Option 2: generate from environment variables ────────────────────────────
else
  echo "[GB] Generating gbconfig.json from environment variables"

  cat > "$CONFIG_FILE" << GBEOF
{
  "livelog": {
    "getcontentpath": "${LIVELOG_GETCONTENTPATH:-}",
    "drive": "${LIVELOG_DRIVE:-}",
    "filepath": "${LIVELOG_FILEPATH:-}",
    "path": "${LIVELOG_PATH:-}",
    "pocheck": "${LIVELOG_POCHECK:-}"
  },
  "serverDetails": {
    "gb4versionidentifier": "${GB4_VERSION_IDENTIFIER:-}",
    "gb5versionidentifier": "${GB5_VERSION_IDENTIFIER:-}",
    "gb5getusersessionSSO": "${GB5_SSO_URL:-}",
    "gb4getusersessionSSO": "${GB4_SSO_URL:-null}",
    "proxylocation": "${PROXY_LOCATION:-}",
    "gb5enabled": "${GB5_ENABLED:-Y}",
    "gbdatabasecount": "${GB_DB_COUNT:-0}",
    "gbdatabasename": "${GB_DB_NAME:-none}",
    "gbmodulecryptokey": "${GB_CRYPTO_KEY:-}",
    "GBSSO": "${GB_SSO:-NONE}",
    "gbssohost": "${GB_SSO_HOST:-}",
    "gbssoclientid": "${GB_SSO_CLIENT_ID:-GB5WEB}",
    "gb5keycloakurl": "${GB5_KEYCLOAK_URL:-}",
    "gb5keycloakhost": "${GB5_KEYCLOAK_HOST:-}",
    "gbkeycloakprivatekey": "${GB_KEYCLOAK_PRIVATE_KEY:-}",
    "gb4versionproxylocation": "${GB4_VERSION_PROXY:-/versionapi}",
    "gb4proxylocation": "${GB4_PROXY_LOCATION:-/prox}",
    "gb5proxylocation": "${GB5_PROXY_LOCATION:-/gb5prox}",
    "dockerproxylocation": "${DOCKER_PROXY_LOCATION:-/docker}",
    "gbai": "${GB_AI_PROXY_LOCATION:-/goodbooksai}",
    "gbaicoanalyst": "${GB_AI_COANALYST_PROXY:-/gbaicoanalyst}",
    "gbaiconverter": "${GB_AI_CONVERTER_PROXY:-/gbaiconverter}",
    "gbaiproxylocation": "${GB_AI_PROXY_LOCATION:-/goodbooksai}",
    "gbaicoanalystproxylocation": "${GB_AI_COANALYST_PROXY:-/gbaicoanalyst}",
    "gbaiconverterproxylocation": "${GB_AI_CONVERTER_PROXY:-/gbaiconverter}"
  },
  "JasperDetails": {
    "Server": "${JASPER_SERVER:-}",
    "Port": "${JASPER_PORT:-8090}",
    "ContextName": "${JASPER_CONTEXT_NAME:-jasperserver/flow.html}",
    "Username": "${JASPER_USERNAME:-}",
    "Password": "${JASPER_PASSWORD:-}",
    "pdfview": "${JASPER_PDF_VIEW:-jasperserver/rest_v2/reports}"
  },
  "WikiDetails": {
    "Domain": "${WIKI_DOMAIN:-}"
  },
  "fayeDetails": {
    "URL": "${FAYE_URL:-}"
  },
  "productionDebug": "${PRODUCTION_DEBUG:-N}",
  "runMode": "${RUN_MODE:-docker}"
}
GBEOF
fi

# ── Generate nginx.conf from template (only substitutes GB backend vars) ──────
# Nginx's own $host, $uri, $remote_addr etc. are NOT in the explicit list
# so envsubst leaves them untouched.
# VERSIONAPI_BACKEND defaults to GB4_BACKEND when not set separately.
export VERSIONAPI_BACKEND="${VERSIONAPI_BACKEND:-$GB4_BACKEND}"

envsubst '${GB4_BACKEND} ${GB5_BACKEND} ${VERSIONAPI_BACKEND} ${AI_BACKEND} ${AI_COANALYST_BACKEND} ${AI_CONVERTER_BACKEND}' \
  < /etc/nginx/templates/default.conf.template \
  > /etc/nginx/conf.d/default.conf

exec nginx -g 'daemon off;'
