#!@l_prefix@/bin/openpkg rc
##
##  rc.drone-runner-exec -- Run-Commands
##

%config
    drone_runner_exec_enable="$openpkg_rc_def"
    drone_runner_exec_flags=""
    drone_runner_exec_log_prolog="true"
    drone_runner_exec_log_epilog="true"
    drone_runner_exec_log_numfiles="10"
    drone_runner_exec_log_minsize="1M"
    drone_runner_exec_log_complevel="9"

%common
    drone_runner_exec_cfgfile="@l_prefix@/etc/drone-runner-exec/drone-runner-exec.sh"
    drone_runner_exec_pidfile="@l_prefix@/var/drone-runner-exec/run/drone-runner-exec.pid"
    drone_runner_exec_tmpfile="@l_prefix@/var/drone-runner-exec/run/drone-runner-exec.tmp"
    drone_runner_exec_logfile="@l_prefix@/var/drone-runner-exec/log/drone-runner-exec.log"
    drone_runner_exec_signal () {
        [ -f $drone_runner_exec_pidfile ] && kill -$1 `cat $drone_runner_exec_pidfile`
    }

%status -u @l_rusr@ -o
    drone_runner_exec_usable="unknown"
    drone_runner_exec_active="no"
    rcService drone_runner_exec enable yes && \
        drone_runner_exec_signal 0 && drone_runner_exec_active="yes"
    echo "drone_runner_exec_enable=\"$drone_runner_exec_enable\""
    echo "drone_runner_exec_usable=\"$drone_runner_exec_usable\""
    echo "drone_runner_exec_active=\"$drone_runner_exec_active\""

%start -u @l_rusr@
    rcService drone-runner-exec enable yes || exit 0
    rcService drone-runner-exec active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        sed -e 's/^\([A-Z][^=]*\)=\(.*\)$/\1=\2; export \1/' \
            <@l_prefix@/etc/drone-runner-exec/drone-runner-exec.sh >$drone_runner_exec_tmpfile
        . $drone_runner_exec_tmpfile
        rm -f $drone_runner_exec_tmpfile
        nohup @l_prefix@/sbin/drone-runner-exec \
            $drone_runner_exec_flags \
        </dev/null >>$drone_runner_exec_logfile 2>&1 &
        echo $! >$drone_runner_exec_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService drone-runner-exec enable yes || exit 0
    rcService drone-runner-exec active no  && exit 0
    drone_runner_exec_signal TERM
    sleep 2
    rm -f $drone_runner_exec_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService drone-runner-exec enable yes || exit 0
    rcService drone-runner-exec active no  && exit 0
    rc drone_runner_exec stop start

%daily -u @l_susr@
    rcService drone-runner-exec enable yes || exit 0
    shtool rotate -f \
        -n ${drone_runner_exec_log_numfiles} -s ${drone_runner_exec_log_minsize} -d \
        -z ${drone_runner_exec_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${drone_runner_exec_log_prolog}" \
        -E "${drone_runner_exec_log_epilog}; rc drone-runner-exec restart" \
        $drone_runner_exec_logfile

