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

%config
    linkerd_enable="$openpkg_rc_def"
    linkerd_flags=""
    linkerd_log_prolog="true"
    linkerd_log_epilog="true"
    linkerd_log_numfiles="10"
    linkerd_log_minsize="1M"
    linkerd_log_complevel="9"

%common
    linkerd_cfgfile="@l_prefix@/etc/linkerd/linkerd.yaml"
    linkerd_pidfile="@l_prefix@/var/linkerd/run/linkerd.pid"
    linkerd_logfile="@l_prefix@/var/linkerd/log/linkerd.log"
    linkerd_signal () {
        [ -f $linkerd_pidfile ] && kill -$1 `cat $linkerd_pidfile`
    }

%status -u @l_rusr@ -o
    linkerd_usable="unknown"
    linkerd_active="no"
    rcService linkerd enable yes && \
        linkerd_signal 0 && linkerd_active="yes"
    echo "linkerd_enable=\"$linkerd_enable\""
    echo "linkerd_usable=\"$linkerd_usable\""
    echo "linkerd_active=\"$linkerd_active\""

%start -u @l_rusr@
    rcService linkerd enable yes || exit 0
    rcService linkerd active yes && exit 0
    (   nohup @l_prefix@/sbin/linkerd \
            $linkerd_flags \
            $linkerd_cfgfile \
        </dev/null >>$linkerd_logfile 2>&1 &
        echo $! >$linkerd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService linkerd enable yes || exit 0
    rcService linkerd active no  && exit 0
    linkerd_signal TERM
    sleep 2
    rm -f $linkerd_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService linkerd enable yes || exit 0
    shtool rotate -f \
        -n ${linkerd_log_numfiles} -s ${linkerd_log_minsize} -d \
        -z ${linkerd_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${linkerd_log_prolog}" \
        -E "${linkerd_log_epilog}; rc linkerd restart" \
        $linkerd_logfile

