#!/bin/bash
#
# cdispd        Configuration Dispatch Daemon
#
# Copyright (c) 2003 EU DataGrid.
# For license conditions see http://www.eu-datagrid.org/license.html
#
# #
# Current developer(s):
#   Luis Fernando Muñoz Mejías <Luis.Munoz@UGent.be>
#   Michel Jouvin <jouvin@lal.in2p3.fr>
#   Nick Williams <nick.williams@morganstanley.com>
#

# #
# Author(s): Germán Cancio Meliá, Marco Emilio Poleggi
#

#
# Init file for ncm-cdispd daemon
#
# chkconfig: 345 30 80
# description: The Configuration Dispatch Daemon

. /etc/rc.d/init.d/functions

DISPATCH=/usr/sbin/ncm-cdispd
RETVAL=0
PIDFILE=/var/run/ncm-cdispd.pid
LOCKFILE=/var/lock/quattor/ncm-cdispd
prog="ncm-cdispd"

start() {
    bash -c "echo -n 'Starting $prog:'"
    daemon "$DISPATCH" -D
    RETVAL=$?
    # Mark the attempt to start the daemon, even if not successful.
    # This will allow the monitoring cron job to attempt to restart it
    touch /var/lock/quattor/ncm-cdispd
    echo
}

stop() {
    bash -c "echo -n 'Stopping $prog:'"
    killproc $DISPATCH -TERM
    RETVAL=$?
    if [ "$RETVAL" = 0 ]
    then
        rm -f $LOCKFILE
        rm -f $PIDFILE
    fi
    echo
}

reload() {
    bash -c "echo -n 'Reloading $prog:'"
    killproc $DISPATCH -HUP
    RETVAL=$?
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $DISPATCH
        RETVAL=$?
        ;;
    reload)
        reload
        ;;
    restart)
        stop
        sleep 1
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|status}"
        RETVAL=1
esac

exit $RETVAL
