#!/bin/sh
#
# qemu-guest-agent - QEMU Guest Agent
#
# chkconfig: 2345 10 65
# description:  Start the QEMU Guest Agent if we're running \
#		in a QEMU virtual machine
#
# processname: qemu-ga
# pidfile: /var/run/qemu-ga.pid
### BEGIN INIT INFO
# Provides:          qemu-guest-agent
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: QEMU Guest Agent startup script
# Description:       Start the QEMU Guest Agent if we're running
#                    in a QEMU virtual machine
### END INIT INFO
# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

NAME=qemu-ga
PIDFILE=/var/run/$NAME.pid
LOCKFILE=/var/lock/subsys/$NAME

# config
DAEMON_ARGS=""
# default transport
TRANSPORT=virtio-serial:/dev/virtio-ports/org.qemu.guest_agent.0
FSFREEZE_HOOK_PATHNAME=/etc/qemu/fsfreeze-hook

SourceIfNotEmpty /etc/sysconfig/qemu-ga

RETVAL=0

# Function that checks whenever system has necessary environment
# It also splits $TRANSPORT into $method and $path
#
do_check_transport() {
    method=${TRANSPORT%%:*}; path=${TRANSPORT#*:}
    case "$method" in
        virtio-serial | isa-serial)
	if [ ! -e "$path" ]; then
	    failure "$NAME: transport endpoint not found, not starting"
	    return 1
	fi
	;;
    esac
}

start()
{
	do_check_transport || exit 0
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- $NAME -d "$DAEMON_ARGS" -m "$method" -p "$path" -b "$BLACKLIST_RPC" -F$FSFREEZE_HOOK_PATHNAME

	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- $NAME
	RETVAL=$?
	return $RETVAL
	rm -f $PIDFILE
}

restart()
{
	stop
	start
}


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

exit $RETVAL
