#!/bin/bash

set -e

export LC_ALL=C

HACRM=$(ngcp-ha-crm)
if [[ "${HACRM}" != 'pacemaker' ]]; then
  # Skip if this is not the supported HA backend.
  exit 0
fi

# MT#17163
COUNTER=0
MAX=10
SLEEP_TIME=5

if crmadmin --help 2>&1 | grep -q 'integer number of seconds' ; then
  TIMEOUT=1
else
  TIMEOUT=1000
fi

echo -n "Waiting for stable HA status. Max tries [${MAX}]"
while [ ${COUNTER} -lt ${MAX} ]; do
  if crmadmin -D -t "${TIMEOUT}" >/dev/null 2>&1; then
    echo ". => DC running ($(ngcp-check-active -v))"
    exit 0
  else
    echo -n "."
  fi
  COUNTER=$((COUNTER+1))
  sleep ${SLEEP_TIME}
done
echo "=> max reached."
