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

%config
    chili_enable="$openpkg_rc_def"
    chili_host="localhost"
    chili_port=3000
    chili_svcs=1
    chili_log_prolog="true"
    chili_log_epilog="true"
    chili_log_numfiles="10"
    chili_log_minsize="1M"
    chili_log_complevel="9"
    chili_logfile="@l_prefix@/lib/chiliproj/log/production.log"
    chili_thinpath="@l_prefix@/var/chiliproj"

%common
    chili_pidpath="@l_prefix@/var/chiliproj"
    chili_signal () {
        for thepid in ${chili_pidpath}/thind.[0-9][0-9]*.pid; do
            [ -f $thepid ] && kill -$1 `cat $thepid`
            proclive=$?
            if [ $proclive = 0 ]; then break; fi
        done
        [ $proclive = 0 ]
    }

%status -u @l_susr@ -o
    chili_usable="unknown"
    chili_active="no"
    rcService chiliproj enable yes && \
        chili_signal 0 && chili_active="yes"
    echo "chili_enable=\"$chili_enable\""
    echo "chili_usable=\"$chili_usable\""
    echo "chili_active=\"$chili_active\""

%start -u @l_susr@
    rcService chiliproj enable yes || exit 0
    rcService chiliproj active yes && exit 0
#--ssl
#--ssl-verify
#--ssl-key-file <path>
#--ssl-cert-file <path>
    cd @l_chilipath@ && \
        @l_gempath@/bin/bundle exec thin \
        -d \
        -e production \
        -u @l_nusr@ \
        -g @l_mgrp@ \
        -a $chili_host \
        -p $chili_port \
        -s $chili_svcs \
        -P $chili_pidpath/thind.pid \
        -l $chili_thinpath/thind.log \
        start

%stop -u @l_susr@
    rcService chiliproj enable yes || exit 0
    rcService chiliproj active no  && exit 0
    cd @l_chilipath@ && \
        @l_gempath@/bin/bundle exec thin \
        -P ${chili_pidpath}/thind.pid stop

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

%daily -u @l_susr@
    rcService chiliproj enable yes || exit 0
    rcTmp -i
    hintfile=`rcTmp -f -n hint`
    shtool rotate -f \
        -n ${chili_log_numfiles} -s ${chili_log_minsize} -d \
        -z ${chili_log_complevel} -m 664 -o @l_nusr@ -g @l_mgrp@ \
        -P "${chili_log_prolog}" \
        -E "${chili_log_epilog}; echo 1 >$hintfile" \
        ${chili_logfile}
    shtool rotate -f \
        -n ${chili_thin_numfiles} -s ${chili_thin_minsize} -d \
        -z ${chili_thin_complevel} -m 664 -o @l_nusr@ -g @l_mgrp@ \
        -P "${chili_thin_prolog}" \
        -E "${chili_thin_epilog}; echo 1 >$hintfile" \
        ${chili_thinpath}/thind.*.log
    if [ -s $hintfile ]; then
        rc chiliproj restart
    fi
    rcTmp -k

