#!/usr/bin/with-contenv bashio
# shellcheck shell=bash
set -e
ingress_entry="$(bashio::app.ingress_entry)"
health_probe="http://127.0.0.1:20129${ingress_entry}/api/monitoring/health"

omniroute_ready=false
deadline=$((SECONDS + 900))
while [ "$SECONDS" -lt "$deadline" ]; do
    if curl -sf --connect-timeout 2 --max-time 5 -o /dev/null "$health_probe"; then
        omniroute_ready=true
        break
    fi
    sleep 2
done

if [ "$omniroute_ready" != true ]; then
    bashio::log.warning "OmniRoute did not answer within 15 minutes. Starting NGinx anyway: ingress will return 502 until it does."
fi

bashio::log.info "Starting NGinx..."
exec nginx
