#!/bin/bash
#
# consul       Manage the consul agent
#
# chkconfig:   2345 95 05
# description: Consul is a tool for service discovery and configuration
# processname: consul
# config: /etc/consul.conf
# pidfile: /var/run/consul/consul.pid

### BEGIN INIT INFO
# Provides:       consul
# Required-Start: $local_fs $network
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:  0 1 6
# Short-Description: Manage the consul agent
# Description: Consul is a tool for service discovery and configuration
### END INIT INFO

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

USER=consul
GROUP=consul
CONFIG_FILE=/etc/consul.json
CONFIG_DIR=/etc/consul.d
PID_FILE=/var/run/consul/consul.pid
EXTRA_OPTS=""

prog="consul"
exec="/usr/bin/$prog"
lockfile="/var/lock/subsys/$prog"

# pull in sysconfig settings such as $CMD_OPTS and $LOG_FILE
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

CMD_OPTS="agent -config-file=$CONFIG_FILE -config-dir=$CONFIG_DIR -pid-file=$PID_FILE ${EXTRA_OPTS}"

conf_check() {
    [ -x $exec ] || exit 5
    [ -f $conffile ] || exit 6
    [ -d $confdir ] || exit 6

    $exec validate ${CONFIG_FILE}
    RETVAL=$?

    echo -n $"Sanity check on consul configuration"
    [ $RETVAL -eq 0 ] && success || failure
    echo

    return $RETVAL
}

start() {
    [ -x $exec ] || exit 5

    [ -f $conffile ] || exit 6
    [ -d $confdir ] || exit 6

    cd /
    umask 077

    echo -n $"Starting $prog: "

    # check if $LOG_FILE is set. Use syslog if it isn't.
    # we don't close the fds 0, 1 & 2, and instead redirect them to /dev/null,
    # to not risk possibility of consul writing messages to unrelated sockets.
    if [ -z ${LOG_FILE+x} ]; then
      daemon --user "-g $GROUP $USER" --pidfile "$PID_FILE" "$exec $CMD_OPTS -syslog </dev/null 1>&0 2>&0 &"
    else
      daemon --user "-g $GROUP $USER" --pidfile "$PID_FILE" "$exec $CMD_OPTS </dev/null >> $LOG_FILE 2>&1 &"
    fi

    RETVAL=$?

    [ $RETVAL -eq 0 ] && touch $lockfile
    [ $RETVAL -eq 0 ] && success || failure

    echo

    return $RETVAL
}

stop() {
    echo -n $"Shutting down $prog: "
    killproc -p $PID_FILE $exec
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    sudo -u $USER -H $exec reload > /dev/null && success || failure
    RETVAL=$?
    echo
    return $RETVAL
}

force_reload() {
    restart
}

rh_status() {
    status -p "$PID_FILE" -l $prog $exec
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        conf_check && $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        conf_check && $1
        ;;
    reload)
        rh_status_q || exit 7
        conf_check && $1
        ;;
    force-reload)
        conf_check && force_reload
        ;;
    confcheck)
        conf_check
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        conf_check && restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|confcheck|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac

exit $?
