#!/bin/bash
#
# Startup script for rtpproxy
#
# chkconfig: - 84 15
# description: A symmetric RTP proxy
#
# processname: rtpproxy

### BEGIN INIT INFO
# Provides: rtpproxy
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $network $syslog
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Short-Description: A symmetric RTP proxy
# Description: A symmetric RTP proxy
### END INIT INFO


prog=rtpproxy
rtpproxy=/usr/sbin/$prog

lockfile=/var/lock/subsys/rtpproxy
pidfile=/var/run/$prog.pid

OPTIONS=
BASE_PORT=7722
OPTIONS_BRIDGE=()

# User-defined options and/or overrides of the variables,
# listed above, should go there:
[ -e /etc/sysconfig/rtpproxy ] && . /etc/sysconfig/rtpproxy

RETVAL=0

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

# Reset status of this service
rc_reset

start() {
	echo -n $"Starting $prog (multiple mode): "

	# check whether rtpproxy was already started
	if pidofproc -p $pidfile > /dev/null 2>&1 ; then
		echo -n "already running" && warning && echo
		return 0
	fi

	echo
	for rtpbridge in ${OPTIONS_BRIDGE[*]} 
	do
		echo -n "    rtpproxy at udp:127.0.0.1:$BASE_PORT bridge $rtpbridge: "
		startproc $rtpproxy -p $pidfile-$BASE_PORT -u daemon -s udp:127.0.0.1:$BASE_PORT $rtpbridge $OPTIONS
		RETVAL=$?
		echo
		[ $RETVAL != 0 ] && return $RETVAL
#		sleep 0.5
		rm -f $pidfile-$BASE_PORT
		let BASE_PORT++
	done
	pidof $prog > $pidfile
	touch $lockfile
	# Remember status and be verbose
	rc_status -v
}

stop() {
	echo -n $"Stopping $prog: "

	killproc -TERM $prog

	# Remember status and be verbose
	rc_status -v
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status -p $pidfile $prog
		rc_status -v
		;;
	restart|force-reload)
		stop
		start
		;;
	reload)
		echo -n $"Reloading $prog: not supported" && failure && echo
		rc_status
		;;
	condrestart|try-restart)
		if [ -f $pidfile ] ; then
			stop
			start
		fi
		rc_status
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|condrestart|status|help}"
		exit 1
esac

exit $RETVAL
