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

%config
    postgraphile_enable="$openpkg_rc_def"
    postgraphile_flags="--host localhost --port 3000 postgres://localhost:5432/example"
    postgraphile_log_prolog="true"
    postgraphile_log_epilog="true"
    postgraphile_log_numfiles="10"
    postgraphile_log_minsize="1M"
    postgraphile_log_complevel="9"

%common
    postgraphile_pidfile="@l_prefix@/var/postgraphile/run/postgraphile.pid"
    postgraphile_logfile="@l_prefix@/var/postgraphile/log/postgraphile.log"
    postgraphile_signal () {
        [ -f $postgraphile_pidfile ] && kill -$1 `cat $postgraphile_pidfile`
    }

%status -u @l_nusr@ -o
    postgraphile_usable="unknown"
    postgraphile_active="no"
    rcService postgraphile enable yes && \
        postgraphile_signal 0 && postgraphile_active="yes"
    echo "postgraphile_enable=\"$postgraphile_enable\""
    echo "postgraphile_usable=\"$postgraphile_usable\""
    echo "postgraphile_active=\"$postgraphile_active\""

%start -u @l_nusr@
    rcService postgraphile enable yes || exit 0
    rcService postgraphile active yes && exit 0
    (   nohup @l_prefix@/bin/postgraphile $postgraphile_flags \
        </dev/null >>$postgraphile_logfile 2>&1 &
        echo $! >$postgraphile_pidfile
    ) >/dev/null 2>&1

%stop -u @l_nusr@
    rcService postgraphile enable yes || exit 0
    rcService postgraphile active no  && exit 0
    postgraphile_signal TERM
    sleep 2
    rm -f $postgraphile_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService postgraphile enable yes || exit 0
    shtool rotate -f \
        -n ${postgraphile_log_numfiles} -s ${postgraphile_log_minsize} -d \
        -z ${postgraphile_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${postgraphile_log_prolog}" \
        -E "${postgraphile_log_epilog}; rc postgraphile restart" \
        $postgraphile_logfile

