#!/bin/sh
set -eu

APP_DOMAIN="${APP_DOMAIN:-}"
APP_WWW_DOMAIN="${APP_WWW_DOMAIN:-}"

PRIMARY_DOMAIN="${APP_DOMAIN:-localhost}"
SERVER_NAMES="_"

if [ -n "$APP_DOMAIN" ]; then
  SERVER_NAMES="$APP_DOMAIN"
  if [ -n "$APP_WWW_DOMAIN" ]; then
    SERVER_NAMES="$SERVER_NAMES $APP_WWW_DOMAIN"
  fi
fi

sed \
  -e "s|__SERVER_NAMES__|$SERVER_NAMES|g" \
  -e "s|__PRIMARY_DOMAIN__|$PRIMARY_DOMAIN|g" \
  /etc/nginx/default.conf.template > /etc/nginx/conf.d/default.conf
