#!/bin/bash
#
# Startup script for OpenSER
#
# chkconfig: - 85 15
# description: OpenSER is a fast SIP Proxy.
#
# processname: openser
# pidfile: /var/run/openser.pid
# config: /etc/openser/openser.cfg

# Source function library.
. /etc/rc.d/init.d/functions

prog=openser
oser=/usr/sbin/$prog
pidfile="/var/run/$prog.pid"
lockfile="/var/lock/subsys/$prog"
configfile="/etc/$prog/$prog.cfg"

OPTIONS=""

[ -f /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

RETVAL=0

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

	# check whether OpenSER was already started
	if status $prog > /dev/null 2>&1 ; then
		echo -n "already running" && warning && echo
		return 0
	fi

	# there is something at end of this output which is needed to
	# report proper [ OK ] status in Fedora scripts
	daemon $oser -P $pidfile -f $configfile $OPTIONS 2>/dev/null | tail -1
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && touch $lockfile
	return $RETVAL
}

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

	# check whether OpenSER is running
	if ! status $prog > /dev/null 2>&1 ; then
		echo -n "not running" && warning && echo
		return 0
	fi

	killproc $prog 2> /dev/null
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f $lockfile $pidfile
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status $prog
		RETVAL=$?
		;;
	restart|force-reload)
		stop
		start
		;;
	condrestart|try-restart)
		if [ -f $pidfile ] ; then
			stop
			start
		fi
		;;
	*)
		echo $"Usage: $prog {start|stop|restart|condrestart|status}"
		RETVAL=2
esac

exit $RETVAL
