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

%config
    teleport_enable="$openpkg_rc_def"
    teleport_flags=""
    teleport_log_prolog="true"
    teleport_log_epilog="true"
    teleport_log_numfiles="10"
    teleport_log_minsize="1M"
    teleport_log_complevel="9"

%common
    teleport_crtfile="@l_prefix@/etc/teleport/ca.crt"
    teleport_cfgfile="@l_prefix@/etc/teleport/teleport.yaml"
    teleport_pidfile="@l_prefix@/var/teleport/run/teleport.pid"
    teleport_logfile="@l_prefix@/var/teleport/log/teleport.log"
    teleport_signal () {
        [ -f $teleport_pidfile ] && kill -$1 `cat $teleport_pidfile`
    }

%status -u @l_rusr@ -o
    teleport_usable="unknown"
    teleport_active="no"
    rcService teleport enable yes && \
        teleport_signal 0 && teleport_active="yes"
    echo "teleport_enable=\"$teleport_enable\""
    echo "teleport_usable=\"$teleport_usable\""
    echo "teleport_active=\"$teleport_active\""

%start -u @l_susr@
    rcService teleport enable yes || exit 0
    rcService teleport active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        SSL_CERT_FILE="$teleport_crtfile"
        export SSL_CERT_FILE
        nohup @l_prefix@/bin/teleport \
           start --config "$teleport_cfgfile" \
           $teleport_flags \
        </dev/null >>$teleport_logfile 2>&1 &
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService teleport enable yes || exit 0
    rcService teleport active no  && exit 0
    teleport_signal TERM
    sleep 2
    rm -f $teleport_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService teleport enable yes || exit 0
    shtool rotate -f \
        -n ${teleport_log_numfiles} -s ${teleport_log_minsize} -d \
        -z ${teleport_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${teleport_log_prolog}" \
        -E "${teleport_log_epilog}; rc teleport restart" \
        $teleport_logfile

