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

%config
    zerotier_enable="$openpkg_rc_def"
    zerotier_flags=""
    zerotier_log_prolog="true"
    zerotier_log_epilog="true"
    zerotier_log_numfiles="10"
    zerotier_log_minsize="1M"
    zerotier_log_complevel="9"

%common
    zerotier_pidfile="@l_prefix@/var/zerotier/run/zerotier.pid"
    zerotier_logfile="@l_prefix@/var/zerotier/log/zerotier.log"
    zerotier_signal () {
        [ -f $zerotier_pidfile ] && kill -$1 `cat $zerotier_pidfile`
    }

%status -u @l_susr@ -o
    zerotier_usable="unknown"
    zerotier_active="no"
    rcService zerotier enable yes && \
        zerotier_signal 0 && zerotier_active="yes"
    echo "zerotier_enable=\"$zerotier_enable\""
    echo "zerotier_usable=\"$zerotier_usable\""
    echo "zerotier_active=\"$zerotier_active\""

%start -u @l_susr@
    rcService zerotier enable yes || exit 0
    rcService zerotier active yes && exit 0
    (   eval "nohup @l_prefix@/sbin/zerotier-one ${zerotier_flags} </dev/null >>$zerotier_logfile 2>&1 &"
        echo $! >$zertotier_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService zerotier enable yes || exit 0
    shtool rotate -f \
        -n ${zerotier_log_numfiles} -s ${zerotier_log_minsize} -d \
        -z ${zerotier_log_complevel} -m 664 -o @l_susr@ -g @l_mgrp@ \
        -P "${zerotier_log_prolog}" \
        -E "${zerotier_log_epilog}; rc zerotier restart" \
        $zerotier_logfile

