#!/bin/sh
# (C) 2007 Michael Meskes <meskes@debian.org>

### BEGIN INIT INFO
# Provides:          vboxadd
# Short-Description: VirtualBox Linux Additions
# Required-Start:
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO

PATH=$PATH:/bin:/sbin:/usr/sbin

. /lib/lsb/init-functions

in_virtual_machine()
{
	if [ -z "$(lspci -d 80ee:beef)" ]; then
		log_warning_msg 'VirtualBox Additions Disabled, not in a Virtual Machine';
		return 1
	fi

	return 0
}

case "$1" in
  start)
  	in_virtual_machine || exit 0
  	log_action_begin_msg 'Starting VirtualBox Additions';
	# udev should have already handled this module
	# double check kernel support is present or die
	modprobe --quiet vboxadd
	if [ "$?" -ne 0 ]; then
		# vboxadd not installed, or has a problem
		log_failure_msg 'cannot modprobe vboxadd kernel module';
		log_end_msg 1
		exit 1
	fi

	start-stop-daemon --start --quiet --exec /usr/sbin/vboxadd-timesync -- --daemonize
	log_end_msg $?
	;;
  stop)
  	in_virtual_machine || exit 0
  	log_action_begin_msg 'Stopping VirtualBox Additions';
	start-stop-daemon --stop --quiet --oknodo --exec /usr/sbin/vboxadd-timesync
	log_end_msg $?
	;;
  restart|force-reload)
	#
	#       If the "reload" option is implemented, move the "force-reload"
	#       option to the "reload" entry. If not, "force-reload" is
	#       just the same as "restart".
	#
	$0 stop
	$0 start
	;;
  *)
  	echo "Usage: $0 {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
