#!/bin/sh
#
# Sun xVM VirtualBox
# Linux Additions timesync daemon init script
#
# Copyright (C) 2006-2007 Sun Microsystems, Inc.
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
# Clara, CA 95054 USA or visit http://www.sun.com if you need
# additional information or have any questions.
#

# chkconfig: 35 35 56
# description: VirtualBox Additions timesync
#
### BEGIN INIT INFO
# Provides:       vboxadd-timesync
# Required-Start: vboxadd
# Required-Stop:  vboxadd
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Description:    VirtualBox Additions timesync
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

LOCKFILE="/var/lock/subsys/vboxadd-timesync"
PIDFILE="/var/run/vboxadd-timesync"
RETVAL=0

binary=/usr/sbin/vboxadd-timesync

test -x "$binary" || {
	echo "Cannot run $binary"
	exit 1
}

vboxaddrunning()
{
	lsmod | grep -q "vboxadd[^_-]"
}

start()
{
	msg_starting $"Starting VirtualBox host to guest time synchronisation"
	vboxaddrunning || {
		echo "VirtualBox Additions module not loaded!"
		exit 1
	}
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce -- $binary
	RETVAL=$?
	return $RETVAL
}

stop()
{
	msg_stopping $"Stopping VirtualBox host to guest time synchronisation"
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce $binary
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
