#!/bin/bash
#
#       /etc/rc.d/init.d/<%= @name %>
#
#       Daemonize the prometheus <%= @name %>.
#
# chkconfig:   2345 95 20
# description: Prometheus <%= @name %>
# processname: <%= @name %>
# pidfile: /var/run/<%= @name %>/pidfile

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

DAEMON=<%= @bin_dir %>/<%= @name %>
PID_FILE=/var/run/<%= @name %>/<%= @name %>.pid
LOG_FILE=/var/log/<%= @name %>

[ -e /etc/sysconfig/<%= @name %> ] && . /etc/sysconfig/<%= @name %>

export GOMAXPROCS=${GOMAXPROCS:-2}

#
# Create the /var/run/<%= @name %> directory, which can live on a tmpfs
# filesystem and be destroyed between reboots.
#
mkrundir() {
        [ ! -d /var/run/<%= @name %> ] && mkdir -p /var/run/<%= @name %>
        chown <%= @user %> /var/run/<%= @name %>
}

#
# Create a PID file if it doesn't already exist, for clean upgrades
# from previous init-script controlled daemons.
#
KILLPROC_OPT="-p ${PID_FILE}"
mkpidfile() {
        # Create PID file if it didn't exist
        mkrundir
        [ ! -f $PID_FILE ] && pidofproc $DAEMON > $PID_FILE
        chown <%= @user %> /var/run/<%= @name %>
        if [ $? -ne 0 ] ; then
            rm $PID_FILE
            KILLPROC_OPT=""
        fi
}

start() {
        echo -n "Starting prometheus <%= @name %>: "
        mkrundir
        [ -f $PID_FILE ] && rm $PID_FILE
        daemon --user=<%= @user %> \
            --pidfile="$PID_FILE" \
            <%- require 'shellwords' -%>
            "$DAEMON" <%= Shellwords.escape(@options) %> >> "$LOG_FILE" &
        retcode=$?
        mkpidfile
        touch /var/lock/subsys/<%= @name %>
        return $retcode
}

stop() {
        DELAY=5 # seconds maximum to wait for a leave

        echo -n "Shutting down prometheus <%= @name %>: "
        mkpidfile

        <%= @name %>_pid=$(cat $PID_FILE)
        killproc $KILLPROC_OPT $DAEMON -INT
        retcode=$?

        # We'll wait if necessary to make sure the leave works, and return
        # early if we can.  If not, escalate to harsher signals.
        try=0
        while [ $try -lt $DELAY ]; do
        if ! checkpid $<%= @name %>_pid ; then
          rm -f /var/lock/subsys/<%= @name %>
          return $retcode
        fi
        sleep 1
          let try+=1
        done

        # If acting as a server, use a SIGTERM to avoid a leave.
        # This behavior is also configurable.  Avoid doing a "leave" because
        # having servers missing is a bad thing that we want to notice.
        #
        # A SIGTERM will mark the node as "failed" until it rejoins.
        # killproc with no arguments uses TERM, then escalates to KILL.
        killproc $KILLPROC_OPT $DAEMON
        retcode=$?

        rm -f /var/lock/subsys/<%= @name %> $PID_FILE
        return $retcode
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p ${PID_FILE} $DAEMON
        ;;
    restart)
        stop
        start
        ;;
    reload)
        mkpidfile
        killproc $KILLPROC_OPT $DAEMON -HUP
        ;;
    condrestart)
        [ -f /var/lock/subsys/<%= @name %> ] && restart || :
        ;;
    *)
        echo "Usage: <%= @name %> {start|stop|status|reload|restart}"
        exit 1
        ;;
esac
retcode=$?
# Don't let the [OK] get stomped on.
echo
exit $retcode
