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

%config
    spire_enable="$openpkg_rc_def"
    spire_daemons="@daemons@"
    spire_flags_agent=""
    spire_flags_server=""
    spire_log_prolog="true"
    spire_log_epilog="true"
    spire_log_numfiles="10"
    spire_log_minsize="1M"
    spire_log_complevel="9"

%common
    spire_cfgfile_agent="@l_prefix@/etc/spire/spire-agent.conf"
    spire_pidfile_agent="@l_prefix@/var/spire/run/spire-agent.pid"
    spire_logfile_agent="@l_prefix@/var/spire/log/spire-agent.log"
    spire_cfgfile_server="@l_prefix@/etc/spire/spire-server.conf"
    spire_pidfile_server="@l_prefix@/var/spire/run/spire-server.pid"
    spire_logfile_server="@l_prefix@/var/spire/log/spire-server.log"
    spire_signal () {
        [ -f $spire_pidfile_agent ] && \
            kill -$1 `cat $spire_pidfile_agent`
        local spire_pid_agent=$?
        [ -f $spire_pidfile_server ] && \
            kill -$1 `cat $spire_pidfile_server`
        local spire_pid_server=$?
        [    $spire_pid_agent -eq 0 \
          -o $spire_pid_server -eq 0 ]
    }

%status -u @l_rusr@ -o
    spire_usable="unknown"
    spire_active="no"
    rcService spire enable yes && \
        spire_signal 0 && spire_active="yes"
    echo "spire_enable=\"$spire_enable\""
    echo "spire_usable=\"$spire_usable\""
    echo "spire_active=\"$spire_active\""

%start -u @l_rusr@
    rcService spire enable yes || exit 0
    rcService spire active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        join_token=""
        for daemon in $spire_daemons; do
            cmd="nohup @l_prefix@/sbin/spire-$daemon"
            cmd="$cmd run"
            cmd="$cmd --config \$spire_cfgfile_$daemon"
            if [ ".$daemon" = .agent -a ".$join_token" != . ]; then
                cmd="$cmd -joinToken \"$join_token\""
            fi
            cmd="$cmd \$spire_flags_$daemon"
            cmd="$cmd </dev/null >>\$spire_logfile_$daemon 2>&1 &"
            eval "$cmd"
            eval "echo \$! >\$spire_pidfile_$daemon"
            sleep 1
            if [ ".$daemon" = .server ]; then
                sleep 3
                join_token=`@l_prefix@/sbin/spire-server token generate -serverAddr 127.0.0.1:8081 | awk '{ print $2; }'`
            fi
        done
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService spire enable yes || exit 0
    rcService spire active no  && exit 0
    spire_signal TERM
    sleep 2
    pidfiles=""
    for daemon in $spire_daemons; do
        eval "pidfiles=\"\$pidfiles \$spire_pidfile_$daemon\""
    done
    rm -f $pidfiles >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService spire enable yes || exit 0
    logfiles=""
    for daemon in $spire_daemons; do
        eval "logfiles=\"\$logfiles \$spire_logfile_$daemon\""
    done
    shtool rotate -f \
        -n ${spire_log_numfiles} -s ${spire_log_minsize} -d \
        -z ${spire_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${spire_log_prolog}" \
        -E "${spire_log_epilog}; rc spire restart" \
        $logfiles

