#!/bin/sh
#
# open-vm-tools	Helper scripts for open-vm-tools
#
# chkconfig:	345 90 20
#
# description:	Helper scripts for open-vm-tools
#
# $Id$

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

RETVAL=0
kver=`uname -r`

is_ESX_running() {
    if [ ! -f /usr/bin/vmware-checkvm ] ; then
		echo no
		return
    fi
    
    if /usr/bin/vmware-checkvm -p | grep -q ESX; then
		echo yes
    else
		echo no
    fi
}

# return true if $module exists for current kernel
module_exist() {
	local module=$1
	test -f /lib/modules/$kver/misc/$module.ko*
}

start_vmblock() {
	# vmblock is not required and unsupported on ESX so first check
	# if it's installed then try to use
	module_exist vmblock || return

	# Check if the service is already running?
	if [ -f /var/lock/subsys/open-vm-tools-vmblock ]; then
		msg_already_running "Open Virtual Machine vmblock script"
		return
	fi

	_modprobe single vmblock
	msg_starting "Open Virtual Machine vmblock script"
	busy
	mkdir -p /tmp/VMwareDnD
	chmod 1777 /tmp/VMwareDnD
	mount -t vmblock none /proc/fs/vmblock/mountPoint
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/open-vm-tools-vmblock && ok && return
	fail
}

stop_vmblock() {
	module_exist vmblock || return

	if [ ! -f /var/lock/subsys/open-vm-tools-vmblock ]; then
		msg_not_running "Open Virtual Machine vmblock script"
		return
	fi

	msg_stopping "Open Virtual Machine vmblock script"
	busy
	umount /proc/fs/vmblock/mountPoint
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/open-vm-tools-vmblock && ok && return
	fail
}

start_vmhgfs() {
	# vmhgfs is not required and usupported on ESX so first check
	# if it's installed then try to use
	module_exist vmhgfs || return

	# Check if the service is already running?
	if [ -f /var/lock/subsys/open-vm-tools-vmhgfs ]; then
		msg_already_running "Open Virtual Machine vmhgfs script"
		return
	fi

	_modprobe single vmhgfs
	msg_starting "Open Virtual Machine vmhgfs script"
	busy
	mkdir -p /mnt/hgfs
	mount -t vmhgfs .host:/ /mnt/hgfs
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/open-vm-tools-vmhgfs && ok && return
	fail
}

stop_vmhgfs() {
	module_exist vmhgfs || return

	if [ ! -f /var/lock/subsys/open-vm-tools-vmhgfs ]; then
		msg_not_running "Open Virtual Machine vmhgfs script"
		return
	fi

	msg_stopping "Open Virtual Machine vmhgfs script"
	busy
	umount /mnt/hgfs
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/open-vm-tools-vmhgfs && ok && return
	fail
}

start_vmsync() {
	# Check if the service is already running?
	if [ -f /var/lock/subsys/open-vm-tools-vmsync ]; then
		msg_already_running "Open Virtual Machine vmsync script"
		return
	fi

	msg_starting "Open Virtual Machine vmsync script"
	daemon /usr/bin/vmtoolsd --background /var/run/vmtoolsd.pid
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/open-vm-tools-vmsync
}

stop_vmsync() {
	if [ ! -f /var/lock/subsys/open-vm-tools-vmsync ]; then
		msg_not_running "Open Virtual Machine vmsync script"
		return
	fi

	msg_stopping "Open Virtual Machine vmsync script"
	killproc --pidfile vmtoolsd.pid vmtoolsd
	rm -f /var/lock/subsys/open-vm-tools-vmsync
}

start() {
	if is_no `is_ESX_running`; then
		start_vmblock
		start_vmhgfs
	fi
	_modprobe single vmware_vmmemctl
	_modprobe single vmw_vmci
	_modprobe single vsock
	start_vmsync
}

stop() {
	stop_vmblock
	stop_vmhgfs
	stop_vmsync
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
  	;;
  *)
	msg_usage "$0 {start|stop|restart}"
	exit 3
esac

exit $RETVAL
