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

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

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

PROXY="$INSTALL_DIR/mediaproxy/mediaproxy.py"
DISPATCHER="$INSTALL_DIR/mediaproxy/proxydispatcher.py"
PROXY_PID="$RUNTIME_DIR/mediaproxy.pid"
DISPATCHER_PID="$RUNTIME_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=""

NAME="mediaproxy"
DESC="SER MediaProxy server"

test -f $PROXY      || exit 0
test -f $DISPATCHER || exit 0

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

start() {
    echo -n "Starting $DESC: $NAME"
    start-stop-daemon --start --quiet --pidfile $PROXY_PID      \
                      --exec $PROXY -- $PROXY_OPTIONS
    start-stop-daemon --start --quiet --pidfile $DISPATCHER_PID \
                      --exec $DISPATCHER -- $DISPATCHER_OPTIONS
    echo "."
}

stop () {
    echo -n "Stopping $DESC: $NAME"
    start-stop-daemon --stop --quiet --oknodo --signal 15 --pidfile $PROXY_PID
    start-stop-daemon --stop --quiet --oknodo --signal 15 --pidfile $DISPATCHER_PID
    echo "."
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|force-reload)
        stop
        #sleep 1
        start
        ;;
    *)
        echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0

