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

%config
    tika_enable="$openpkg_rc_def"
    tika_flags="--host=127.0.0.1 --port=9998 --cors=all --log=info"
    tika_log_prolog="true"
    tika_log_epilog="true"
    tika_log_numfiles="10"
    tika_log_minsize="1M"
    tika_log_complevel="9"

%common
    tika_pidfile="@l_prefix@/var/tika/run/tika.pid"
    tika_logfile="@l_prefix@/var/tika/log/tika.log"
    tika_signal () {
        [ -f $tika_pidfile ] && kill -$1 `cat $tika_pidfile`
    }

%status -u @l_nusr@ -o
    tika_usable="unknown"
    tika_active="no"
    rcService tika enable yes && \
        tika_signal 0 && tika_active="yes"
    echo "tika_enable=\"$tika_enable\""
    echo "tika_usable=\"$tika_usable\""
    echo "tika_active=\"$tika_active\""

%start -u @l_nusr@
    rcService tika enable yes || exit 0
    rcService tika active yes && exit 0
    (   nohup @l_prefix@/sbin/tika-server $tika_flags \
        </dev/null >>$tika_logfile 2>&1 &
        echo $! >$tika_pidfile
    ) >/dev/null 2>&1

%stop -u @l_nusr@
    rcService tika enable yes || exit 0
    rcService tika active no  && exit 0
    tika_signal TERM
    sleep 2
    rm -f $tika_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_nusr@
    rcService tika enable yes || exit 0
    shtool rotate -f \
        -n ${tika_log_numfiles} -s ${tika_log_minsize} -d \
        -z ${tika_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${tika_log_prolog}" \
        -E "${tika_log_epilog}; rc tika restart" \
        $tika_logfile

