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

%config
    keycloak_enable="$openpkg_rc_def"
    keycloak_flags=""
    keycloak_log_prolog="true"
    keycloak_log_epilog="true"
    keycloak_log_numfiles="10"
    keycloak_log_minsize="1M"
    keycloak_log_complevel="9"

%common
    keycloak_cfgfile="@l_prefix@/etc/keycloak/keycloak.yaml"
    keycloak_pidfile="@l_prefix@/var/keycloak/run/keycloak.pid"
    keycloak_logfile="@l_prefix@/var/keycloak/log/keycloak.log"
    keycloak_signal () {
        [ -f $keycloak_pidfile ] && kill -$1 `cat $keycloak_pidfile`
    }

%status -u @l_rusr@ -o
    keycloak_usable="unknown"
    keycloak_active="no"
    rcService keycloak enable yes && \
        keycloak_signal 0 && keycloak_active="yes"
    echo "keycloak_enable=\"$keycloak_enable\""
    echo "keycloak_usable=\"$keycloak_usable\""
    echo "keycloak_active=\"$keycloak_active\""

%start -u @l_rusr@
    rcService keycloak enable yes || exit 0
    rcService keycloak active yes && exit 0
    (   nohup @l_prefix@/sbin/keycloak $keycloak_flags \
            </dev/null >>$keycloak_logfile 2>&1 &
        echo $! >$keycloak_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService keycloak enable yes || exit 0
    shtool rotate -f \
        -n $keycloak_log_numfiles -s $keycloak_log_minsize -d \
        -z $keycloak_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$keycloak_log_prolog" \
        -E "$keycloak_log_epilog; rc keycloak reload" \
        $keycloak_logfile

