#!/bin/sh
# Generate nginx config based on ENABLE_WRAPPER env var

TEMPLATE="/etc/nginx/templates/docker-local.conf.template"
OUTPUT="/etc/nginx/conf.d/default.conf"

# Start with the template
cp "$TEMPLATE" "$OUTPUT"

# If ENABLE_WRAPPER is not true, remove wrapper injection
if [ "$ENABLE_WRAPPER" != "true" ]; then
    echo "Wrapper disabled - removing injection lines"
    sed -i '/wrapper/d' "$OUTPUT"
    sed -i '/sub_filter/d' "$OUTPUT"
    sed -i '/Accept-Encoding/d' "$OUTPUT"
fi

# Replace env vars
envsubst '${DEPLOYMENT_NAME} ${ROOM_NAME} ${MANAGED_DOMAIN} ${SOLEPRINT_DOMAIN}' < "$OUTPUT" > /tmp/nginx.conf
mv /tmp/nginx.conf "$OUTPUT"

echo "Nginx config generated (ENABLE_WRAPPER=$ENABLE_WRAPPER)"
cat "$OUTPUT"
