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

%config
    vault_enable="$openpkg_rc_def"
    vault_flags="-log-level=info"
    vault_log_prolog="true"
    vault_log_epilog="true"
    vault_log_numfiles="10"
    vault_log_minsize="1M"
    vault_log_complevel="9"

%common
    vault_cfgfile="@l_prefix@/etc/vault/vault.hcl"
    vault_datdir="@l_prefix@/var/vault/data"
    vault_pidfile="@l_prefix@/var/vault/run/vault.pid"
    vault_logfile="@l_prefix@/var/vault/log/vault.log"
    vault_signal () {
        [ -f $vault_pidfile ] && kill -$1 `cat $vault_pidfile`
    }

%status -u @l_rusr@ -o
    vault_usable="unknown"
    vault_active="no"
    rcService vault enable yes && \
        vault_signal 0 && vault_active="yes"
    echo "vault_enable=\"$vault_enable\""
    echo "vault_usable=\"$vault_usable\""
    echo "vault_active=\"$vault_active\""

%start -u @l_rusr@
    rcService vault enable yes || exit 0
    rcService vault active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/bin/vault \
            server \
            -config "$vault_cfgfile" \
            $vault_flags \
        </dev/null >>$vault_logfile 2>&1 &
        echo $! >$vault_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService vault enable yes || exit 0
    shtool rotate -f \
        -n ${vault_log_numfiles} -s ${vault_log_minsize} -d \
        -z ${vault_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${vault_log_prolog}" \
        -E "${vault_log_epilog}; rc vault restart" \
        $vault_logfile

