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

%config
    jaeger_enable="$openpkg_rc_def"
    jaeger_daemons="@daemons@"
    jaeger_flags_agent=""
    jaeger_flags_collector=""
    jaeger_flags_query=""
    jaeger_log_prolog="true"
    jaeger_log_epilog="true"
    jaeger_log_numfiles="10"
    jaeger_log_minsize="1M"
    jaeger_log_complevel="9"

%common
    jaeger_cfgfile_agent="@l_prefix@/etc/jaeger/jaeger-agent.cfg"
    jaeger_pidfile_agent="@l_prefix@/var/jaeger/run/jaeger-agent.pid"
    jaeger_logfile_agent="@l_prefix@/var/jaeger/log/jaeger-agent.log"
    jaeger_cfgfile_collector="@l_prefix@/etc/jaeger/jaeger-collector.cfg"
    jaeger_pidfile_collector="@l_prefix@/var/jaeger/run/jaeger-collector.pid"
    jaeger_logfile_collector="@l_prefix@/var/jaeger/log/jaeger-collector.log"
    jaeger_cfgfile_query="@l_prefix@/etc/jaeger/jaeger-query.cfg"
    jaeger_pidfile_query="@l_prefix@/var/jaeger/run/jaeger-query.pid"
    jaeger_logfile_query="@l_prefix@/var/jaeger/log/jaeger-query.log"
    jaeger_signal () {
        [ -f $jaeger_pidfile_agent ] && \
            kill -$1 `cat $jaeger_pidfile_agent`
        local jaeger_pid_agent=$?
        [ -f $jaeger_pidfile_collector ] && \
            kill -$1 `cat $jaeger_pidfile_collector`
        local jaeger_pid_collector=$?
        [ -f $jaeger_pidfile_query ] && \
            kill -$1 `cat $jaeger_pidfile_query`
        local jaeger_pid_query=$?
        [    $jaeger_pid_agent -eq 0 \
          -o $jaeger_pid_collector -eq 0 \
          -o $jaeger_pid_query -eq 0 ]
    }

%status -u @l_rusr@ -o
    jaeger_usable="unknown"
    jaeger_active="no"
    rcService jaeger enable yes && \
        jaeger_signal 0 && jaeger_active="yes"
    echo "jaeger_enable=\"$jaeger_enable\""
    echo "jaeger_usable=\"$jaeger_usable\""
    echo "jaeger_active=\"$jaeger_active\""

%start -u @l_rusr@
    rcService jaeger enable yes || exit 0
    rcService jaeger active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        for daemon in $jaeger_daemons; do
            cmd="nohup @l_prefix@/sbin/jaeger-$daemon"
            cmd="$cmd \$jaeger_flags_$daemon"
            eval "cfgfile=\$jaeger_cfgfile_$daemon"
            rcTmp -i
            tmpfile=`rcTmp -f -n flags`
            sed \
                -e '/^[[:space:]]*#.*/d' \
                -e '/^[[:space:]]*$/d' \
                -e 's/^\([[:space:]]*\)\([a-zA-Z]\)/\1--\2/' \
                -e ':a; $!N; s/\n/ /; ta' \
                -e 's;  *; ;g' <$cfgfile >$tmpfile
            cmd="$cmd `cat $tmpfile`"
            cmd="$cmd </dev/null >>\$jaeger_logfile_$daemon 2>&1 &"
            eval "$cmd"
            eval "echo \$! >\$jaeger_pidfile_$daemon"
            sleep 1
            rcTmp -k
        done
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService jaeger enable yes || exit 0
    rcService jaeger active no  && exit 0
    jaeger_signal TERM
    sleep 2
    rm -f $jaeger_pidfile_agent     >/dev/null 2>&1 || true
    rm -f $jaeger_pidfile_collector >/dev/null 2>&1 || true
    rm -f $jaeger_pidfile_query     >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService jaeger enable yes || exit 0
    logfiles=""
    for daemon in $jaeger_daemons; do
        eval "logfiles=\"\$logfiles \$jaeger_logfile_$daemon\""
    done
    shtool rotate -f \
        -n ${jaeger_log_numfiles} -s ${jaeger_log_minsize} -d \
        -z ${jaeger_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${jaeger_log_prolog}" \
        -E "${jaeger_log_epilog}; rc jaeger restart" \
        $logfiles

