#!/bin/sh
set -e

mkdir -p /etc/nginx/templates

if [ "$NGINX_SSL_ENABLED" = "true" ]; then
    echo "Using SSL configuration"
    cp /templates/ssl.conf.template /templates/default.conf.template
else
    echo "Using non-SSL configuration"
    cp /templates/nossl.conf.template /templates/default.conf.template
fi

if [ "$ENVIRONMENT" = "production" ]; then
  export UI_ROOT="/usr/share/nginx/html"
  envsubst '${VITE_API_URL}' < /usr/share/nginx/html/config.template.js > /usr/share/nginx/html/config.js
  echo "Setting production root to ${UI_ROOT}"
else
  export UI_ROOT="/home"
  echo "Setting development root to ${UI_ROOT}"
fi

envsubst '$UI_ROOT' < /templates/default.conf.template > /etc/nginx/templates/default.conf.template
