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

%config
    visitors_enable="$openpkg_rc_def"
    visitors_daemon="no"
    visitors_daemon_concurrency="100"
    visitors_daemon_logfile="@l_prefix@/var/apache/run/access.log"
    visitors_daemon_outfile="@l_prefix@/var/visitors/visitors.html"
    visitors_daemon_update="600"
    visitors_daemon_flags="--all"

%common
    visitors_pidfile="@l_prefix@/var/visitors/visitors.pid"
    visitors_signal () {
        [ -f $visitors_pidfile ] && kill -$1 `cat $visitors_pidfile`
    }

%status -u @l_rusr@ -o
    visitors_usable="unknown"
    visitors_active="no"
    rcService visitors enable yes && \
        visitors_signal 0 && visitors_active="yes"
    echo "visitors_enable=\"$visitors_enable\""
    echo "visitors_usable=\"$visitors_usable\""
    echo "visitors_active=\"$visitors_active\""

%start -p 500 -u @l_rusr@
    rcService visitors enable yes || exit 0
    rcService visitors active yes && exit 0
    rcVarIsYes visitors_daemon || exit 0
    (   (   (   @l_prefix@/bin/adnslogres \
                    -p -c $visitors_daemon_concurrency \
                    $visitors_daemon_logfile; \
                tail -n 0 -f $visitors_daemon_logfile |\
                @l_prefix@/bin/adnsresfilter
            ) | @l_prefix@/bin/visitors \
                --stream --update-every $visitors_daemon_update \
                --output-file $visitors_daemon_outfile \
                $visitors_daemon_flags -- -
        ) </dev/null >/dev/null 2>&1 &
        echo $! >$visitors_pidfile
    ) </dev/null >/dev/null 2>&1

%stop -p 500 -u @l_rusr@
    rcService visitors enable yes || exit 0
    rcService visitors active no  && exit 0
    rcVarIsYes visitors_daemon || exit 0
    visitors_signal TERM
    sleep 2
    rm -f $visitors_pidfile >/dev/null 2>&1 || true

%restart -p 100 -u @l_rusr@
    rcService visitors enable yes || exit 0
    rcService visitors active no  && exit 0
    rcVarIsYes visitors_daemon || exit 0
    rc visitors stop start

