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

%config
    elasticsearch_enable="$openpkg_rc_def"
    elasticsearch_flags="-server"
    elasticsearch_minmem="512m"
    elasticsearch_maxmem="512m"
    elasticsearch_log_prolog="true"
    elasticsearch_log_epilog="true"
    elasticsearch_log_numfiles="10"
    elasticsearch_log_minsize="1M"
    elasticsearch_log_complevel="9"

%common
    elasticsearch_pidfile="@l_prefix@/var/elasticsearch/run/elasticsearch.pid"
    elasticsearch_signal () {
        [ -f $elasticsearch_pidfile ] && kill -$1 `cat $elasticsearch_pidfile`
    }

%status -u @l_rusr@ -o
    elasticsearch_usable="unknown"
    elasticsearch_active="no"
    rcService elasticsearch enable yes && \
        elasticsearch_signal 0 && elasticsearch_active="yes"
    echo "elasticsearch_enable=\"$elasticsearch_enable\""
    echo "elasticsearch_usable=\"$elasticsearch_usable\""
    echo "elasticsearch_active=\"$elasticsearch_active\""

%start -u @l_rusr@
    rcService elasticsearch enable yes || exit 0
    rcService elasticsearch active yes && exit 0
    export ES_JAVA_OPTS="-Xms$elasticsearch_minmem -Xmx$elasticsearch_maxmem $elasticsearch_flags"
    @l_prefix@/bin/elasticsearch --daemonize -p $elasticsearch_pidfile

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

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

