#!/bin/bash
#
# Startup script for Kamailio
#
# chkconfig: 345 85 15
# description: Kamailio is a fast, reliable and flexible SIP Server.
# processname: kamailio
# pidfile: /var/run/kamailio/kazoo-kamailio.pid
# config: /etc/kazoo/kamailio/kamailio.cfg
#

RETVAL=0
LOCK_FILE=/var/lock/subsys/kazoo-kamailio

. /etc/rc.d/init.d/functions

start() {
	echo -n $"Starting Kamailio: "
	/usr/sbin/kazoo-kamailio prepare >/dev/null 2>&1
	daemon "/usr/sbin/kazoo-kamailio background >/dev/null"
	RETVAL=$?
	if [ ${RETVAL} -eq 0 ]; then
		touch ${LOCK_FILE}
		success $"OK"
		echo
	else
		failure $"Kamailio is already running or failed to start!"
		echo
	fi
}

stop() {
	echo -n $"Stopping Kamailio: "
	/usr/sbin/kazoo-kamailio stop >/dev/null 2>&1
	RETVAL=$?
		if [ ${RETVAL} -eq 0 ]; then
			rm -f ${LOCK_FILE} ${pidfile}
			success $"OK"
			echo
		else
			failure $"Kamailio is still running!"
			echo
		fi
}

status() {
	/usr/sbin/kazoo-kamailio status
	RETVAL=$?
}

restart() {
	stop
	start
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	check)
		/usr/sbin/kazoo-kamailio check
		;;
	restart|reload)
		restart
			;;
	condrestart)
		[ ! -e ${LOCK_FILE} ] && restart
		;;
	*)
		echo $"Usage: $0 (start|stop|restart|status|check)"
		RETVAL=1
esac

exit ${RETVAL}
