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

%config
    chiliproj_enable="$openpkg_rc_def"
    chiliproj_cleartext"="yes"
    chiliproj_tlsecured"="no"
    #chiliproj_flags="-u @l_nusr@ -g @l_musr@"
    chiliproj_flags=""
    chiliproj_host="localhost"
    chiliproj_port_clt=80
    chiliproj_port_tls=443
    chiliproj_svcs=1
    chiliproj_key="@l_prefix@/etc/openssl/host-ca-rsa-4096-key.pem"
    chiliproj_crt="@l_prefix@/etc/openssl/host-ca-rsa-4096-crt.pem"
    chiliproj_log_prolog="true"
    chiliproj_log_epilog="true"
    chiliproj_log_numfiles="10"
    chiliproj_log_minsize="1M"
    chiliproj_log_complevel="9"
    chiliproj_logfile="@l_prefix@/lib/chiliproj/log/production.log"
    chiliproj_thinpath="@l_prefix@/var/chiliproj"

%common
    chiliproj_pidpath="@l_prefix@/var/chiliproj"
    chiliproj_signal () {
        for thepid in ${chiliproj_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
    chiliproj_usable="unknown"
    chiliproj_active="no"
    rcService chiliproj enable yes && \
        chiliproj_signal 0 && chiliproj_active="yes"
    echo "chiliproj_enable=\"$chiliproj_enable\""
    echo "chiliproj_usable=\"$chiliproj_usable\""
    echo "chiliproj_active=\"$chiliproj_active\""

%start -u @l_susr@
    rcService chiliproj enable yes || exit 0
    rcService chiliproj active yes && exit 0
    if [ "$chiliproj_cleartext" = "yes" ]; then
        cd @l_chilipath@ && \
            @l_gempath@/bin/bundle exec thin \
                -d \
                -e production \
                -a $chiliproj_host \
                -p $chiliproj_port_clt \
                -s $chiliproj_svcs \
                -P $chiliproj_pidpath/thind.pid \
                -l $chiliproj_thinpath/thind.log \
                $chiliproj_flags \
                start
    fi
    if [ "$chiliproj_tlsecured" = "yes" ]; then
        cd @l_prefix@/lib/chiliproj && \
            @l_gempath@/bin/bundle exec thin \
                -d \
                -e production \
                -a $chiliproj_host \
                -p $chiliproj_port_tls \
                -s $chiliproj_svcs \
                -P $chiliproj_pidpath/thind.pid \
                -l $chiliproj_thinpath/thind.log \
                --ssl \
                --ssl-key-file $chiliproj_key \
                --ssl-cert-file $chiliproj_crt \
                $chiliproj_flags \
                start
    fi

%stop -u @l_susr@
    rcService chiliproj enable yes || exit 0
    rcService chiliproj active no  && exit 0
    cd @l_chilipath@
    for thepid in ${chiliproj_pidpath}/thind.[0-9][0-9]*.pid; do
        cd @l_prefix@/lib/chiliproj
        @l_gempath@/bin/bundle exec thin -P $thepid stop
    done

%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 ${chiliproj_log_numfiles} -s ${chiliproj_log_minsize} -d \
        -z ${chiliproj_log_complevel} -m 664 -o @l_nusr@ -g @l_mgrp@ \
        -P "${chiliproj_log_prolog}" \
        -E "${chiliproj_log_epilog}; echo 1 >$hintfile" \
        ${chiliproj_logfile}
    shtool rotate -f \
        -n ${chiliproj_thin_numfiles} -s ${chiliproj_thin_minsize} -d \
        -z ${chiliproj_thin_complevel} -m 664 -o @l_nusr@ -g @l_mgrp@ \
        -P "${chiliproj_thin_prolog}" \
        -E "${chiliproj_thin_epilog}; echo 1 >$hintfile" \
        ${chiliproj_thinpath}/thind.*.log
    if [ -s $hintfile ]; then
        rc chiliproj restart
    fi
    rcTmp -k

