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

%config
    apacheds_enable="$openpkg_rc_def"
    apacheds_flags=""
    apacheds_flags_jvm=""
    apacheds_log_prolog="true"
    apacheds_log_epilog="true"
    apacheds_log_numfiles="10"
    apacheds_log_minsize="1M"
    apacheds_log_complevel="9"

%common
    apacheds_pidfile="@l_prefix@/var/apacheds/run/apacheds.pid"
    apacheds_logfile="@l_prefix@/var/apacheds/log/apacheds.console.log"
    apacheds_signal () {
        [ -f $apacheds_pidfile ] && kill -$1 `cat $apacheds_pidfile`
    }

%status -u @l_rusr@ -o
    apacheds_usable="unknown"
    apacheds_active="no"
    rcService apacheds enable yes && \
        apacheds_signal 0 && apacheds_active="yes"
    echo "apacheds_enable=\"$apacheds_enable\""
    echo "apacheds_usable=\"$apacheds_usable\""
    echo "apacheds_active=\"$apacheds_active\""

%start -u @l_rusr@
    rcService apacheds enable yes || exit 0
    rcService apacheds active yes && exit 0
    cmd="@l_prefix@/bin/java"
    cmd="$cmd $apacheds_flags_jvm"
    cmd="$cmd -server"
    cmd="$cmd -Dlog4j.debug"
    cmd="$cmd -Dlog4j.configuration=file:@l_prefix@/etc/apacheds/log4j.properties"
    cp=""
    for jar in @l_prefix@/libexec/apacheds/*.jar; do
        if [ ".$jar" = ".@l_prefix@/libexec/apacheds/apacheds-tools.jar" ]; then continue; fi
        if [ ".$cp" != . ]; then cp="$cp:"; fi
        cp="$cp$jar"
    done
    cmd="$cmd -cp $cp"
    cmd="$cmd org.apache.directory.server.UberjarMain"
    cmd="$cmd $apacheds_flags"
    cmd="$cmd @l_prefix@/etc/apacheds/server.xml"
    ( eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
      cd @l_prefix@/var/apacheds/run
      nohup $cmd </dev/null >>$apacheds_logfile 2>&1 &
      echo $! >$apacheds_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService apacheds enable yes || exit 0
    shtool rotate -f \
        -n $apacheds_log_numfiles -s $apacheds_log_minsize -d \
        -z $apacheds_log_complevel -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "$apacheds_log_prolog" \
        -E "$apacheds_log_epilog; rc apacheds restart" \
        $apacheds_logfile

