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

%config
    orchestrator_enable="$openpkg_rc_def"
    orchestrator_flags=""
    orchestrator_log_rotsteps="10"
    orchestrator_log_rotminsize="10M"
    orchestrator_log_rotcomplevel="9"
    orchestrator_log_rotprolog="true"
    orchestrator_log_rotepilog="true"

%common
    orchestrator_cfgfile="@l_prefix@/etc/orchestrator/orchestrator.json"
    orchestrator_pidfile="@l_prefix@/var/orchestrator/run/orchestrator.pid"
    orchestrator_logfile="@l_prefix@/var/orchestrator/log/orchestrator.log"

%status -u @l_susr@ -o
    orchestrator_usable="no"
    orchestrator_active="no"
    [ -f $orchestrator_pidfile ] && \
        kill -0 `cat $orchestrator_pidfile` && \
        orchestrator_active="yes"
    echo "orchestrator_enable=\"$orchestrator_enable\""
    echo "orchestrator_usable=\"$orchestrator_usable\""
    echo "orchestrator_active=\"$orchestrator_active\""

%start -u @l_rusr@
    rcService orchestrator enable yes || exit 0
    rcService orchestrator active yes && exit 0
    (   cd @l_prefix@/lib/orchestrator
        nohup @l_prefix@/sbin/orchestrator $orchestrator_flags \
            </dev/null >>$orchestrator_logfile 2>&1 &
        echo $! >$orchestrator_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService orchestrator enable yes || exit 0
    rcService orchestrator active no  && exit 0
    [ -f $orchestrator_pidfile ] && \
        kill -TERM `cat $orchestrator_pidfile`
    sleep 2

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

%daily -u @l_susr@
    rcService orchestrator enable yes || exit 0
    shtool rotate -f \
        -n $orchestrator_log_rotsteps -s $orchestrator_log_rotminsize -d \
        -z $orchestrator_log_rotcomplevel -o @l_rusr@ -g @l_rgrp@ -m 644 \
        -P "$orchestrator_log_rotprolog" \
        -E "$orchestrator_log_rotepilog; rc orchestrator restart" \
        $orchestrator_logfile

