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

%config
    solr_enable="$openpkg_rc_def"
    solr_admin_port="8079"
    solr_log_prolog="true"
    solr_log_epilog="true"
    solr_log_numfiles="10"
    solr_log_minsize="1M"
    solr_log_complevel="9"

%common
    solr_logfile="@l_prefix@/var/solr/log/solr.log"
    solr_keyfile="@l_prefix@/var/solr/run/solr.key"
    solr_cmd () {
        if [ ! -f $solr_keyfile ]; then
            @l_prefix@/bin/apg -Mncl -a1 -m12 -n1 >$solr_keyfile
        fi
        cmd="cd @l_prefix@/lib/solr/server &&"
        cmd="$cmd nohup @l_prefix@/bin/java"
        cmd="$cmd -Dsolr.solr.home=@l_prefix@/etc/solr"
        cmd="$cmd -Dsolr.data.dir=@l_prefix@/var/solr/dat"
        cmd="$cmd -Djavax.servlet.context.tempdir=@l_prefix@/var/solr/tmp"
        cmd="$cmd -Djava.io.tmpdir=@l_prefix@/var/solr/tmp"
        cmd="$cmd -DSTOP.PORT=$solr_admin_port -DSTOP.KEY=\"`cat $solr_keyfile`\""
        cmd="$cmd -jar start.jar"
        eval "$cmd $@ </dev/null >>$solr_logfile 2>&1 &"
    }

%status -u @l_rusr@ -o
    solr_usable="unknown"
    solr_active="no"
    if rcService solr enable yes; then
        @l_prefix@/bin/curl -q -s -m 2 http://127.0.0.1:$solr_admin_port </dev/null >/dev/null 2>&1
        if [ $? -eq 52 ]; then
            solr_active="yes"
        fi
    fi
    echo "solr_enable=\"$solr_enable\""
    echo "solr_usable=\"$solr_usable\""
    echo "solr_active=\"$solr_active\""

%start -u @l_rusr@
    rcService solr enable yes || exit 0
    rcService solr active yes && exit 0
    solr_cmd

%stop -u @l_rusr@
    rcService solr enable yes || exit 0
    rcService solr active no  && exit 0
    solr_cmd --stop
    sleep 2
    rm -f $solr_keyfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService solr enable yes || exit 0
    shtool rotate -f \
        -n ${solr_log_numfiles} -s ${solr_log_minsize} -d \
        -z ${solr_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${solr_log_prolog}" \
        -E "${solr_log_epilog}; rc solr restart" \
        $solr_logfile

