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

%config
    seaweedfs_enable="$openpkg_rc_def"
    seaweedfs_flags=""
    seaweedfs_log_prolog="true"
    seaweedfs_log_epilog="true"
    seaweedfs_log_numfiles="10"
    seaweedfs_log_minsize="1M"
    seaweedfs_log_complevel="9"

%common
    seaweedfs_cfgfile="@l_prefix@/etc/seaweedfs/seaweedfs.conf"
    seaweedfs_pidfile="@l_prefix@/var/seaweedfs/run/seaweedfs.pid"
    seaweedfs_logfile="@l_prefix@/var/seaweedfs/log/seaweedfs.log"
    seaweedfs_signal () {
        [ -f $seaweedfs_pidfile ] && kill -$1 `cat $seaweedfs_pidfile`
    }

%status -u @l_rusr@ -o
    seaweedfs_usable="unknown"
    seaweedfs_active="no"
    rcService seaweedfs enable yes && \
        seaweedfs_signal 0 && seaweedfs_active="yes"
    echo "seaweedfs_enable=\"$seaweedfs_enable\""
    echo "seaweedfs_usable=\"$seaweedfs_usable\""
    echo "seaweedfs_active=\"$seaweedfs_active\""

%start -u @l_rusr@
    rcService seaweedfs enable yes || exit 0
    rcService seaweedfs active yes && exit 0
    (   nohup @l_prefix@/sbin/weed server \
            `sed -e "/^ *#/d" -e '/^ *$/d' -e "s;^;-;" <$seaweedfs_cfgfile | \
            sed -e ":a" -e "N" -e '$!ba' -e 's/\n/ /g'` \
        </dev/null >>$seaweedfs_logfile 2>&1 &
        echo $! >$seaweedfs_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService seaweedfs enable yes || exit 0
    shtool rotate -f \
        -n ${seaweedfs_log_numfiles} -s ${seaweedfs_log_minsize} -d \
        -z ${seaweedfs_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${seaweedfs_log_prolog}" \
        -E "${seaweedfs_log_epilog}; rc seaweedfs restart" \
        $seaweedfs_logfile

