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

%config
    traefik_enable="$openpkg_rc_def"
    traefik_flags=""
    traefik_log_prolog="true"
    traefik_log_epilog="true"
    traefik_log_numfiles="10"
    traefik_log_minsize="1M"
    traefik_log_complevel="9"

%common
    traefik_cfgfile="@l_prefix@/etc/traefik/traefik.toml"
    traefik_pidfile="@l_prefix@/var/traefik/run/traefik.pid"
    traefik_logfile="@l_prefix@/var/traefik/log/traefik.log"
    traefik_signal () {
        [ -f $traefik_pidfile ] && kill -$1 `cat $traefik_pidfile`
    }

%status -u @l_susr@ -o
    traefik_usable="unknown"
    traefik_active="no"
    rcService traefik enable yes && \
        traefik_signal 0 && traefik_active="yes"
    echo "traefik_enable=\"$traefik_enable\""
    echo "traefik_usable=\"$traefik_usable\""
    echo "traefik_active=\"$traefik_active\""

%start -u @l_susr@
    rcService traefik enable yes || exit 0
    rcService traefik active yes && exit 0
    (   nohup @l_prefix@/sbin/traefik \
           -c "$traefik_cfgfile" \
           $traefik_flags \
        </dev/null >>$traefik_logfile 2>&1 &
        echo $! >$traefik_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService traefik enable yes || exit 0
    shtool rotate -f \
        -n ${traefik_log_numfiles} -s ${traefik_log_minsize} -d \
        -z ${traefik_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${traefik_log_prolog}" \
        -E "${traefik_log_epilog}; rc traefik restart" \
        $traefik_logfile

