#!/bin/sh
set -e

echo "🚀 Initializing Nginx with SSL_MODE=${SSL_MODE} and DNS=${DNS}"

# Substitute variables into nginx.conf
envsubst '${SSL_MODE} ${DNS}' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf

# Validate cert files exist
CRT_PATH="/etc/nginx/certs/${SSL_MODE}.crt"
KEY_PATH="/etc/nginx/certs/${SSL_MODE}.key"

if [ ! -f "$CRT_PATH" ] || [ ! -f "$KEY_PATH" ]; then
  echo "⚠️ Missing certificate files for mode: ${SSL_MODE}"
  echo "Expected: $CRT_PATH and $KEY_PATH"
fi

echo "✅ Starting Nginx..."
exec nginx -g "daemon off;"
