#!/bin/bash
#
# rhev-agentd - Startup script for the RHEV agent daemon
#
# chkconfig: 35 85 15
# description: Startup/shutdown script for the RHEV-M agent.
# processname: rhev-agentd
# config: /etc/rhev-agent.conf
# pidfile: /var/run/rhev-agent/rhev-agent.pid

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

agentd=/usr/share/rhev-agent/rhev-agentd.py
prog=rhev-agentd
lockfile=/var/lock/subsys/rhev-agent/${prog}
pidfile=/var/run/rhev-agent/${prog}.pid

RETVAL=0

start() {
    echo -n $"Starting $prog: "
    # RHEL5 doesn't automatically load the virtio serial module.
    if [ ! -d /dev/virtio-ports ]; then
        modprobe virtio_console >/dev/null 2>&1
        # Give udev some time to create the symbolic links.
        for retries in `seq 5`; do
            sleep 1
            [ -d /dev/virtio-ports ] && break
        done
        [ -f /sbin/udevtrigger ] && /sbin/udevtrigger
    fi
    daemon --pidfile=${pidfile} --user=rhevagent $agentd $OPTIONS
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    echo
    return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	killproc -p ${pidfile} -d 10 $agentd
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f $lockfile
	echo
}

case "$1" in

    start)
        start
        ;;

    stop)
        stop
        ;;

    status)
        status -p ${pidfile} $agentd
        RETVAL=$?
        ;;

    restart)
        stop
        start
        ;;

    condrestart)
        if [ -f ${pidfile} ] ; then
            stop
            start
        fi
        ;;

    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|status}"
        exit 1
esac

exit $RETVAL
