#!/usr/bin/openrc-run

# FIXME: integrate with netwaitXXX, should hack depend()

description="Wait for specific IP"

. /opt/etc/functions.openrc

DC_IP=${DC_IP-"10.1.5.15"}
WAIT_TIMEOUT=${WAIT_TIMEOUT-"20"}

depend()
{
	need net
	provide ${SVCNAME} pdc
}

wait_for_ip()
{
	local _rv=1 _count=$2
	while [ ${_count} -gt 0 ]; do
		ping -c 1 $1 > /dev/null 2>&1 && break
		_count=$((${_count}-1))
		update_me
		ewarn "${ME}: waiting for $1, ${_count} more times..."
		sleep 1
	done
	[ ${_count} -gt 0 ]
}

start()
{
	local _rv
	ebegin "Starting \"${SVCNAME}\""
	eindent
	wait_for_ip ${DC_IP} ${WAIT_TIMEOUT}
	_rv=$?
	eoutdent
	eend ${_rv}
	return ${_rv}
}

stop()
{
	ebegin "Stopping \"${SVCNAME}\""
	eend 0
	return 0
}
