#!/bin/bash
#
# Init file for Netborder Call Analyzer Engine on OpenSuSE
#
# description: Sangoma NetBorder Call Analyzer
#
# pidfile: /var/run/netborder-call-analyzer-service.pid
# pidfile: /var/run/netborder-call-analyzer-engine.pid
#
### BEGIN INIT INFO
# Provides:	  netborder-engine
# Required-Start:    $syslog $network netborder-service 
# Should-Start:      
# Required-Stop:     
# Should-Stop:       
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Starts the netborder call analyzer engine
# Description:       Starts the netborder call analyzer engine
### END INIT INFO

# source function library
. /lib/lsb/init-functions

INSTDIR=/opt/Sangoma_NetBorderCallAnalyzer

#add our /lib to .so search path
export LD_LIBRARY_PATH=$INSTDIR/lib:$LD_LIBRARY_PATH

NCA_COMMON_OPTIONS="--start-service --putenv PARAXIP=$INSTDIR --putenv PARAXIP_BINDIR=$INSTDIR/bin --putenv PARAXIP_BRAND=netborder --putenv PARAXIP_INSTALLDIR=$INSTDIR --putenv PARAXIP_PLATFORM=x86_64-linux"

# engine process
NCA_ENGINE_NAME=netborder-call-analyzer-engine
NCA_ENGINE_FULL_PATH=$INSTDIR/bin/$NCA_ENGINE_NAME

NCA_ENGINE_OPTIONS="$NCA_COMMON_OPTIONS --global-config $INSTDIR/config/call-analyzer-engine-logging.properties --global-config $INSTDIR/config/call-analyzer-engine.properties"

# Sets process priority to 'Realtime' (Highest priority available)
# use as prefix before lauching the application
PRIO_RT="/usr/bin/chrt -r 99"

case "$1" in

	start)
		NCA_ENGINE_CMD="$PRIO_RT $NCA_ENGINE_FULL_PATH $NCA_ENGINE_OPTIONS"
		cd $INSTDIR
		echo -n "Starting $NCA_ENGINE_NAME"
		echo $NCA_ENGINE_CMD >> $INSTDIR/logs/stderr/$NCA_ENGINE_NAME.log
		$NCA_ENGINE_CMD >> $INSTDIR/logs/stderr/$NCA_ENGINE_NAME.log
		rc_status -v
		;;

	stop)
		echo -n "Stopping $NCA_ENGINE_NAME"
		/sbin/killproc -TERM $NCA_ENGINE_FULL_PATH
		rc_status -v
		;;

	status)
		echo -n "Checking for service $NCA_ENGINE_NAME"
		/sbin/checkproc $NCA_ENGINE_FULL_PATH
		rc_status -v
		;;

	restart)
		$0 stop
		sleep 2
		$0 start
		;;

	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 1

esac
