#!/bin/sh

# start janus

. /etc/sysconfig/rc.conf
. /bin/network-functions
. /etc/image_features

if [ -e "/tmp/featurestatus.conf" ] ; then
	. /tmp/featurestatus.conf
fi

LOGGER="/bin/logger -t $0"

PIDFILE=/var/run/janus.pid
SERVICE=janus
PROCESS_NAME=janus
SETSID=/usr/bin/setsid
KILLALL=/usr/bin/killall

do_start ()
{
    if [ "$START_WEBRTCGW" = "true" ]; then
	ServiceCheckRunning1
	
	$SETSID /opt/janus/bin/run_janus.sh > /dev/null 2>&1 &
	ServiceCheckRunning2
	return $?
    else
	return 1
    fi
}

do_stop ()
{
	ServiceStop
	if [ -f $PIDFILE ]; then
	    pid=$(cat $PIDFILE)
	    if [ -n "$pid" ]; then
			kill -9 $pid
	    fi
	else
	    $KILLALL -9 janus
	fi
	return $?
}

do_status ()
{
	ServiceStatus
	return $?
}

case "$1" in
	start)
		do_start
		;;
	stop)
		do_stop
		;;
	status) 
		do_status
		;;
	restart)
		do_stop
		sleep 1
		do_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|status}"
		exit 1
esac

exit $?
