#!/bin/bash
cmd=$1;
cnt=0;
max_retry=5;
use_syslog=1;
args=' ';

NICE="nice -n -5"
BRI_NICE="nice -n -10"

while [ ! -z "$2" ]; 
do
	args=$args"$2 "
	shift
done


function stop_all()
{

	echo " "
	echo "Stopping running processes..."

	stop_sangoma_brid
	stop_sangoma_mgd
}



function stop_sangoma_brid()
{
	#stop sangoma_brid
	eval "pidof sangoma_brid >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending TERM signal to sangoma_brid..."
		eval "kill -TERM $(pidof sangoma_brid) 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
		else
			echo "FAILED"
			break;
		fi
	fi
	for ((i=0;i<$max_retry;i++))
	do
		eval "pidof sangoma_brid >/dev/null 2>/dev/null"
		if [ $? -ne 0 ]; then
			echo "sangoma_brid is stopped"
			return;
		else
			echo "waiting for sangoma_brid to finish($i/$max_retry)...."
			sleep 1
		fi
	done
	eval "pidof sangoma_brid >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending KILL signal to sangoma_brid..."
		eval "kill -KILL $(pidof sangoma_brid) 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
		else	
			echo "FAILED"
			break;
		fi
	fi
	for ((i=0;i<$max_retry;i++))
	do
		eval "pidof sangoma_brid >/dev/null 2>/dev/null"
		if [ $? -ne 0 ]; then
			echo "sangoma_brid is stopped"
			break;
		else
			echo "waiting for sangoma_brid to finish($i/$max_retry)...."
			sleep 1
		fi
	done
}

function stop_sangoma_mgd()
{
	#stop sangoma media gateway
	eval "pidof sangoma_mgd >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending TERM signal to sangoma_mgd..."
		eval "sangoma_mgd -term 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
			return;
		else 
			echo "FAILED"
			echo "Failed to stop sangoma_mgd"
			exit 1
		fi
	else 
		echo "sangoma_mgd not running..."
		return;
	fi

	
	#stop sangoma media gateway
	eval "pidof sangoma_mgd >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending TERM signal to sangoma_mgd..."
		eval "kill -KILL $(pidof sangoma_mgd) 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
		else 
			echo "FAILED"
			echo "Failed to stop sangoma_mgd"
			exit 1
		fi
	else 
		echo "sangoma_mgd not running..."
	fi


}


function start_test()
{
	echo " "
	echo "Testing configuration files..."
	if [ $use_syslog -eq 1 ]; then
		eval "$BRI_NICE sangoma_brid -t"
	else 
		eval "$BRI_NICE sangoma_brid -t -log"
	fi

	if [ $? -eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
	fi
	
}


function start_all()
{

	echo " " 
	echo "Starting processes..."
	echo -n "Loading SCTP..."
	eval "modprobe sctp  >>/var/log/sangoma_mgd.log 2>>/var/log/sangoma_mgd.log"
	if [ $?	-eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to load SCTP module, check /var/log/sangoma_mgd.log"
		exit 1;
	fi
	sleep 1

	eval "ls /dev/wptdm* >>/dev/null 2>>/dev/null"
	
	if [ $? -ne 0 ]; then
		echo "No Sangoma TDM API interfaces running"
		echo "Did you start wanrouter? "
		exit 1;
	fi

	echo -n "Starting sangoma_brid..."
	if [ $use_syslog -eq 1 ]; then
		eval "$BRI_NICE sangoma_brid  >/dev/null 2>/dev/null &" 
	else 
		eval "$BRI_NICE sangoma_brid -log >/dev/null 2>/dev/null &" 
	fi
	if [ $?	-eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to start sangoma_brid, check /var/log/sangoma_mgd.log for errors"
		exit 1;
	fi
	sleep 10
	if [ ! $(pidof sangoma_brid) ]; then
		echo "sangoma_brid failed to start"
		echo "check /var/log/sangoma_mgd.log for errors"
		exit 1;
	fi

	echo -n "Starting sangoma_mgd..."
	eval "$NICE sangoma_mgd -bg >/dev/null 2>/dev/null" 
	if [ $? -eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to start sangoma_mgd, check /var/log/sangoma_mgd.log for errors"
		exit 1;
	fi

	sleep 2	
	if [ ! $(pidof sangoma_mgd) ]; then
		echo "sangoma_mgd failed to start"
		echo "check /var/log/sangoma_mgd.log for errors"
		exit 1;
	fi

	echo "Sangoma SMG running.."
	if [ $use_syslog -eq 1 ]; then
		echo "log file: /var/log/sangoma_mgd.log"
	else
		echo "log file: /var/log/sangoma_mgd/sangoma_mgd.log"
	fi
	echo " "
}

function usage()
{
	echo " "
	echo "Usage: smg_ctrl <command> [options]"
	echo "command:"
	echo " "
	echo "start	:start sangoma_brid and sangoma media gateway"
	echo "stop 	:stop sangoma_brid and sangoma media gateway"
	echo "restart	:restart sangoma_brid and sangoma media gateway"
	echo "test	:test sangoma_brid configuration files without"
	echo "		 applying changes"
	echo " "
	echo "options:"
	echo "nosyslog  :does not use syslog to log data"
	echo " "

}

function check_running ()
{
	if [ $(pidof sangoma_brid) ]; then
		echo "sangoma_brid is currently running"
		echo "exiting..."
		exit 0
	fi

	if [ $(pidof sangoma_mgd) ]; then
		echo "sangoma_mgd is currently running"
		echo "exiting..."
		exit 0
	fi
}
function check_pid_brid()
{
	if [ ! $(pidof sangoma_brid) ]; then
		echo "sangoma_brid is not running"
		exit 1
	fi
}

function toggle_capture()
{
	check_pid_brid
	eval "kill -SIGRTMIN+2 $(pidof sangoma_brid)"
	if [ $? -eq 0 ]; then
		echo "BRI: Protocol capture toggled"
	else 
		echo "BRI: Failed to send command"
	fi
}

function increase_verbose()
{
	check_pid_brid
	SIG_VAL=`kill -l SIGRTMIN`
	echo "signal: $SIG_VAL"
	eval "kill -SIGRTMIN $(pidof sangoma_brid)"
	if [ $? -eq 0 ]; then
		echo "BRI: Verbosity increased"
	else 
		echo "BRI: Failed to send command"
	fi

}

function decrease_verbose()
{
	check_pid_brid
	eval "kill -SIGRTMIN+1 $(pidof sangoma_brid)"
	if [ $? -eq 0 ]; then
		echo "BRI: Verbosity decreased"
	else 
		echo "BRI: Failed to send command"
	fi
}

function show_calls()
{
	check_pid_brid
	eval "kill -SIGRTMIN+3 $(pidof sangoma_brid) "
	if [ $? -eq 0 ]; then
		echo "BRI: Show calls"
	else 
		echo "BRI: Failed to send command"
	fi
}

function show_spans()
{
	check_pid_brid
	eval "kill -SIGRTMIN+4 $(pidof sangoma_brid)"
	if [ $? -eq 0 ]; then
		echo "BRI: Show spans"
	else 
		echo "BRI: Failed to send command"
	fi
}

function parse_args()
{
	for arg in $args
	do
		case $arg in 
			nosyslog)
				use_syslog=0;
				;;
		esac

	done	
}

parse_args


if [ "$cmd" = "start" ]; then
	check_running
	start_all
elif [ "$cmd" = "stop" ]; then
	stop_all
elif [ "$cmd" = "restart" ]; then
	stop_all
	start_all
elif [ "$cmd" = "capture" ]; then
	toggle_capture	
elif [ "$cmd" = "v+" ]; then
	increase_verbose
elif [ "$cmd" = "v-" ]; then
	decrease_verbose
elif [ "$cmd" = "show_spans" ]; then
	show_spans
elif [ "$cmd" = "show_calls" ]; then
	show_calls
elif [ "$cmd" = "test" ]; then
	start_test
else 
	usage
fi


