#!/bin/bash
#
# hv_vss_daemon Implementation of VSS daemon for Linux
#
# chkconfig:   345 20 80
# description: Implementation of VSS daemon for Linux.
#

### BEGIN INIT INFO
# Provides:          hv_vss_daemon
# Required-Start:    $null
# Should-Start:      $syslog $remote_fs $time
# Required-Stop:     $null
# Should-Stop:       $syslog $remote_fs $time
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: hv_vss_daemon provides info to the host
# Description:       Start hv_vss_daemon to allow the host to query this guest
### END INIT INFO

. /etc/rc.d/init.d/functions

RETVAL=0
HV_VSS_BIN="/usr/sbin/hv_vss_daemon"
lockfile=/var/lock/subsys/hypervvssd

# Check for missing binaries
test -x $HV_VSS_BIN || { echo "$HV_VSS_BIN not installed";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

start(){
	# check if Hyper-V drivers are present
	if [ -z "`lsmod | grep hv_utils`" ]; then
		echo "Hyper-V drivers are not loaded."
		echo -n "Hyper-V VSS daemon not started:"
		warning
		echo
		# return LSB exit code "program is not running" ~ 7
		RETVAL=7
		return $RETVAL
	fi
	
	if ! status $HV_VSS_BIN >&/dev/null; then
		echo -n "Starting Hyper-V VSS daemon "
		daemon $HV_VSS_BIN
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch $lockfile
	else
		echo -n "Hyper-V VSS daemon is already started"
	fi
	echo
}

stop(){
	echo -n "Shutting down Hyper-V VSS daemon "
	killproc $HV_VSS_BIN
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $lockfile
}

restart() {
	echo -n "Hyper-V VSS daemon does not support restarting"
	warning
	echo
	RETVAL=3
	return $RETVAL
}

reload() {
	restart
}

force_reload() {
	restart
}

rh_status() {
	# run checks to determine if the service is running or use generic status
	status $HV_VSS_BIN
	RETVAL=$?
	return $RETVAL
}

rh_status_q() {
	rh_status >/dev/null 2>&1
}

case "$1" in
	start)
		rh_status_q && exit 0
		$1
		;;
	stop)
		rh_status_q || exit 0
		$1
		;;
	restart)
		$1
		;;
	reload)
		rh_status_q || exit 7
		$1
		;;
	force-reload)
		force_reload
		;;
	status)
		rh_status
		;;
	condrestart|try-restart)
		rh_status_q || exit 0
		restart
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
		exit 2
esac

exit $RETVAL
