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

%config
    tyk_enable="$openpkg_rc_def"
    tyk_flags=""
    tyk_log_prolog="true"
    tyk_log_epilog="true"
    tyk_log_numfiles="10"
    tyk_log_minsize="1M"
    tyk_log_complevel="9"

%common
    tyk_pidfile="@l_prefix@/var/tyk/run/tyk.pid"
    tyk_logfile="@l_prefix@/var/tyk/log/tyk.log"
    tyk_signal () {
        [ -f $tyk_pidfile ] && kill -$1 `cat $tyk_pidfile`
    }

%status -u @l_rusr@ -o
    tyk_usable="unknown"
    tyk_active="no"
    rcService tyk enable yes && \
        tyk_signal 0 && tyk_active="yes"
    echo "tyk_enable=\"$tyk_enable\""
    echo "tyk_usable=\"$tyk_usable\""
    echo "tyk_active=\"$tyk_active\""

%start -u @l_rusr@
    rcService tyk enable yes || exit 0
    rcService tyk active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/tyk $tyk_flags \
        </dev/null >>$tyk_logfile 2>&1 &
        echo $! >$tyk_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService tyk enable yes || exit 0
    shtool rotate -f \
        -n ${tyk_log_numfiles} -s ${tyk_log_minsize} -d \
        -z ${tyk_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tyk_log_prolog}" \
        -E "${tyk_log_epilog}; rc tyk restart" \
        $tyk_logfile

