#!/bin/sh
set -eu

config_path="${PLIMI_CONFIG_PATH:-/usr/share/nginx/html/config.js}"

json_string() {
  value="${1:-}"
  escaped="$(printf '%s' "$value" | sed \
    -e 's/\\/\\\\/g' \
    -e 's/"/\\"/g' \
    -e 's/\r/\\r/g')"
  printf '"%s"' "$escaped"
}

write_entry() {
  key="$1"
  default_value="$2"
  eval "value=\${$key:-\$default_value}"
  printf '  %s: ' "$key" >> "$config_path"
  json_string "$value" >> "$config_path"
  printf ',\n' >> "$config_path"
}

printf 'window.__PLIMI_CONFIG__ = {\n' > "$config_path"
write_entry VITE_PLIMI_REPO_URL "https://github.com/your-org/plimi"
write_entry VITE_SITE_URL ""
write_entry VITE_LEGAL_NAME "Achraf ACHKARI-BEGDOURI"
write_entry VITE_LEGAL_COUNTRY "France"
write_entry VITE_PRIVACY_EMAIL "privacy@example.com"
write_entry VITE_PRIVACY_EFFECTIVE_DATE "2026-06-07"
write_entry VITE_ANALYTICS_PROVIDER_NAME "Umami"
write_entry VITE_ANALYTICS_SCRIPT_URL "https://u.achraf.app/script.js"
write_entry VITE_ANALYTICS_HOST_URL "https://u.achraf.app"
write_entry VITE_ANALYTICS_WEBSITE_ID "89d0f8d2-5b59-438c-b1f6-ab2655e97f6e"
write_entry VITE_ANALYTICS_HOST "u.achraf.app"
write_entry VITE_ANALYTICS_HOST_COUNTRY "France"
write_entry VITE_ANALYTICS_RETENTION_MONTHS "13"
printf '};\n' >> "$config_path"
