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

%config
    mediaproxy_enable="$openpkg_rc_def"
    mediaproxy_dispatcher="yes"

%common
    mediaproxy_pidfile="@l_prefix@/var/mediaproxy/mediaproxy.pid"
    proxydispatcher_pidfile="@l_prefix@/var/mediaproxy/proxydispatcher.pid"
    mediaproxy_signal () {
        [ -f $mediaproxy_pidfile ] && kill -$1 `cat $mediaproxy_pidfile`
    }
    proxydispatcher_signal () {
        [ -f $proxydispatcher_pidfile ] && kill -$1 `cat $proxydispatcher_pidfile`
    }

%status -u @l_susr@ -o
    mediaproxy_usable="unknown"
    mediaproxy_active="no"
    rcService mediaproxy enable yes && \
        mediaproxy_signal 0 && mediaproxy_active="yes"
    echo "mediaproxy_enable=\"$mediaproxy_enable\""
    echo "mediaproxy_usable=\"$mediaproxy_usable\""
    echo "mediaproxy_active=\"$mediaproxy_active\""

%start -u @l_susr@
    rcService mediaproxy enable yes || exit 0
    rcService mediaproxy active yes && exit 0
    if rcVarIsYes mediaproxy_dispatcher; then
        @l_prefix@/bin/python @l_prefix@/sbin/proxydispatcher
    fi
    @l_prefix@/bin/python @l_prefix@/sbin/mediaproxy

%stop -u @l_susr@
    rcService mediaproxy enable yes || exit 0
    rcService mediaproxy active no  && exit 0
    mediaproxy_signal TERM
    if rcVarIsYes mediaproxy_dispatcher; then
        proxydispatcher_signal TERM
    fi
    sleep 2

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

