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

%config
    pgweb_enable="$openpkg_rc_def"
    pgweb_flags="--url postgres://postgresql:postgresql@localhost/template1 --sessions"
    pgweb_log_prolog="true"
    pgweb_log_epilog="true"
    pgweb_log_numfiles="10"
    pgweb_log_minsize="1M"
    pgweb_log_complevel="9"

%common
    pgweb_logdir="@l_prefix@/var/pgweb/log"
    pgweb_pidfile="@l_prefix@/var/pgweb/run/pgweb.pid"
    pgweb_logfile="@l_prefix@/var/pgweb/log/pgweb.log"
    pgweb_signal () {
        [ -f $pgweb_pidfile ] && kill -$1 `cat $pgweb_pidfile`
    }

%status -u @l_rusr@ -o
    pgweb_usable="unknown"
    pgweb_active="no"
    rcService pgweb enable yes && \
        pgweb_signal 0 && pgweb_active="yes"
    echo "pgweb_enable=\"$pgweb_enable\""
    echo "pgweb_usable=\"$pgweb_usable\""
    echo "pgweb_active=\"$pgweb_active\""

%start -u @l_rusr@
    rcService pgweb enable yes || exit 0
    rcService pgweb active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/pgweb $pgweb_flags \
        </dev/null >>$pgweb_logfile 2>&1 &
        echo $! >$pgweb_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService pgweb enable yes || exit 0
    shtool rotate -f \
        -n ${pgweb_log_numfiles} -s ${pgweb_log_minsize} -d \
        -z ${pgweb_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${pgweb_log_prolog}" \
        -E "${pgweb_log_epilog}; rc pgweb restart" \
        $pgweb_logfile

