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

%config
    matomo_enable="$openpkg_rc_def"
    matomo_cron="yes"
    matomo_log_rotsteps="10"
    matomo_log_rotminsize="10M"
    matomo_log_rotcomplevel="9"
    matomo_log_rotprolog="true"
    matomo_log_rotepilog="true"

%common
    matomo_apache_cfgfile="@l_prefix@/etc/matomo/matomo-apache.conf"
    matomo_apache_pidfile="@l_prefix@/var/matomo/run/matomo-apache.pid"
    matomo_apache_logfile="@l_prefix@/var/matomo/log/matomo-access.log"

%status -u @l_susr@ -o
    matomo_usable="no"
    matomo_active="no"
    @l_prefix@/sbin/apache -t -f $matomo_apache_cfgfile 2>/dev/null && \
        matomo_usable="yes"
    [ -f $matomo_apache_pidfile ] && \
        kill -0 `cat $matomo_apache_pidfile` && \
        matomo_active="yes"
    echo "matomo_enable=\"$matomo_enable\""
    echo "matomo_usable=\"$matomo_usable\""
    echo "matomo_active=\"$matomo_active\""

%start -u @l_susr@
    rcService matomo enable yes || exit 0
    rcService matomo active yes && exit 0
    @l_prefix@/sbin/apache -f $matomo_apache_cfgfile

%stop -u @l_susr@
    rcService matomo enable yes || exit 0
    rcService matomo active no  && exit 0
    [ -f $matomo_apache_pidfile ] && \
        kill -TERM `cat $matomo_apache_pidfile`
    sleep 2

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

%reload -u @l_susr@
    rcService matomo enable yes || exit 0
    rcService matomo active no  && exit 0
    @l_prefix@/sbin/apache -f $matomo_apache_cfgfile -k graceful

%quarterly -u @l_rusr@
    rcService matomo enable yes || exit 0
    rcService matomo active no  && exit 0
    if [ ".$matomo_cron" = .yes ]; then
        @l_prefix@/sbin/matomo-cron
    fi

%daily -u @l_susr@
    rcService matomo enable yes || exit 0
    shtool rotate -f \
        -n $matomo_log_rotsteps -s $matomo_log_rotminsize -d \
        -z $matomo_log_rotcomplevel -o @l_susr@ -g @l_mgrp@ -m 644 \
        -P "$matomo_log_rotprolog" \
        -E "$matomo_log_rotepilog; rc matomo reload" \
        $matomo_apache_logfile

