#!/bin/bash
#
# Startup script for OpenSIPS
#
# chkconfig: - 85 15
# description: OpenSIPS is a fast SIP Server.
#
# processname: opensips
# pidfile: /var/run/opensips.pid
# config: /etc/opensips/opensips.cfg
#
### BEGIN INIT INFO
# Provides: opensips
# Required-Start: $local_fs $network $named
# Should-Start: mysqld postgresql
# Short-Description: start, stop OpenSIPS
# Description: OpenSIPS is a very fast and flexible SIP (RFC3261) server.
### END INIT INFO

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

prog=opensips
oser=/usr/sbin/$prog
pidfile="/var/run/$prog.pid"
lockfile="/var/lock/subsys/$prog"
configfile="/etc/$prog/$prog.cfg"
OPTIONS=""
RETVAL=0

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

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

	# check whether OpenSIPs 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 OpenSIPs 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|reload)
		stop
		start
		;;
	condrestart|try-restart)
		if [ -f $pidfile ] ; then
			stop
			start
		fi
		;;
	*)
		echo $"Usage: $prog {start|stop|reload|restart|condrestart|status|help}"
		RETVAL=2
esac

exit $RETVAL
