#!@l_prefix@/bin/openpkg rc
##
##  rc.nomad -- Run-Commands
##

%config
    nomad_enable="$openpkg_rc_def"
    nomad_flags=""
    nomad_log_prolog="true"
    nomad_log_epilog="true"
    nomad_log_numfiles="10"
    nomad_log_minsize="1M"
    nomad_log_complevel="9"

%common
    nomad_cfgfile="@l_prefix@/etc/nomad/nomad.conf"
    nomad_pidfile="@l_prefix@/var/nomad/run/nomad.pid"
    nomad_logfile="@l_prefix@/var/nomad/log/nomad.log"
    nomad_signal () {
        [ -f $nomad_pidfile ] && kill -$1 `cat $nomad_pidfile`
    }

%status -u @l_rusr@ -o
    nomad_usable="unknown"
    nomad_active="no"
    rcService nomad enable yes && \
        nomad_signal 0 && nomad_active="yes"
    echo "nomad_enable=\"$nomad_enable\""
    echo "nomad_usable=\"$nomad_usable\""
    echo "nomad_active=\"$nomad_active\""

%start -u @l_rusr@
    rcService nomad enable yes || exit 0
    rcService nomad active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/nomad \
            agent \
            -config "$nomad_cfgfile" \
            $nomad_flags \
        </dev/null >>$nomad_logfile 2>&1 &
        echo $! >$nomad_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService nomad enable yes || exit 0
    rcService nomad active no  && exit 0
    nomad_signal TERM
    sleep 2
    rm -f $nomad_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService nomad enable yes || exit 0
    rcService nomad active no  && exit 0
    rc nomad stop start

%reload -u @l_rusr@
    rcService nomad enable yes || exit 0
    rcService nomad active no  && exit 0
    @l_prefix@/sbin/nomad reload

%daily -u @l_susr@
    rcService nomad enable yes || exit 0
    shtool rotate -f \
        -n ${nomad_log_numfiles} -s ${nomad_log_minsize} -d \
        -z ${nomad_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${nomad_log_prolog}" \
        -E "${nomad_log_epilog}; rc nomad restart" \
        $nomad_logfile

