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

%config
    weinre_enable="$openpkg_rc_def"
    weinre_bind_addr="127.0.0.1"
    weinre_bind_port="8888"
    weinre_flags=""
    weinre_log_prolog="true"
    weinre_log_epilog="true"
    weinre_log_numfiles="10"
    weinre_log_minsize="1M"
    weinre_log_complevel="9"

%common
    weinre_pidfile="@l_prefix@/var/weinre/weinre.pid"
    weinre_logfile="@l_prefix@/var/weinre/weinre.log"
    weinre_signal () {
        [ -f $weinre_pidfile ] && kill -$1 `cat $weinre_pidfile`
    }

%status -u @l_susr@ -o
    weinre_usable="unknown"
    weinre_active="no"
    rcService weinre enable yes && \
        weinre_signal 0 && weinre_active="yes"
    echo "weinre_enable=\"$weinre_enable\""
    echo "weinre_usable=\"$weinre_usable\""
    echo "weinre_active=\"$weinre_active\""

%start -u @l_susr@
    rcService weinre enable yes || exit 0
    rcService weinre active yes && exit 0
    (   nohup @l_prefix@/bin/weinre \
            --boundHost "${weinre_bind_addr}" \
            --httpPort "${weinre_bind_port}" \
            --verbose \
            ${weinre_flags} \
            </dev/null >>$weinre_logfile 2>&1 &
        echo $! >$weinre_pidfile
    ) >/dev/null 2>&1

%stop -u @l_susr@
    rcService weinre enable yes || exit 0
    rcService weinre active no  && exit 0
    weinre_signal TERM
    sleep 2
    rm -f $weinre_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService weinre enable yes || exit 0
    shtool rotate -f \
        -n ${weinre_log_numfiles} -s ${weinre_log_minsize} -d \
        -z ${weinre_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${weinre_log_prolog}" \
        -E "${weinre_log_epilog}; rc weinre restart" \
        $weinre_logfile

