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

%config
    h2_enable="$openpkg_rc_def"
    h2_server_flags="-tcp -pg -web"
    h2_server_tcp_port="9092"
    h2_server_pg_port="5435"
    h2_server_web_port="8082"
    h2_log_prolog="true"
    h2_log_epilog="true"
    h2_log_numfiles="10"
    h2_log_minsize="1M"
    h2_log_complevel="9"

%common
    h2_pidfile="@l_prefix@/var/h2/run/h2.pid"
    h2_logfile="@l_prefix@/var/h2/log/h2.log"
    h2_dbdir="@l_prefix@/var/h2/db"
    h2_signal () {
        [ -f $h2_pidfile ] && kill -$1 `cat $h2_pidfile`
    }

%status -u @l_rusr@ -o
    h2_usable="unknown"
    h2_active="no"
    rcService h2 enable yes && \
        h2_signal 0 && h2_active="yes"
    echo "h2_enable=\"$h2_enable\""
    echo "h2_usable=\"$h2_usable\""
    echo "h2_active=\"$h2_active\""

%start -u @l_rusr@
    rcService h2 enable yes || exit 0
    rcService h2 active yes && exit 0
    ( nohup @l_prefix@/bin/h2 Server \
        -tcpPort "$h2_server_tcp_port" \
        -pgPort "$h2_server_pg_port" \
        -webPort "$h2_server_web_port" \
        -baseDir "$h2_dbdir" \
        -ifExists \
        $h2_flags \
        </dev/null >>$h2_logfile 2>&1 &
      echo $! >$h2_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService h2 enable yes || exit 0
    shtool rotate -f \
        -n ${h2_log_numfiles} -s ${h2_log_minsize} -d \
        -z ${h2_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${h2_log_prolog}" \
        -E "${h2_log_epilog}; rc h2 restart" \
        $h2_logfile

