#!/sbin/openrc-run

description="VPN proxy"

depend()
{
	before networking
}

start()
{
	if [ ! -d /sys/bus/vmbus ]; then
		# skip if not on Hyper-V
		exit 0
	fi
        if mobyconfig exists network
        then
                NETWORK_MODE="$(mobyconfig get network | tr -d '[[:space:]]')"
                if [ "${NETWORK_MODE}" = "hybrid" ]; then

			ebegin "Starting VPN proxy"

			PIDFILE=/var/run/tap-vsockd.pid
			start-stop-daemon --start \
				--exec /sbin/tap-vsockd \
				--background \
				--pidfile ${PIDFILE} \
				-- \
				--pidfile "${PIDFILE}" \
				--listen

			eend $? "Failed to start VPN proxy"
		fi
	fi
}

stop()
{
	if [ ! -d /sys/bus/vmbus ]; then
		# skip if not on Hyper-V
		exit 0
	fi
	ebegin "Stopping VPN proxy"

	PIDFILE=/var/run/tap-vsockd.pid

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

	eend $? "Failed to stop VPN proxy"
}
