#!/bin/sh

# PROVIDE: vboxheadless
# REQUIRE: LOGIN vboxnet
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf[.local] to enable vboxheadless
#
# vboxheadless_enable (bool):         Set to "NO" by default.
#                                     Set it to "YES" to enable vboxheadless.
# vboxheadless_machines (str):        Space separated list of machines 
# vboxheadless_user (str):            Default user account to run with.
#                                     (default: vboxusers)
# vboxheadless_stop (str):            Default stop cmd for VBoxManage controlvm.
#                                     (default: savestate)
# vboxheadless_delay (int):           Default startup/shutdown delay in seconds.
#                                     (default: 0)
# vboxheadless_<machine>_name (str):  Virtualbox machine name or UUID.
# vboxheadless_<machine>_user (str):  User account to run with.
# vboxheadless_<machine>_flags (str): Additional flags for VBoxHeadless.
# vboxheadless_<machine>_stop (str):  Stop command for VBoxManage controlvm.
# vboxheadless_<machine>_delay (int): Startup and shutdown delay in seconds.

. /etc/rc.subr

name="vboxheadless"
rcvar=vboxheadless_enable
rc_fast="YES"

command="/usr/local/lib/virtualbox/VBoxHeadless"
pidbase="/var/run/${name}"
   
start_cmd="${name}_start"
stop_cmd="${name}_stop"
status_cmd="${name}_status"

vboxheadless_start()
{
	local machine mpidfile pid vmname vmuser vmflags vmdelay

	echo "Starting Virtual Machines:"
	for machine in ${vboxheadless_machines}; do
		mpidfile="${pidbase}_${machine}.pid"
		pid=$(check_pidfile $mpidfile $command)
		eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
		eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
		eval vmflags="\${vboxheadless_${machine}_flags:-}"
		eval vmdelay="\${vboxheadless_${machine}_delay:-${vboxheadless_delay}}"

		HOME=$(/usr/sbin/pw usershow -7 -n "${vmuser}" | /usr/bin/cut -d: -f6)

		/usr/bin/printf "%25s " "${vmname}"

		/usr/bin/su ${vmuser} -c "/usr/local/lib/virtualbox/VBoxManage showvminfo '${vmname}' >/dev/null" 2>/dev/null

		if [ $? != 0 ]; then
			echo "Unknown machine"
			continue
		fi

		if [ -n "${pid}" ]; then 
			echo "Already running? (pid=${pid})"
			continue
		fi

		/bin/sleep ${vmdelay}
		/usr/bin/install -o ${vmuser} -g vboxusers -m 644 /dev/null ${mpidfile}
		/usr/sbin/daemon -f -p ${mpidfile} -u ${vmuser} ${command} --startvm "${vmname}" ${vmflags}
		echo "Started"
	done
}

vboxheadless_stop()
{
	local machine mpidfile pid pids vmname vmuser vmstop vmdelay

	echo "Saving states for Virtual Machines:"
	for machine in ${vboxheadless_machines}; do
		mpidfile="${pidbase}_${machine}.pid"
		pid=$(check_pidfile $mpidfile $command)
		eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
		eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"
		eval vmstop="\${vboxheadless_${machine}_stop:-${vboxheadless_stop}}"
		eval vmdelay="\${vboxheadless_${machine}_delay:-${vboxheadless_delay}}"

		/usr/bin/printf "%25s " "${vmname}"

		if [ -n "${pid}" ]; then
			pids="${pids} ${pid}"
			/bin/sleep ${vmdelay}
			/usr/bin/su ${vmuser} -c "/usr/local/lib/virtualbox/VBoxManage controlvm '${vmname}' ${vmstop} >/dev/null &" 2>/dev/null
		fi
	done
	if [ -n "${pids}" ]; then
		wait_for_pids $pids >/dev/null
		echo "Stopped"
	else
		echo "Not running?"
	fi
}

vboxheadless_status()
{
	local machine mpidfile pid vmname vmuser

	/usr/bin/printf "%25s %s\n" "Machine" "Status"
	/usr/bin/printf "%25s %s\n" "-------------------------" "------------"

	for machine in ${vboxheadless_machines}; do
		mpidfile="${pidbase}_${machine}.pid"
		pid=$(check_pidfile $mpidfile $command)
		eval vmname="\${vboxheadless_${machine}_name:-${machine}}"
		eval vmuser="\${vboxheadless_${machine}_user:-${vboxheadless_user}}"

		/usr/bin/su ${vmuser} -c "/usr/local/lib/virtualbox/VBoxManage showvminfo '${vmname}' >/dev/null" 2>/dev/null

		if [ $? != 0 ]; then
			/usr/bin/printf "%20s %s\n" "${vmname}" "Unknown Machine"
		elif [ -n "${pid}" ]; then
			/usr/bin/printf "%25s %s\n" "${vmname}" "Running"
		else
			/usr/bin/printf "%25s %s\n" "${vmname}" "Powered Off"
		fi
	done
}

load_rc_config $name

: ${vboxheadless_enable="NO"}
: ${vboxheadless_user="vboxusers"}
: ${vboxheadless_stop="savestate"}
: ${vboxheadless_delay="0"}

cmd_arg="$1" ; shift

if [ -n "$*" ]; then
    vboxheadless_machines="$*"
fi

run_rc_command "${cmd_arg}"
