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

%config
    couchdb_enable="$openpkg_rc_def"
    couchdb_flags=""
    couchdb_log_prolog="true"
    couchdb_log_epilog="true"
    couchdb_log_numfiles="10"
    couchdb_log_minsize="1M"
    couchdb_log_complevel="9"

%common
    couchdb_logfile="@l_prefix@/var/couchdb/log/couchdb.log"
    couchdb_running () {
        if [ ".`@l_prefix@/bin/couchdb -s | grep 'Apache CouchDB is running'`" != . ]; then
            return 0
        else
            return 1
        fi
    }

%status -u @l_susr@ -o
    couchdb_usable="unknown"
    couchdb_active="no"
    rcService couchdb enable yes && \
        couchdb_running && couchdb_active="yes"
    echo "couchdb_enable=\"$couchdb_enable\""
    echo "couchdb_usable=\"$couchdb_usable\""
    echo "couchdb_active=\"$couchdb_active\""

%start -u @l_rusr@
    rcService couchdb enable yes || exit 0
    rcService couchdb active yes && exit 0
    @l_prefix@/bin/couchdb -b -o $couchdb_logfile -e $couchdb_logfile $couchdb_flags

%stop -u @l_susr@
    rcService couchdb enable yes || exit 0
    rcService couchdb active no  && exit 0
    @l_prefix@/bin/couchdb -d

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

%daily -u @l_susr@
    rcService couchdb enable yes || exit 0
    shtool rotate -f \
        -n $couchdb_log_numfiles -s $couchdb_log_minsize -d \
        -z $couchdb_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$couchdb_log_prolog" \
        -E "$couchdb_log_epilog; rc couchdb restart" \
        $couchdb_logfile

