#!/usr/bin/env sh

set -euo pipefail

if [ -z "${NGINX_PROXY_PASS_URL:-}" ]; then
    echo "NGINX_PROXY_PASS_URL not defined."
    exit 1
fi

if [ -z "${NGINX_RESOLVER:-}" ]; then
  export NGINX_RESOLVER=$(grep ^nameserver /etc/resolv.conf | awk '{print $2}' | xargs)
  echo "No resolvers specified via NGINX_RESOLVER. Got '${NGINX_RESOLVER}' from /etc/resolv.conf"
fi

export NGINX_ERROR_LOG_LEVEL="${NGINX_ERROR_LOG_LEVEL:-error}"

envsubst '${NGINX_PROXY_PASS_URL} ${NGINX_ERROR_LOG_LEVEL} ${NGINX_RESOLVER}' < /tmp/nginx.conf.tpl > /etc/nginx/nginx.conf

exec nginx -g 'daemon off;'
