#!/bin/sh
#
#     SUSE system startup script for the ovirt-guest-agent daemon
#     Copyright (C) 1995--2005  Kurt Garloff, SUSE / Novell Inc.
#     Copyright (C) 2014 Vinzenz Feenstra, Red Hat Inc.
#
#     This library is free software; you can redistribute it and/or modify it
#     under the terms of the GNU Lesser General Public License as published by
#     the Free Software Foundation; either version 2.1 of the License, or (at
#     your option) any later version.
#
#     This library is distributed in the hope that it will be useful, but
#     WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.
#
#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
#     USA.
#
# /etc/init.d/ovirt-guest-agent
#   and its symbolic link
# /(usr/)sbin/rcovirt-guest-agent
#
### BEGIN INIT INFO
# Provides:          ovirt-guest-agent
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: ovirt-guest-agent
# Description:       Start ovirt-guest-agent
### END INIT INFO


# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
ovirt_guest_agent_BIN=/usr/share/ovirt-guest-agent/ovirt-guest-agent.py
test -x $ovirt_guest_agent_BIN || { echo "$ovirt_guest_agent_BIN not installed";
    if [ "$1" = "stop" ]; then exit 0;
    else exit 5; fi; }

. /etc/rc.status

# Reset status of this service
rc_reset

pidfile=/var/run/ovirt-guest-agent.pid

case "$1" in
    start)
        echo -n "Starting ovirt-guest-agent "
        if [ ! -d /dev/virtio-ports ]; then
            modprobe virtio_console > /dev/null 2>&1
            for retries in `seq 5`; do
                sleep 1
                [ -d /dev/virtio-ports ] && break
            done
            [ -f /sbin/udevadm ] && /sbin/udevadm trigger
        fi
        if [ ! -f ${pidfile} ]; then
            /bin/touch ${pidfile}
            /bin/chown ovirtagent:ovirtagent ${pidfile}
        fi

        /sbin/startproc -u ovirtagent -g ovirtagent $ovirt_guest_agent_BIN
        rc_status -v
    ;;
    stop)
        echo -n "Shutting down ovirt-guest-agent "
        /sbin/killproc -TERM $ovirt_guest_agent_BIN
        rc_status -v
        ;;
    try-restart)
        $0 status
        if test $? = 0; then
            $0 restart
        else
            rc_reset
        fi
        rc_status
    ;;
    restart)
        $0 stop
        $0 start
        rc_status
    ;;
    force-reload)
        echo -n "Reload service ovirt-guest-agent "
        /sbin/killproc -HUP $ovirt_guest_agent_BIN
        rc_status -v
    ;;
    reload)
        echo -n "Reload service ovirt-guest-agent "
        /sbin/killproc -HUP $ovirt_guest_agent_BIN
        rc_status -v
    ;;
    status)
        echo -n "Checking for service ovirt-guest-agent "
        /sbin/checkproc $ovirt_guest_agent_BIN
        rc_status -v
    ;;
    probe)
        test /etc/ovirt-guest-agent.conf -nt /var/run/ovirt-guest-agent.pid && echo reload
    ;;
    *)
        echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
        exit 1
    ;;
esac

rc_exit
