#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License
#
# /etc/rc.d/init.d/qdrouterd
#
# Startup script for the Qpid Router.
#
# <tags -- see below for tag definitions. *Every line* from the top
# of the file to the end of the tags section must begin with a #
# character. After the tags section, there should be a blank line.
# This keeps normal comments in the rest of the file from being
# mistaken for tags, should they happen to fit the pattern.>

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

### BEGIN INIT INFO
# Provides: qdrouterd
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start or stop qdrouterd
# Description: Qpid Router is an intermediary for AMQP messaging.
### END INIT INFO

prog=qdrouterd
exe=/usr/sbin/$prog

# Configurable variables, can be set in /etc/sysconfig/$prog
USER=qdrouterd
CONFIG=/etc/qpid-dispatch/qdrouterd.conf
RUNDIR=/var/run/qpid-dispatch

if [ -f /etc/sysconfig/$prog ]; then
    . /etc/sysconfig/$prog
fi

pidfile=$RUNDIR/$prog.pid

# ensure binary is present before continuing
if [[ !(-x $exe) ]]; then
    echo "$exe not found or is not executable"
    exit 5			# Not installed
fi

start() {
    echo -n "Starting qdrouterd services: "
    daemon --check $prog --user $USER --pidfile $pidfile $exe --daemon --config $CONFIG --pidfile $pidfile
    echo
    RETVAL=$?
}

stop() {
    echo -n "Shutting down qdrouterd services: "
    killproc -p $pidfile $prog
    echo
}

restart() {
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;

    status)
        status $prog
        RETVAL=$?
        ;;

    try-restart|condrestart)
        status $prog
        if [ "$?" -eq "0" ]; then restart; fi
        ;;

    reload|force-reload)
        exit 3			# Not implemented
        ;;

    *)
        echo "Usage: $0 {start|stop|status|restart|reload|try-restart|condrestart|force-reload}"
        exit 2			# Invalid arguments
        ;;
esac

exit $RETVAL
