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

%config
    coturn_enable="$openpkg_rc_def"
    coturn_flags="--prod"
    coturn_log_prolog="true"
    coturn_log_epilog="true"
    coturn_log_numfiles="10"
    coturn_log_minsize="1M"
    coturn_log_complevel="9"

%common
    coturn_cfgfile="@l_prefix@/etc/coturn/turnserver.conf"
    coturn_pidfile="@l_prefix@/var/coturn/run/turnserver.pid"
    coturn_logfile="@l_prefix@/var/coturn/log/turnserver.log"
    coturn_signal () {
        [ -f $coturn_pidfile ] && kill -$1 `cat $coturn_pidfile`
    }

%status -u @l_rusr@ -o
    coturn_usable="unknown"
    coturn_active="no"
    rcService coturn enable yes && \
        coturn_signal 0 && coturn_active="yes"
    echo "coturn_enable=\"$coturn_enable\""
    echo "coturn_usable=\"$coturn_usable\""
    echo "coturn_active=\"$coturn_active\""

%start -u @l_rusr@
    rcService coturn enable yes || exit 0
    rcService coturn active yes && exit 0
    @l_prefix@/bin/turnserver -c $coturn_cfgfile --daemon

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

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

%daily -u @l_susr@
    rcService coturn enable yes || exit 0
    shtool rotate -f \
        -n ${coturn_log_numfiles} -s ${coturn_log_minsize} -d \
        -z ${coturn_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${coturn_log_prolog}" \
        -E "${coturn_log_epilog}; rc coturn restart" \
        $coturn_logfile

