#! /bin/sh
#
# freeswitch	Start/Stop the freeswitch daemon.
#
# chkconfig:	345 90 10
# description:	freeswitch -- an telephony platform
# processname:	freeswitch
# config: 	/etc/freeswitch
# pidfile: 	/var/run/freeswitch/freeswitch.pid

WITHOUT_RC_COMPAT=1

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

SourceIfNotEmpty /etc/sysconfig/freeswitch

PIDFILE=/var/run/freeswitch/freeswitch.pid
LOCKFILE=/var/lock/subsys/freeswitch
RETVAL=0

start()
{
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _pbx -- freeswitch -u _pbx -nc $FSOPTIONS
    RETVAL=$?
    return $RETVAL
}

stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user _pbx freeswitch
    RETVAL=$?
    return $RETVAL
}

restart()
{
    stop
    start
}

reload()
{
    action "reloading freeswitch configuration:" fs_cli -x reloadxml
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    reload)
    reload
    ;;
    restart)
    restart
    ;;
    condstop)
    if [ -e "$LOCKFILE" ]; then
	stop
    fi
    ;;
    condrestart)
    if [ -e "$LOCKFILE" ]; then
	restart
    fi
    ;;
    condreload)
    if [ -e "$LOCKFILE" ]; then
	reload
    fi
    ;;
    status)
    status --pidfile "$PIDFILE" --expect-user _pbx freeswitch
    RETVAL=$?
    ;;
    *)
    msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
    RETVAL=1
esac

exit $RETVAL
