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

%config
    drone_runner_docker_enable="$openpkg_rc_def"
    drone_runner_docker_flags=""
    drone_runner_docker_log_prolog="true"
    drone_runner_docker_log_epilog="true"
    drone_runner_docker_log_numfiles="10"
    drone_runner_docker_log_minsize="1M"
    drone_runner_docker_log_complevel="9"

%common
    drone_runner_docker_cfgfile="@l_prefix@/etc/drone-runner-docker/drone-runner-docker.sh"
    drone_runner_docker_pidfile="@l_prefix@/var/drone-runner-docker/run/drone-runner-docker.pid"
    drone_runner_docker_tmpfile="@l_prefix@/var/drone-runner-docker/run/drone-runner-docker.tmp"
    drone_runner_docker_logfile="@l_prefix@/var/drone-runner-docker/log/drone-runner-docker.log"
    drone_runner_docker_signal () {
        [ -f $drone_runner_docker_pidfile ] && kill -$1 `cat $drone_runner_docker_pidfile`
    }

%status -u @l_rusr@ -o
    drone_runner_docker_usable="unknown"
    drone_runner_docker_active="no"
    rcService drone_runner_docker enable yes && \
        drone_runner_docker_signal 0 && drone_runner_docker_active="yes"
    echo "drone_runner_docker_enable=\"$drone_runner_docker_enable\""
    echo "drone_runner_docker_usable=\"$drone_runner_docker_usable\""
    echo "drone_runner_docker_active=\"$drone_runner_docker_active\""

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

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

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

%daily -u @l_susr@
    rcService drone-runner-docker enable yes || exit 0
    shtool rotate -f \
        -n ${drone_runner_docker_log_numfiles} -s ${drone_runner_docker_log_minsize} -d \
        -z ${drone_runner_docker_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${drone_runner_docker_log_prolog}" \
        -E "${drone_runner_docker_log_epilog}; rc drone-runner-docker restart" \
        $drone_runner_docker_logfile

