#!/sbin/openrc-run

description="vsock socket proxy client"

depend()
{
	before logger docker
}

start()
{
	ebegin "Starting vsock proxy"

	if [ -d /sys/bus/vmbus ]; then
		DOCKER_PORT=23a432c2-537a-4291-bcb5-d62504644739
		SYSLOG_PORT="" #TBD
	else
		DOCKER_PORT=2376
		SYSLOG_PORT=514
	fi

	[ -n "${PIDFILE}" ] || PIDFILE=/var/run/vsudd.pid

	if [ -n "$SYSLOG_PORT" ] ; then
	   # Can be inlined below once Windows defines syslog port
	   SYSLOG_OPT="-syslog ${SYSLOG_PORT}:/var/run/syslog.vsock"
	fi

	start-stop-daemon --start --quiet \
		--background \
		--exec /sbin/vsudd \
		--make-pidfile --pidfile ${PIDFILE} \
		-- -inport "${DOCKER_PORT}:unix:/var/run/docker.sock" \
		   ${SYSLOG_OPT} \
		   -detach

	eend $? "Failed to start vsudd"
}

stop()
{
	ebegin "Stopping docker socket passthrough"

	[ -n "${PIDFILE}" ] || PIDFILE=/var/run/vsudd.pid

	start-stop-daemon --stop --quiet --pidfile ${PIDFILE}

	eend $? "Failed to stop vsudd"
}
