#!/bin/bash
set -euo pipefail

# Use the _first_ entry in `/etc/resolv.conf` as the resolver for Nginx. This
# guarantees that it will be able to resolve internal services (later entries
# may not).
export NAMESERVER=$( \
  cat /etc/resolv.conf \
  | grep "nameserver" \
  | awk '{print $2}' \
  | head -n1 \
  | tr '\n' ' ' \
)
echo "Nameserver is: $NAMESERVER"

echo "Copying nginx config"
envsubst '${NAMESERVER} ${CHUM_TO_JSON_SERVER} ${ARGUS_API_SERVER} ${ZRN_VIEWER_SERVER}' < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf

echo "Using nginx config:"
cat /etc/nginx/conf.d/default.conf

echo "Starting nginx"
nginx -g "daemon off;"
