#!/bin/sh
set -eu

js_escape() {
  printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}

write_app_config() {
  cat <<EOF >/usr/share/pweb/app-config.js
window.appConfig = {
  apiProto: "$(js_escape "${API_PROTOCOL:-https}")",
  apiHost: "$(js_escape "${SERVICE_HOST:-app.sendico.io}")",
  apiEndpoint: "$(js_escape "${API_ENDPOINT:-/api/v1}")",
  amplitudeSecret: "$(js_escape "${AMPLITUDE_SECRET:-}")",
  defaultLocale: "$(js_escape "${DEFAULT_LOCALE:-en}")",
  defaultCurrency: "$(js_escape "${DEFAULT_CURRENCY:-EUR}")",
  wsProto: "$(js_escape "${WS_PROTOCOL:-wss}")",
  wsEndpoint: "$(js_escape "${WS_ENDPOINT:-/ws}")"
};
EOF
}

echo "Starting container"
write_app_config

exec caddy run --config /etc/caddy/Caddyfile --adapter caddyfile
