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

%config
    meilisearch_enable="$openpkg_rc_def"
    meilisearch_flags=""
    meilisearch_log_prolog="true"
    meilisearch_log_epilog="true"
    meilisearch_log_numfiles="10"
    meilisearch_log_minsize="1M"
    meilisearch_log_complevel="9"

%common
    meilisearch_cfgfile="@l_prefix@/etc/meilisearch/meilisearch.conf"
    meilisearch_pidfile="@l_prefix@/var/meilisearch/run/meilisearch.pid"
    meilisearch_logfile="@l_prefix@/var/meilisearch/log/meilisearch.log"
    meilisearch_signal () {
        [ -f $meilisearch_pidfile ] && kill -$1 `cat $meilisearch_pidfile`
    }

%status -u @l_rusr@ -o
    meilisearch_usable="unknown"
    meilisearch_active="no"
    rcService meilisearch enable yes && \
        meilisearch_signal 0 && meilisearch_active="yes"
    echo "meilisearch_enable=\"$meilisearch_enable\""
    echo "meilisearch_usable=\"$meilisearch_usable\""
    echo "meilisearch_active=\"$meilisearch_active\""

%start -u @l_rusr@
    rcService meilisearch enable yes || exit 0
    rcService meilisearch active yes && exit 0
    (   nohup @l_prefix@/bin/meilisearch \
            `sed -e "/^ *#/d" -e '/^ *$/d' -e "s;^;--;" <$meilisearch_cfgfile | \
            sed -e ":a" -e "N" -e '$!ba' -e 's/\n/ /g'` \
            $meilisearch_flags \
        </dev/null >>$meilisearch_logfile 2>&1 &
        echo $! >$meilisearch_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService meilisearch enable yes || exit 0
    shtool rotate -f \
        -n ${meilisearch_log_numfiles} -s ${meilisearch_log_minsize} -d \
        -z ${meilisearch_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${meilisearch_log_prolog}" \
        -E "${meilisearch_log_epilog}; rc meilisearch restart" \
        $meilisearch_logfile

