#!/bin/sh
# MacOSX 10.3
# mediaproxy   starts and stops the SER MediaProxy server

. /etc/rc.common

PIDS_DIR="/var/run"
INSTALL_DIR="/usr/local"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
PROXY=$INSTALL_DIR/mediaproxy/mediaproxy.py
DISPATCHER=$INSTALL_DIR/mediaproxy/proxydispatcher.py
NAME=mediaproxy
DESC="SERMediaProxy server"
PROXY_PID="$PIDS_DIR/mediaproxy.pid"
DISPATCHER_PID="$PIDS_DIR/proxydispatcher.pid"

# Options for mediaproxy and dispatcher. Do not include --pid <pidfile>
# --pid <pidfile> will be added automatically if needed.
PROXY_OPTIONS=""
DISPATCHER_OPTIONS=""

if [ "$PROXY_PID" != "/var/run/mediaproxy.pid" ]; then                          
    PROXY_OPTIONS="--pid $PROXY_PID $PROXY_OPTIONS"                             
fi                                                                              
if [ "$DISPATCHER_PID" != "/var/run/proxydispatcher.pid" ]; then                
    DISPATCHER_OPTIONS="--pid $DISPATCHER_PID $DISPATCHER_OPTIONS"              
fi                                                                              
                                                                                
StartService ()
{
    if [ "${MEDIAPROXY:=-NO-}" = "-YES-" ]; then
        echo -n "Starting MediaProxy server: $NAME"
        $PROXY $PROXY_OPTIONS
        $DISPATCHER $DISPATCHER_OPTIONS
        echo "."
    fi
}

StopService ()
{
    echo -n "Stopping MediaProxy server: $NAME"
    kill `cat $PROXY_PID`
    kill `cat $DISPATCHER_PID`
    echo "."
}

RestartService ()
{
    echo "Restarting $DESC:"
    StopService
    sleep 3
    StartService
}

RunService "$1"
