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

%config
    surrealdb_enable="$openpkg_rc_def"
    surrealdb_flags=""
    surrealdb_log_prolog="true"
    surrealdb_log_epilog="true"
    surrealdb_log_numfiles="10"
    surrealdb_log_minsize="1M"
    surrealdb_log_complevel="9"

%common
    surrealdb_cfgfile="@l_prefix@/etc/surrealdb/surrealdb.conf"
    surrealdb_pidfile="@l_prefix@/var/surrealdb/run/surrealdb.pid"
    surrealdb_logfile="@l_prefix@/var/surrealdb/log/surrealdb.log"
    surrealdb_signal () {
        [ -f $surrealdb_pidfile ] && kill -$1 `cat $surrealdb_pidfile`
    }

%status -u @l_rusr@ -o
    surrealdb_usable="unknown"
    surrealdb_active="no"
    rcService surrealdb enable yes && \
        surrealdb_signal 0 && surrealdb_active="yes"
    echo "surrealdb_enable=\"$surrealdb_enable\""
    echo "surrealdb_usable=\"$surrealdb_usable\""
    echo "surrealdb_active=\"$surrealdb_active\""

%start -u @l_rusr@
    rcService surrealdb enable yes || exit 0
    rcService surrealdb active yes && exit 0
    (   eval "`sed -e '/^ *#/d' -e '/^ *$/d' -e 's;^;export ;' <$surrealdb_cfgfile`"
        nohup @l_prefix@/bin/surreal start $surrealdb_flags \
        </dev/null >>$surrealdb_logfile 2>&1 &
        echo $! >$surrealdb_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService surrealdb enable yes || exit 0
    shtool rotate -f \
        -n ${surrealdb_log_numfiles} -s ${surrealdb_log_minsize} -d \
        -z ${surrealdb_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${surrealdb_log_prolog}" \
        -E "${surrealdb_log_epilog}; rc surrealdb restart" \
        $surrealdb_logfile

