#!/bin/bash

set -e

if [[ ! -e /etc/pacemaker/pingnodes.inc.sh ]]; then
	echo "No HA pingnodes configured, not waiting for networking."
	exit 0
fi

# shellcheck disable=SC1091
source /etc/pacemaker/pingnodes.inc.sh

if [[ "${#PINGNODES[@]}" -eq 0 ]]; then
	echo "No HA pingnodes configured, not waiting for networking."
	exit 0
fi

while true; do
	for NODE in "${PINGNODES[@]}"; do
		if ping -n -c 1 -q -w 2 -W 2 "$NODE" > /dev/null; then
			echo "Networking is up, continuing HA startup."
			exit 0
		fi
	done
	echo "Networking not yet up (no HA pingnodes are responding), sleeping and retrying..."
	sleep 1
done
