#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          prometheus-pushgateway
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Prometheus exporter for ephemereal jobs
# Description:       The Prometheus Pushgateway exists to allow ephemeral and
#                    batch jobs to expose their metrics to Prometheus. Since
#                    these kinds of jobs may not exist long enough to be
#                    scraped, they can instead push their metrics to a
#                    Pushgateway.  The Pushgateway then exposes these metrics
#                    to Prometheus.
### END INIT INFO

# Author: Martina Ferrari <tina@debian.org>
# Author: Guillem Jover <gjover@sipwise.com>

DESC="Prometheus exporter for ephemereal jobs"
NAME=prometheus-pushgateway
USER=prometheus
GROUP=$USER
DAEMON=/usr/bin/$NAME
PIDFILE=/run/prometheus/$NAME.pid
LOGFILE=/var/log/prometheus/$NAME.log

START_ARGS="--no-close --background --make-pidfile"
STOP_ARGS="--remove-pidfile"

do_start_prepare()
{
  mkdir -p $(dirname $PIDFILE)
}

do_start_cmd_override()
{
  start-stop-daemon --start --quiet --oknodo \
    --exec $DAEMON --pidfile $PIDFILE --user $USER --group $GROUP \
    --chuid $USER:$GROUP $START_ARGS -- $ARGS >>$LOGFILE 2>&1
}

do_stop_cmd_override()
{
  start-stop-daemon --stop --quiet --oknodo --retry=TERM/30/KILL/5 \
    --exec $DAEMON --pidfile $PIDFILE --user $USER $STOP_ARGS
}

alias do_reload=do_reload_sigusr1
