#!/bin/sh
set -u
[ -n "${BASH_VERSION:-}" ] || exec /bin/bash "$0" "$@"
conf=${UPOD_CTRL_CONF:-/etc/upod-ctrl/upod-ctrl.yaml}
addr=""
if [ -r "$conf" ]; then
  addr=$(awk '/^health:/{f=1;next} f&&/^[^[:space:]#]/{f=0} f&&/^[[:space:]]+address:/{sub(/.*address:[[:space:]]*/,"");print;exit}' "$conf" 2>/dev/null)
fi
addr=${addr%%#*}
addr=${addr//[\"\'[:space:]]/}
[ -n "$addr" ] || addr=":8081"
host=${addr%:*}
port=${addr##*:}
host=${host#[}
host=${host%]}
case $host in ""|0.0.0.0|::|"*") host=127.0.0.1 ;; esac
case $port in ""|*[!0-9]*) exit 1 ;; esac
{ exec 3<>"/dev/tcp/$host/$port"; } 2>/dev/null || exit 1
printf "GET /readyz HTTP/1.0\r\nHost: %s\r\n\r\n" "$host" >&3
read -r -t 5 _proto code _rest <&3 || exit 1
exec 3<&- 3>&-
[ "$code" = 200 ]
