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

%config
    ndb_enable="$openpkg_rc_def"
    ndb_daemons="ndb_mgmd ndbd"
    ndb_log_prolog="true"
    ndb_log_epilog="true"
    ndb_log_numfiles="10"
    ndb_log_minsize="1M"
    ndb_log_complevel="9"

%common
    ndb_etcdir="@l_prefix@/etc/ndb"
    ndb_vardir="@l_prefix@/var/ndb"
    ndb_signal () {
        local retcode=0
        local daemons=
        for daemon in $ndb_daemons; do
            [ -f $ndb_vardir/run/$daemon.pid ] \
            && kill -$1 `cat $ndb_vardir/run/$daemon.pid`
            retcode=$(($retcode+$?))
        done
        return $retcode
    }

%status -u @l_susr@ -o
    ndb_usable="unknown"
    ndb_active="no"
    rcService ndb enable yes && \
        ndb_signal 0 && ndb_active="yes"
    echo "ndb_enable=\"$ndb_enable\""
    echo "ndb_usable=\"$ndb_usable\""
    echo "ndb_active=\"$ndb_active\""

%start -u @l_rusr@
    rcService ndb enable yes || exit 0
    rcService ndb active yes && exit 0
    for daemon in $ndb_daemons; do
        cmd=""
        case $daemon in
            ndb_mgmd )
                cmd="@l_prefix@/sbin/$daemon --config-file=$ndb_etcdir/$daemon.ini --nodaemon"
                ;;
            ndbd )
                cmd="@l_prefix@/sbin/$daemon --defaults-file=$ndb_etcdir/$daemon.ini --nodaemon"
                ;;
        esac
        ( nohup $cmd </dev/null >$ndb_vardir/log/$daemon.log 2>&1 &
          echo $! >$ndb_vardir/run/$daemon.pid
        ) >/dev/null 2>&1
    done

%stop -u @l_susr@
    rcService ndb enable yes || exit 0
    rcService ndb active no  && exit 0
    ndb_signal TERM
    sleep 2
    for daemon in $ndb_daemons; do
        rm -f $ndb_vardir/run/$daemon.pid
    done

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

%daily -u @l_susr@
    rcService ndb enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    for daemon in $ndb_daemons; do
        if [ -f $ndb_vardir/log/$daemon.log ]; then
            shtool rotate -f \
                -n $ndb_log_numfiles -s $ndb_log_minsize -d \
                -z $ndb_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
                -P "$ndb_log_prolog" \
                -E "$ndb_log_epilog; echo 1 >$hintfile" \
                $ndb_vardir/log/$daemon.log
        fi
    done
    if [ -s $hintfile ]; then
        rc ndb restart
    fi
    rcTmp -k

