#!/sbin/openrc-run

description="vsock socket proxy client"

depend()
{
	before logger docker
}

start()
{
	[ "$(mobyplatform)" != "mac" ] && [ "$(mobyplatform)" != "windows" ] && exit 0

	ebegin "Starting vsock proxy"

	if [ "$(mobyplatform)" = "windows" ]; 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()
{
	[ "$(mobyplatform)" != "mac" ] && [ "$(mobyplatform)" != "windows" ] && exit 0

	ebegin "Stopping docker socket passthrough"

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

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

	eend $? "Failed to stop vsudd"
}
