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

%config
    mongodb_enable="$openpkg_rc_def"
    mongodb_flags=""
    mongodb_log_prolog="true"
    mongodb_log_epilog="true"
    mongodb_log_numfiles="10"
    mongodb_log_minsize="1M"
    mongodb_log_complevel="9"

%common
    mongodb_cfgfile="@l_prefix@/etc/mongodb/mongodb.conf"
    mongodb_pidfile="@l_prefix@/var/mongodb/run/mongodb.pid"
    mongodb_logfile="@l_prefix@/var/mongodb/log/mongodb.log"
    mongodb_signal () {
        [ -f $mongodb_pidfile ] && kill -$1 `cat $mongodb_pidfile`
    }

%status -u @l_rusr@ -o
    mongodb_usable="unknown"
    mongodb_active="no"
    rcService mongodb enable yes && \
        mongodb_signal 0 && mongodb_active="yes"
    echo "mongodb_enable=\"$mongodb_enable\""
    echo "mongodb_usable=\"$mongodb_usable\""
    echo "mongodb_active=\"$mongodb_active\""

%start -u @l_rusr@
    rcService mongodb enable yes || exit 0
    rcService mongodb active yes && exit 0
    @l_prefix@/bin/mongod --config "$mongodb_cfgfile" ${mongodb_flags}

%stop -u @l_rusr@
    rcService mongodb enable yes || exit 0
    rcService mongodb active no  && exit 0
    mongodb_signal INT
    sleep 2
    mongodb_signal TERM
    sleep 1
    rm -f $mongodb_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_rusr@
    rcService mongodb enable yes || exit 0
    shtool rotate -f \
        -n ${mongodb_log_numfiles} -s ${mongodb_log_minsize} -d \
        -z ${mongodb_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${mongodb_log_prolog}" \
        -E "${mongodb_log_epilog}; rc mongodb restart" \
        $mongodb_logfile

