#!/command/with-contenv bashio
# shellcheck shell=bash
# ==============================================================================
# Community Hass.io Add-ons: ESPHome
# Runs the NGINX proxy
# ==============================================================================

# The new device builder handles HA ingress itself, so nginx is bypassed.
# Block the longrun so s6 keeps the dependency satisfied, but exit 0 on
# SIGTERM instead of being signal-killed; a 256/15 exit makes nginx/finish
# stamp the container exit 143, which trips the Supervisor's SIGTERM check.
if bashio::config.true 'use_new_device_builder'; then
    bashio::log.info "NGINX bypassed: new device builder serves ingress directly."
    trap 'exit 0' TERM
    sleep infinity &
    wait
    exit 0
fi

bashio::log.info "Waiting for ESPHome dashboard to come up..."

while [[ ! -S /var/run/esphome.sock ]]; do
  sleep 0.5
done

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