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

%config
    cayley_enable="$openpkg_rc_def"
    cayley_flags="-host 127.0.0.1 -port 64210"
    cayley_log_prolog="true"
    cayley_log_epilog="true"
    cayley_log_numfiles="10"
    cayley_log_minsize="1M"
    cayley_log_complevel="9"

%common
    cayley_pidfile="@l_prefix@/var/cayley/run/cayley.pid"
    cayley_logfile="@l_prefix@/var/cayley/log/cayley.log"
    cayley_signal () {
        [ -f $cayley_pidfile ] && kill -$1 `cat $cayley_pidfile`
    }

%status -u @l_rusr@ -o
    cayley_usable="unknown"
    cayley_active="no"
    rcService cayley enable yes && \
        cayley_signal 0 && cayley_active="yes"
    echo "cayley_enable=\"$cayley_enable\""
    echo "cayley_usable=\"$cayley_usable\""
    echo "cayley_active=\"$cayley_active\""

%start -u @l_rusr@
    rcService cayley enable yes || exit 0
    rcService cayley active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/cayley http \
           --db=leveldb \
           --dbpath=@l_prefix@/var/cayley/db \
           --assets=@l_prefix@/share/cayley \
           --log_dir=@l_prefix@/var/cayley/log \
           $cayley_flags \
        </dev/null >>$cayley_logfile 2>&1 &
        echo $! >$cayley_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService cayley enable yes || exit 0
    shtool rotate -f \
        -n ${cayley_log_numfiles} -s ${cayley_log_minsize} -d \
        -z ${cayley_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${cayley_log_prolog}" \
        -E "${cayley_log_epilog}; rc cayley restart" \
        $cayley_logfile

