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

%config
    corenlp_enable="$openpkg_rc_def"
    corenlp_flags="-port 9000"
    corenlp_log_prolog="true"
    corenlp_log_epilog="true"
    corenlp_log_numfiles="10"
    corenlp_log_minsize="1M"
    corenlp_log_complevel="9"

%common
    corenlp_pidfile="@l_prefix@/var/corenlp/run/corenlp.pid"
    corenlp_logfile="@l_prefix@/var/corenlp/log/corenlp.log"
    corenlp_signal () {
        [ -f $corenlp_pidfile ] && kill -$1 `cat $corenlp_pidfile`
    }

%status -u @l_nusr@ -o
    corenlp_usable="unknown"
    corenlp_active="no"
    rcService corenlp enable yes && \
        corenlp_signal 0 && corenlp_active="yes"
    echo "corenlp_enable=\"$corenlp_enable\""
    echo "corenlp_usable=\"$corenlp_usable\""
    echo "corenlp_active=\"$corenlp_active\""

%start -u @l_nusr@
    rcService corenlp enable yes || exit 0
    rcService corenlp active yes && exit 0
    eval `JAVA_PLATFORM="sun-jdk" @l_prefix@/bin/java-toolkit -e`
    (   @l_prefix@/bin/java \
            -mx5g -server \
            -cp "@l_prefix@/lib/corenlp/*" \
            -Djava.io.tmpdir="@l_prefix@/var/corenlp/run" \
            edu.stanford.nlp.pipeline.StanfordCoreNLPServer \
            $corenlp_flags \
            </dev/null >>$corenlp_logfile 2>&1 &
        echo $! >$corenlp_pidfile
    ) >/dev/null 2>&1

%stop -u @l_nusr@
    rcService corenlp enable yes || exit 0
    rcService corenlp active no  && exit 0
    corenlp_signal TERM
    sleep 2
    rm -f $corenlp_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService corenlp enable yes || exit 0
    shtool rotate -f \
        -n ${corenlp_log_numfiles} -s ${corenlp_log_minsize} -d \
        -z ${corenlp_log_complevel} -m 664 -o @l_nusr@ -g @l_ngrp@ \
        -P "${corenlp_log_prolog}" \
        -E "${corenlp_log_epilog}; rc corenlp restart" \
        $corenlp_logfile

