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

%config
    trac_enable="$openpkg_rc_def"
    trac_host="127.0.0.1"
    trac_port="8080"
    trac_envdir="@l_prefix@/var/trac/env"
    trac_basepath=""
    trac_log_prolog="true"
    trac_log_epilog="true"
    trac_log_numfiles="10"
    trac_log_minsize="1M"
    trac_log_complevel="9"

%common
    trac_pidfile="@l_prefix@/var/trac/run/trac.pid"
    trac_logfile="@l_prefix@/var/trac/log/trac.log"
    trac_signal () {
        [ -f $trac_pidfile ] && kill -$1 `cat $trac_pidfile`
    }

%status -u @l_susr@ -o
    trac_usable="no"
    trac_active="no"
    rcService trac enable yes && \
        trac_signal 0 && trac_active="yes"
    echo "trac_enable=\"$trac_enable\""
    echo "trac_usable=\"$trac_usable\""
    echo "trac_active=\"$trac_active\""

%start -u @l_susr@
    rcService trac enable yes || exit 0
    rcService trac active yes && exit 0
    cmd="PYTHON_EGG_CACHE=@l_prefix@/var/trac/cache; export PYTHON_EGG_CACHE;"
    cmd="$cmd @l_prefix@/bin/tracd"
    cmd="$cmd --daemonize"
    cmd="$cmd --hostname='$trac_host'"
    cmd="$cmd --port='$trac_port'"
    cmd="$cmd --pidfile='$trac_pidfile'"
    cmd="$cmd --env-parent-dir='$trac_envdir'"
    cmd="$cmd --base-path='$trac_basepath'"
    for env in $trac_envdir/*; do
        if [ -f $env/.htpasswd ]; then
            name=`echo $env | sed -e 's;^.*/;;'`
            cmd="$cmd --basic-auth='$name,$env/.htpasswd,$name'"
        elif [ -f $env/.htdigest ]; then
            name=`echo $env | sed -e 's;^.*/;;'`
            cmd="$cmd --auth='$name,$env/.htdigest,$name'"
        fi
    done
    su - @l_rusr@ -c "$cmd" >>$trac_logfile

%stop -u @l_susr@
    rcService trac enable yes || exit 0
    rcService trac active no  && exit 0
    trac_signal TERM
    sleep 1
    trac_signal USR1
    sleep 1
    trac_signal KILL
    sleep 2
    rm -f $trac_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService trac enable yes || exit 0
    shtool rotate -f \
        -n ${trac_log_numfiles} -s ${trac_log_minsize} -d \
        -z ${trac_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${trac_log_prolog}" \
        -E "${trac_log_epilog}; rc trac restart" \
        $trac_logfile

