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

%config
    tile38_enable="$openpkg_rc_def"
    tile38_flags="-h 127.0.0.1 -p 9851"
    tile38_log_prolog="true"
    tile38_log_epilog="true"
    tile38_log_numfiles="10"
    tile38_log_minsize="1M"
    tile38_log_complevel="9"

%common
    tile38_datdir="@l_prefix@/var/tile38/db"
    tile38_pidfile="@l_prefix@/var/tile38/run/tile38.pid"
    tile38_logfile="@l_prefix@/var/tile38/log/tile38.log"
    tile38_signal () {
        [ -f $tile38_pidfile ] && kill -$1 `cat $tile38_pidfile`
    }

%status -u @l_rusr@ -o
    tile38_usable="unknown"
    tile38_active="no"
    rcService tile38 enable yes && \
        tile38_signal 0 && tile38_active="yes"
    echo "tile38_enable=\"$tile38_enable\""
    echo "tile38_usable=\"$tile38_usable\""
    echo "tile38_active=\"$tile38_active\""

%start -u @l_rusr@
    rcService tile38 enable yes || exit 0
    rcService tile38 active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/tile38-server \
            -d $tile38_datdir \
            $tile38_flags \
        </dev/null >>$tile38_logfile 2>&1 &
        echo $! >$tile38_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService tile38 enable yes || exit 0
    shtool rotate -f \
        -n ${tile38_log_numfiles} -s ${tile38_log_minsize} -d \
        -z ${tile38_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${tile38_log_prolog}" \
        -E "${tile38_log_epilog}; rc tile38 restart" \
        $tile38_logfile

