#!/bin/sh

# PROVIDE: jitsi-videobridge
# REQUIRE: NETWORKING SERVERS
# KEYWORD: shutdown
#
# Configuration settings for jitsi-videobridge in /etc/rc.conf:
#
# jitsi_videobridge_enable:	run jitsi_videobridge server (default=NO)
# jitsi_videobridge_maxmem:	maximum memory used (default=3072m)
# jitsi_videobridge_flags:	additional flags for jitsi_videobridge server (default="")
#

. /etc/rc.subr

name="jitsi_videobridge"
rcvar=${name}_enable

jitsi_videobridge_config="%%ETCDIR%%/jitsi-videobridge.conf"
jitsi_videobridge_jar="%%JAVAJARDIR%%/jitsi-videobridge.jar"
jitsi_videobridge_logging_config="%%DATADIR%%/lib/logging.properties"
jitsi_videobridge_user="jvb"

load_rc_config "${name}"

: ${jitsi_videobridge_enable="NO"}
: ${jitsi_videobridge_maxmem="3072m"}
: ${jitsi_videobridge_flags=""}

command="%%JAVA%%"

pidfile="/var/run/jitsi-videobridge.pid"
required_files="${jitsi_videobridge_config}"
start_cmd=${name}_start
stop_cmd=${name}_stop
status_cmd=${name}_status
restart_cmd=${name}_restart

jitsi_videobridge_start()
{
	daemon -u ${jitsi_videobridge_user} -p ${pidfile} -o /var/log/jitsi-videobridge.log \
		${command} -Xmx${jitsi_videobridge_maxmem} \
		-XX:+UseConcMarkSweepGC \
		-XX:+HeapDumpOnOutOfMemoryError \
		-XX:HeapDumpPath=/tmp \
		-Djava.util.logging.config.file=${jitsi_videobridge_logging_config} \
		-Dnet.java.sip.communicator.SC_HOME_DIR_LOCATION=%%LOCALBASE%%/etc/jitsi \
		-Dnet.java.sip.communicator.SC_HOME_DIR_NAME=videobridge \
		-Dnet.java.sip.communicator.SC_LOG_DIR_LOCATION=/var/log/ \
		-Dconfig.file=${jitsi_videobridge_config} \
		-cp ${jitsi_videobridge_jar} \
		org.jitsi.videobridge.MainKt \
		${jitsi_videobridge_flags}

		echo "Starting ${name}"
}

jitsi_videobridge_stop()
{
	if [ -f ${pidfile} ]
	then
		kill `cat ${pidfile}`
		rm ${pidfile}
		echo "Stopping ${name}"
	fi
}

jitsi_videobridge_status()
{
	# If running, show pid
	if [ -f ${pidfile} ]
	then
		echo "${name} is running as pid" `cat ${pidfile}`
	else
		echo "${name} is not running"
	fi
}

jitsi_videobridge_restart()
{
	echo "Performing restart ${name}"
	jitsi_videobridge_stop
	sleep 5
	jitsi_videobridge_start
}

run_rc_command "$1"
