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

%config
    caddy_enable="$openpkg_rc_def"
    caddy_flags=""
    caddy_log_prolog="true"
    caddy_log_epilog="true"
    caddy_log_numfiles="10"
    caddy_log_minsize="1M"
    caddy_log_complevel="9"

%common
    caddy_cfgfile="@l_prefix@/etc/caddy/Caddyfile"
    caddy_pidfile="@l_prefix@/var/caddy/run/caddy.pid"
    caddy_logfile="@l_prefix@/var/caddy/log/caddy.log"
    caddy_signal () {
        [ -f $caddy_pidfile ] && kill -$1 `cat $caddy_pidfile`
    }

%status -u @l_rusr@ -o
    caddy_usable="unknown"
    caddy_active="no"
    rcService caddy enable yes && \
        caddy_signal 0 && caddy_active="yes"
    echo "caddy_enable=\"$caddy_enable\""
    echo "caddy_usable=\"$caddy_usable\""
    echo "caddy_active=\"$caddy_active\""

%start -u @l_rusr@
    rcService caddy enable yes || exit 0
    rcService caddy active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/bin/caddy \
           -conf="$caddy_cfgfile" \
           -log="$caddy_logfile" \
           -pidfile="$caddy_pidfile" \
           $caddy_flags \
        </dev/null >>$caddy_logfile 2>&1 &
        echo $! >$caddy_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService caddy enable yes || exit 0
    shtool rotate -f \
        -n ${caddy_log_numfiles} -s ${caddy_log_minsize} -d \
        -z ${caddy_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${caddy_log_prolog}" \
        -E "${caddy_log_epilog}; rc caddy restart" \
        $caddy_logfile

