#!/usr/bin/with-contenv bashio

START_TIME="$(date +%s)"

echo "Waiting for first healthcheck execution (max $FIRST_HEALTHCHECK_TIMEOUT seconds)..."

while [ ! -f "/tmp/healthcheck-signal" ];
do
  sleep 1

  ELAPSED_TIME="$(expr \date +%s\ - $START_TIME)"

  if [ $ELAPSED_TIME -gt $FIRST_HEALTHCHECK_TIMEOUT ]
  then
    echo "Signal from healthcheck not received in time, failing the execution..."
    exit 20
  fi
done

echo "Signal from healthcheck received"
exit 0