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

%config
    opensips_enable="$openpkg_rc_def"
    opensips_flags=""
    opensips_rtpproxy="yes"
    opensips_log_prolog="true"
    opensips_log_epilog="true"
    opensips_log_numfiles="10"
    opensips_log_minsize="1M"
    opensips_log_complevel="9"

%common
    opensips_cfgfile="@l_prefix@/etc/opensips/opensips.cfg"
    opensips_pidfile="@l_prefix@/var/opensips/opensips.pid"
    opensips_rtpproxy_pidfile="@l_prefix@/var/opensips/opensips_rtpproxy.pid"
    opensips_rtpproxy_socket="@l_prefix@/var/opensips/opensips_rtpproxy.sock"
    opensips_rtpproxy_listen=`grep '^listen=' $opensips_cfgfile | sed -e 's/^listen="\(.*\)".*/\1/'`
    opensips_rtpproxy_signal () {
        [ -f $opensips_rtpproxy_pidfile ] && kill -$1 `cat $opensips_rtpproxy_pidfile`
    }

%status -u @l_susr@ -o
    opensips_usable="no"
    opensips_active="no"
    if @l_prefix@/sbin/opensips -c >/dev/null 2>&1; then
        opensips_usable="yes"
    fi
    if @l_prefix@/sbin/opensipsctl ps >/dev/null 2>&1; then
        opensips_active="yes"
    fi
    echo "opensips_enable=\"$opensips_enable\""
    echo "opensips_usable=\"$opensips_usable\""
    echo "opensips_active=\"$opensips_active\""

%start -u @l_susr@
    rcService opensips enable yes || exit 0
    rcService opensips active yes && exit 0
    if rcVarIsYes opensips_rtpproxy; then
        cmd="@l_prefix@/sbin/opensips_rtpproxy" \
        cmd="$cmd -l $opensips_rtpproxy_listen"
        cmd="$cmd -s unix:$opensips_rtpproxy_socket"
        cmd="$cmd -p $opensips_rtpproxy_pidfile"
        su @l_rusr@ -c "$cmd"
    fi
    @l_prefix@/sbin/opensips \
        -P $opensips_pidfile \
        ${opensips_flags}

%stop -u @l_susr@
    rcService opensips enable yes || exit 0
    rcService opensips active no  && exit 0
    @l_prefix@/sbin/opensipsctl stop
    if rcVarIsYes opensips_rtpproxy; then
        opensips_rtpproxy_signal TERM
    fi
    sleep 2

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

%daily -u @l_susr@
    rcService opensips enable yes || exit 0
    shtool rotate -f \
        -n ${opensips_log_numfiles} -s ${opensips_log_minsize} -d \
        -z ${opensips_log_complevel} -m 644 -o @l_rusr@ -g @l_rgrp@ \
        -P "${opensips_log_prolog}" \
        -E "${opensips_log_epilog}; rc opensips reload" \
        @l_prefix@/var/opensips/opensips.log

