#!/bin/sh

# PROVIDE:	vboxnet
# BEFORE:	NETWORKING
# REQUIRE:	FILESYSTEMS
# KEYWORD:	nojail

#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# vboxnet_enable (bool):   Set to NO by default.
#               Set it to YES to load network related kernel modules on startup

. /etc/rc.subr

name="vboxnet"
rcvar=vboxnet_enable
start_cmd="vboxnet_start"
stop_cmd="vboxnet_stop"

vboxnet_modules="vboxdrv vboxnetflt:ng_vboxnetflt vboxnetadp"

vboxnet_start()
{
	local _k _m

	for _m in ${vboxnet_modules}; do
		_k=${_m%:*}
		_m=${_m#*:}
		if ! kldstat -q -m ${_m}; then
			if ! kldload ${_k} > /dev/null 2>&1; then
				warn "Can't load ${_k} module."
				return 1
			fi
		fi
	done

	# initialize configured host-only interfaces
	LD_LIBRARY_PATH=%%PREFIX%%/lib %%PREFIX%%/bin/VBoxManage list hostonlyifs >/dev/null
}

vboxnet_stop()
{
	local _k _m _r

	_r=
	for _m in ${vboxnet_modules}; do
		_r="${_m} ${_r}"
	done
	for _m in ${_r}; do
		_k=${_m%:*}
		_m=${_m#*:}
		if kldstat -q -m ${_m}; then
			if ! kldunload ${_k} > /dev/null 2>&1; then
				warn "Can't unload ${_k} module."
				return 1
			fi
		fi
	done
}

load_rc_config $name

: ${vboxnet_enable="NO"}

run_rc_command "$1"
