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

%config
    vulcand_enable="$openpkg_rc_def"
    vulcand_svc_listen_addr="127.0.0.1"
    vulcand_svc_listen_port="8181"
    vulcand_api_listen_addr="127.0.0.1"
    vulcand_api_listen_port="8182"
    vulcand_etcd_peers="http:/127.0.0.1:4001"
    vulcand_flags=""
    vulcand_log_prolog="true"
    vulcand_log_epilog="true"
    vulcand_log_numfiles="10"
    vulcand_log_minsize="1M"
    vulcand_log_complevel="9"

%common
    vulcand_pidfile="@l_prefix@/var/vulcand/run/vulcand.pid"
    vulcand_logfile="@l_prefix@/var/vulcand/log/vulcand.log"
    vulcand_signal () {
        [ -f $vulcand_pidfile ] && kill -$1 `cat $vulcand_pidfile`
    }

%status -u @l_susr@ -o
    vulcand_usable="unknown"
    vulcand_active="no"
    rcService vulcand enable yes && \
        vulcand_signal 0 && vulcand_active="yes"
    echo "vulcand_enable=\"$vulcand_enable\""
    echo "vulcand_usable=\"$vulcand_usable\""
    echo "vulcand_active=\"$vulcand_active\""

%start -u @l_susr@
    rcService vulcand enable yes || exit 0
    rcService vulcand active yes && exit 0
    (   nohup @l_prefix@/sbin/vulcand \
           -interface="$vulcand_svc_listen_addr" \
           -port="$vulcand_svc_listen_port" \
           -apiInterface="$vulcand_api_listen_addr" \
           -apiPort="$vulcand_api_listen_port" \
           -etcd="$vulcand_etcd_peers" \
           $vulcand_flags \
        </dev/null >>$vulcand_logfile 2>&1 &
        echo $! >$vulcand_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService vulcand enable yes || exit 0
    rcService vulcand active no  && exit 0
    vulcand_signal TERM
    sleep 2
    rm -f $vulcand_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService vulcand enable yes || exit 0
    shtool rotate -f \
        -n ${vulcand_log_numfiles} -s ${vulcand_log_minsize} -d \
        -z ${vulcand_log_complevel} -m 664 -o @l_susr@ -g @l_sgrp@ \
        -P "${vulcand_log_prolog}" \
        -E "${vulcand_log_epilog}; rc vulcand restart" \
        $vulcand_logfile

