#!/sbin/openrc-run

depend()
{
        before docker
}

start()
{
	ebegin "Setting up proxy port service"

	mkdir -p /port
	if [ -d /sys/bus/vmbus ]; then
		# Running on a Hyper-V hypervisor
		/sbin/9pmount-vsock --serviceid 0B95756A-9985-48AD-9470-78E060895BE7 listen port /port
	else
		mount -t 9p -o trans=virtio,dfltuid=1001,dfltgid=50,version=9p2000 port /port
	fi
	[ -n "${PIDFILE}" ] || PIDFILE=/var/run/proxy-vsockd.pid
	[ -n "${LOGFILE}" ] || LOGFILE=/var/log/proxy-vsockd.log

	ulimit -n 1048576
	start-stop-daemon --start --quiet \
		--background \
		--exec /sbin/proxy-vsockd \
		--make-pidfile --pidfile ${PIDFILE} \
		--stderr "${LOGFILE}" --stdout "${LOGFILE}" \
		-- -vsockPort 62373 -hvGuid 0B95756A-9985-48AD-9470-78E060895BE7

	eend $? "Failed to start proxy port service"
}

stop()
{
	ebegin "Stopping proxy port service"

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

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

	eend $? "Failed to stop proxy-vsockd"
}
