#!/usr/bin/with-contenv bash
# shellcheck shell=bash

if netstat -apn | grep -q ":53 " && [[ -z "${USE_COREDNS}" ]]; then
    USE_COREDNS="false"
fi

if [[ ${USE_COREDNS} == "false" ]]; then
    s6-notifyoncheck -d -n 300 -w 1000 -c "echo **** Disabling CoreDNS ****" \
        sleep infinity
else
    if grep -q "health" /config/coredns/Corefile; then
        exec \
            s6-notifyoncheck -d -n 300 -w 1000 -c "redirfd -w 1 /dev/null curl -s http://localhost:8080/health" \
            cd /config/coredns \
            /usr/bin/coredns -dns.port=53
    else
        exec \
            s6-notifyoncheck -d -n 300 -w 1000 -c "nc -zu localhost 53" \
            cd /config/coredns \
            /usr/bin/coredns -dns.port=53
    fi
fi
