#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          freeswitch
# Required-Start:    $remote_fs
# Should-Start:      $time ypbind sendmail mysql postgresql
# Required-Stop:     $syslog $remote_fs
# Should-Stop: $time ypbind sendmail
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: FREESWITCH XYZ daemon providing ZYX
# Description:       Start FREESWITCH the Open Source
#	Voice Platform
### END INIT INFO
#
FREESWITCH_BIN=/usr/bin/freeswitch

# Check for existence of needed config file and read it
FREESWITCH_CONFIG=/etc/sysconfig/freeswitch
test -r $FREESWITCH_CONFIG || { echo "$FREESWITCH_CONFIG not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config
. $FREESWITCH_CONFIG
FREESWITCH_USER=${FS_USER-freeswitch}
FREESWITCH_HOME=${FS_HOME-/var/run/freeswitch}
FREESWITCH_ARGS="-nc"

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
test -x $FREESWITCH_BIN || { echo "$FREESWITCH_BIN not installed";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	echo -n "Starting FreeSWITCH "
	# always re-create var/run as it is on tmpfs
	mkdir -p /var/run/freeswitch && chown $FREESWITCH_USER /var/run/freeswitch
	/sbin/startproc -u $FREESWITCH_USER $FREESWITCH_BIN $FREESWITCH_ARGS $FREESWITCH_PARAMS
	rc_status -v
	;;
    stop)
	echo -n "Shutting down FreeSWITCH "
	/sbin/killproc -TERM $FREESWITCH_BIN
	rc_status -v
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    reload)
	echo -n "Reload service FreeSWITCH "
	/sbin/killproc -HUP $FREESWITCH_BIN
	rc_status -v
	;;
    status)
	echo -n "Checking for service FreeSWITCH "
	/sbin/checkproc $FREESWITCH_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac
rc_exit
