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

%config
    fourstore_enable="$openpkg_rc_def"
    fourstore_bind_addr="127.0.0.1"
    fourstore_bind_port="8081"
    fourstore_log_prolog="true"
    fourstore_log_epilog="true"
    fourstore_log_numfiles="10"
    fourstore_log_minsize="1M"
    fourstore_log_complevel="9"

%common
    fourstore_pidfile_backend="@l_prefix@/var/fourstore/run/fourstore.backend.pid"
    fourstore_pidfile_httpd="@l_prefix@/var/fourstore/run/fourstore.httpd.pid"
    fourstore_signal () {
        [ -f $fourstore_pidfile_backend ] && kill -$1 `cat $fourstore_pidfile_backend`
        [ -f $fourstore_pidfile_httpd ] && kill -$1 `cat $fourstore_pidfile_httpd`
    }

%status -u @l_rusr@ -o
    fourstore_usable="unknown"
    fourstore_active="no"
    rcService fourstore enable yes && \
        fourstore_signal 0 && fourstore_active="yes"
    echo "fourstore_enable=\"$fourstore_enable\""
    echo "fourstore_usable=\"$fourstore_usable\""
    echo "fourstore_active=\"$fourstore_active\""

%start -u @l_rusr@
    rcService fourstore enable yes || exit 0
    rcService fourstore active yes && exit 0
    ( nohup @l_prefix@/bin/4s-backend -D \
        default </dev/null >/dev/null 2>&1 &
      echo $! >$fourstore_pidfile_backend
    ) >/dev/null 2>&1
    ( nohup @l_prefix@/bin/4s-httpd -D \
        -H "$fourstore_bind_addr" \
        -p "$fourstore_bind_port" \
        default </dev/null >/dev/null 2>&1 &
      echo $! >$fourstore_pidfile_httpd
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService fourstore enable yes || exit 0
    rcService fourstore active no  && exit 0
    fourstore_signal TERM
    sleep 2
    rm -f $fourstore_pidfile_backend >/dev/null 2>&1 || true
    rm -f $fourstore_pidfile_httpd   >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService fourstore enable yes || exit 0
    shtool rotate -f \
        -n ${fourstore_log_numfiles} -s ${fourstore_log_minsize} -d \
        -z ${fourstore_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${fourstore_log_prolog}" \
        -E "${fourstore_log_epilog}; rc fourstore restart" \
        $fourstore_logfile

