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

%config
    cfssl_enable="$openpkg_rc_def"
    cfssl_listen_addr="127.0.0.1"
    cfssl_listen_port="8888"
    cfssl_loglevel="1"
    cfssl_log_prolog="true"
    cfssl_log_epilog="true"
    cfssl_log_numfiles="10"
    cfssl_log_minsize="1M"
    cfssl_log_complevel="9"

%common
    cfssl_datdir="@l_prefix@/var/cfssl/dat"
    cfssl_pidfile="@l_prefix@/var/cfssl/run/cfssl.pid"
    cfssl_logfile="@l_prefix@/var/cfssl/log/cfssl.log"
    cfssl_signal () {
        [ -f $cfssl_pidfile ] && kill -$1 `cat $cfssl_pidfile`
    }

%status -u @l_rusr@ -o
    cfssl_usable="unknown"
    cfssl_active="no"
    rcService cfssl enable yes && \
        cfssl_signal 0 && cfssl_active="yes"
    echo "cfssl_enable=\"$cfssl_enable\""
    echo "cfssl_usable=\"$cfssl_usable\""
    echo "cfssl_active=\"$cfssl_active\""

%start -u @l_rusr@
    rcService cfssl enable yes || exit 0
    rcService cfssl active yes && exit 0
    (   cd "$cfssl_datdir"
        nohup @l_prefix@/bin/cfssl \
           -loglevel "$cfssl_loglevel" \
           serve \
           -address "$cfssl_listen_addr" \
           -port "$cfssl_listen_port" \
           -ca "ca.crt" \
           -ca-key "ca.key" \
           -ca-bundle "ca-bundle.crt" \
           -int-bundle "int-bundle.crt" \
           -int-dir "int.d" \
        </dev/null >>$cfssl_logfile 2>&1 &
        echo $! >$cfssl_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService cfssl enable yes || exit 0
    shtool rotate -f \
        -n ${cfssl_log_numfiles} -s ${cfssl_log_minsize} -d \
        -z ${cfssl_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${cfssl_log_prolog}" \
        -E "${cfssl_log_epilog}; rc cfssl restart" \
        $cfssl_logfile

