#!/bin/sh

ADMIN_CONTACT_NAME="${ADMIN_CONTACT_NAME:-}"
ADMIN_CONTACT_URL="${ADMIN_CONTACT_URL:-}"
API_URL="${API_URL:-}"
APP_NAME="${APP_NAME:-}"
IS_DOCUMINE="${IS_DOCUMINE:-false}"
RULE_EDITOR_DEV_ONLY="${RULE_EDITOR_DEV_ONLY:-true}"
AUTO_READ_TIME="${AUTO_READ_TIME:-1.5}"
BACKEND_APP_VERSION="${BACKEND_APP_VERSION:-4.7.0}"
EULA_URL="${EULA_URL:-}"
FRONTEND_APP_VERSION="${FRONTEND_APP_VERSION:-}"

MAX_FILE_SIZE_MB="${MAX_FILE_SIZE_MB:-50}"
MAX_RETRIES_ON_SERVER_ERROR="${MAX_RETRIES_ON_SERVER_ERROR:-3}"
OAUTH_CLIENT_ID="${OAUTH_CLIENT_ID:-gin-client}"
OAUTH_IDP_HINT="${OAUTH_IDP_HINT:-}"
OAUTH_URL="${OAUTH_URL:-/auth}"
RECENT_PERIOD_IN_HOURS="${RECENT_PERIOD_IN_HOURS:-24}"
SELECTION_MODE="${SELECTION_MODE:-structural}"
MANUAL_BASE_URL="${MANUAL_BASE_URL:-https://docs.redactmanager.com/preview}"
ANNOTATIONS_THRESHOLD="${ANNOTATIONS_THRESHOLD:-1000}"
AVAILABLE_NOTIFICATIONS_DAYS="${AVAILABLE_NOTIFICATIONS_DAYS:-30}"
AVAILABLE_OLD_NOTIFICATIONS_MINUTES="${AVAILABLE_OLD_NOTIFICATIONS_MINUTES:-60}"
NOTIFICATIONS_THRESHOLD="${NOTIFICATIONS_THRESHOLD:-1000}"
BASE_TRANSLATIONS_DIRECTORY="${BASE_TRANSLATIONS_DIRECTORY:-/assets/i18n/redact/}"
THEME="${THEME:-theme-template}"
WATERMARK_PREVIEW_PAPER_FORMAT="${WATERMARK_PREVIEW_PAPER_FORMAT:-a4}"

echo '{
          "ADMIN_CONTACT_NAME":"'"$ADMIN_CONTACT_NAME"'",
          "ADMIN_CONTACT_URL":"'"$ADMIN_CONTACT_URL"'",
          "API_URL":"'"$API_URL"'",
          "APP_NAME":"'"$APP_NAME"'",
          "IS_DOCUMINE":"'"$IS_DOCUMINE"'",
          "RULE_EDITOR_DEV_ONLY":"'"$RULE_EDITOR_DEV_ONLY"'",
          "AUTO_READ_TIME":"'"$AUTO_READ_TIME"'",
          "BACKEND_APP_VERSION":"'"$BACKEND_APP_VERSION"'",
          "EULA_URL":"'"$EULA_URL:"'",
          "FRONTEND_APP_VERSION":"'"$FRONTEND_APP_VERSION:"'",
          "MAX_FILE_SIZE_MB":"'"$MAX_FILE_SIZE_MB"'",
          "MAX_RETRIES_ON_SERVER_ERROR":"'"$MAX_RETRIES_ON_SERVER_ERROR"'",
          "OAUTH_CLIENT_ID":"'"$OAUTH_CLIENT_ID"'",
          "OAUTH_IDP_HINT":"'"$OAUTH_IDP_HINT"'",
          "OAUTH_URL":"'"$OAUTH_URL"'",
          "RECENT_PERIOD_IN_HOURS":"'"$RECENT_PERIOD_IN_HOURS"'",
          "SELECTION_MODE":"'"$SELECTION_MODE"'",
          "MANUAL_BASE_URL":"'"$MANUAL_BASE_URL"'",
          "BASE_TRANSLATIONS_DIRECTORY":"'"$BASE_TRANSLATIONS_DIRECTORY"'",
          "THEME":"'"$THEME"'",
          "ANNOTATIONS_THRESHOLD":"'"$ANNOTATIONS_THRESHOLD"'",
          "AVAILABLE_NOTIFICATIONS_DAYS":"'"$AVAILABLE_NOTIFICATIONS_DAYS"'",
          "AVAILABLE_OLD_NOTIFICATIONS_MINUTES":"'"$AVAILABLE_OLD_NOTIFICATIONS_MINUTES"'",
          "NOTIFICATIONS_THRESHOLD":"'"$NOTIFICATIONS_THRESHOLD"'",
          "WATERMARK_PREVIEW_PAPER_FORMAT":"'"$WATERMARK_PREVIEW_PAPER_FORMAT"'"
       }' >/usr/share/nginx/html/ui/assets/config/config.json

echo 'Env variables: '
cat /usr/share/nginx/html/ui/assets/config/config.json

echo 'Running with config: '
cat /etc/nginx/nginx.conf

nginx -g 'daemon off;'
