#!/bin/sh

# the following is the LSB init header see
# http://refspecs.linuxfoundation.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/initscrcomconv.html
#
### BEGIN INIT INFO
# Provides: libvirtd
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Required-Start: $network messagebus virtlogd
# Required-Stop: $network messagebus
# Should-Start: $named xend avahi-daemon virtlockd
# Should-Stop: $named
# Short-Description: daemon for libvirt virtualization API
# Description: This is a daemon for managing guest instances
#              and libvirt virtual networks
#              See http://libvirt.org
### END INIT INFO

# the following is chkconfig init header
#
# libvirtd:   guest and virtual network management daemon
#
# chkconfig: - 97 03
# description:  This is a daemon for managing guest instances \
#               and libvirt virtual networks \
#               See http://libvirt.org
#
# processname: libvirtd
# pidfile: /var/run/libvirtd.pid
#

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

SERVICE=libvirtd
PROCESS=libvirtd
PIDFILE=/var/run/$SERVICE.pid
LOCKFILE=/var/lock/subsys/$SERVICE

LIBVIRTD_CONFIG=
LIBVIRTD_ARGS=
KRB5_KTNAME=/etc/libvirt/krb5.tab
export KRB5_KTNAME

SourceIfNotEmpty /etc/sysconfig/libvirtd

export QEMU_AUDIO_DRV
export SDL_AUDIODRIVER

LIBVIRTD_CONFIG_ARGS=
if [ -n "$LIBVIRTD_CONFIG" ]
then
    LIBVIRTD_CONFIG_ARGS="--config $LIBVIRTD_CONFIG"
fi

RETVAL=0

start()
{
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- "$SERVICE" --daemon $LIBVIRTD_CONFIG_ARGS $LIBVIRTD_ARGS
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root -- "$SERVICE"
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
	    rm -f /var/lock/subsys/$SERVICE
	    rm -rf /var/cache/libvirt/*
	fi
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading "$SERVICE"
	stop_daemon --pidfile "$PIDFILE" --expect-user root -HUP -- "$SERVICE"
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    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 $PROCESS
        RETVAL=$?
        ;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
        RETVAL=1
esac

exit $RETVAL
