#!/usr/bin/openrc-run

description="Wait for specific host to become pingable"

. /opt/etc/functions.openrc

# /etc/conf.d/netwait variables:
# GOOD_HOST - IP of host to ping

GOOD_HOST=${GOOD_HOST-"8.8.8.8"}
TIMEOUT=${TIMEOUT-"30"}

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

start()
{
	local _rv=1 count=${TIMEOUT}
	ebegin "Starting \"${SVCNAME}\""
	eindent
	while [ ${count} -gt 0 ]; do
		if ping -c 1 ${GOOD_HOST} > /dev/null 2>&1; then
			_rv=0
			break
		fi
		count=$(($count-1))
		update_me
		ewarn "${ME}: no link, ${count} more tries..."
		sleep 1
	done
	eoutdent
	eend ${_rv}
	return ${_rv}
}

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