#!/bin/sh

# the following is the LSB init header see
# http://www.linux-foundation.org/spec//booksets/LSB-Core-generic/LSB-Core-generic.html#INITSCRCOMCONV
#
### BEGIN INIT INFO
# Provides: libvirtd
# Should-Start: xend
# Default-Start: 3 4 5
# 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: 345 99 03
# description:  This is a daemon for managing guest instances
#               and libvirt virtual networks
#               See http://libvirt.org
#
# processname: libvirtd
# pidfile: /var/run/hde-libvirtd.pid
#

# Sanity checks.
[ -x /opt/hde/sbin/libvirtd ] || exit 0

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

SERVICE=hde-libvirtd
PROCESS=/opt/hde/sbin/libvirtd

LIBVIRTD_CONFIG=
LIBVIRTD_ARGS=
KRB5_KTNAME=/etc/opt/hde/libvirt/krb5.tab

test -f /etc/sysconfig/${SERVICE} && . /etc/sysconfig/${SERVICE}

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

RETVAL=0

start() {
    echo -n $"Starting $SERVICE daemon: "
    mkdir -p /opt/hde/var/cache/libvirt
    rm -rf /opt/hde/var/cache/libvirt/*
    if [ -s /var/run/$SERVICE.pid ]; then
      if [ ! -e /proc/`cat /var/run/$SERVICE.pid`/exe ]; then
        rm -f /var/run/$SERVICE.pid
      fi
    fi
    KRB5_KTNAME=$KRB5_KTNAME daemon --check $SERVICE $PROCESS --daemon $LIBVIRTD_CONFIG_ARGS $LIBVIRTD_ARGS --pid-file=/var/run/$SERVICE.pid
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE

    # deligate admin privillege to PRIVSEP_GROUP
    if [ "x${PRIVSEP_GROUP}" != "x" -a -d /var/lib/xend/ ]; then
      chgrp -R ${PRIVSEP_GROUP} /var/lib/xend/
      chmod -R g+rwx /var/lib/xend/
    fi
    if [ "x${PRIVSEP_GROUP}" != "x" -a  -d /var/run/xenstored/ ]; then
      chgrp -R ${PRIVSEP_GROUP} /var/run/xenstored/
      chmod -R g+rw /var/run/xenstored/
    fi
    if [ "x${PRIVSEP_GROUP}" != "x" -a  -e /proc/xen/privcmd ]; then
      chgrp ${PRIVSEP_GROUP} /proc/xen/privcmd
      chmod g+rw /proc/xen/privcmd
    fi
}

stop() {
    echo -n $"Stopping $SERVICE daemon: "

    killproc $PROCESS
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$SERVICE
        rm -f /var/run/$SERVICE.pid
	rm -rf /opt/hde/var/cache/libvirt/*
    fi
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $SERVICE configuration: "

    killproc $PROCESS -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

# See how we were called.
case "$1" in
    start|stop|restart|reload)
        $1
        ;;
    status)
        status $PROCESS
        RETVAL=$?
        ;;
    force-reload)
        reload
	;;
    condrestart|try-restart)
        [ -f /var/lock/subsys/$SERVICE ] && restart || :
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
	exit 1
        ;;
esac
exit $RETVAL
