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

%config
    etcd_enable="$openpkg_rc_def"
    etcd_daemon="yes"
    etcd_log_prolog="true"
    etcd_log_epilog="true"
    etcd_log_numfiles="10"
    etcd_log_minsize="1M"
    etcd_log_complevel="9"

%common
    etcd_cfgfile="@l_prefix@/etc/etcd/etcd.conf"
    etcd_pidfile="@l_prefix@/var/etcd/run/etcd.pid"
    etcd_logfile="@l_prefix@/var/etcd/log/etcd.log"
    etcd_signal () {
        [ -f $etcd_pidfile ] && kill -$1 `cat $etcd_pidfile`
    }

%status -u @l_rusr@ -o
    etcd_usable="unknown"
    etcd_active="no"
    rcService etcd enable yes && \
        etcd_signal 0 && etcd_active="yes"
    echo "etcd_enable=\"$etcd_enable\""
    echo "etcd_usable=\"$etcd_usable\""
    echo "etcd_active=\"$etcd_active\""

%start -u @l_rusr@
    rcService etcd enable yes || exit 0
    rcService etcd active yes && exit 0
    rcVarIsYes etcd_daemon || exit 0
    (   eval "nohup @l_prefix@/sbin/etcd --config-file $etcd_cfgfile </dev/null >>$etcd_logfile 2>&1 &"
        echo $! >$etcd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService etcd enable yes || exit 0
    rcService etcd active no  && exit 0
    rcVarIsYes etcd_daemon || exit 0
    etcd_signal TERM
    sleep 2
    rm -f $etcd_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService etcd enable yes || exit 0
    rcVarIsYes etcd_daemon || exit 0
    shtool rotate -f \
        -n ${etcd_log_numfiles} -s ${etcd_log_minsize} -d \
        -z ${etcd_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${etcd_log_prolog}" \
        -E "${etcd_log_epilog}; rc etcd restart" \
        $etcd_logfile

