#!/bin/bash
cmd=$1;
sigd=$2;
cnt=0;
max_retry=10;
use_syslog=1;
use_safe=0;
args=' ';
LOG=/var/log/sangoma_mgd.log
SIG_LOG=$LOG
BRI=0
SS7=0

function usage()
{
	echo " "
	echo "Usage: smg_ctrl <command> <sigd> <options> "
	echo
	echo "command options:"
	echo " "
	echo "  start       :start $sigd and sangoma media gateway"
	echo "  stop        :stop $sigd and sangoma media gateway"
	echo "  restart     :restart $sigd and sangoma media gateway"
	echo
	echo "  BRI Specific"
	echo "  test        :test configuration only "
	echo "  capture     :enable protocol capture "
	echo "  show_spans  :show configured spans "
	echo "  show_calls  :show active calls "
	echo "  v+          :increase verbosity"
	echo "  v-          :decrease verbosity"
	echo " "
	echo "sigd options:"
	echo "  ss7boost   	:ss7 isup daemon"
	echo "  sangoma_brid	:bri daemon"
	echo
	echo "options:"
	echo "  nosyslog	:does not use syslog to log data"
	echo " "

	return 1
}

if [ $sigd = "ss7boost" ]; then
	sigd_safe_args="-i"
	sigd_bg_args=""
	SIG_LOG=/var/log/messages
	
	eval "type $sigd 2> /dev/null > /dev/null"
	if [ $? -ne 0 ]; then
		export PATH=$PATH:/usr/local/ss7box
	fi
	eval "type $sigd 2> /dev/null > /dev/null"
	if [ $? -ne 0 ]; then
		echo
		echo "Error $sigd is not found"
		echo
		exit 1
	fi

	SS7=1;
elif [ $sigd = "sangoma_brid" ]; then
	sigd_safe_args=""
	sigd_bg_args="-bg"
	BRI=1;
else
	echo
	usage
	exit 1
fi

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


function stop_all()
{

	echo " "
	echo "Stopping running processes..."
	
	stop_safe_sangoma
	stop_sigdaemon
	stop_sangoma_mgd
	remove_pid_files
}

function stop_sigdaemon()
{
	#stop $sigd
	eval "pidof $sigd >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending TERM signal to $sigd..."
		eval "kill -TERM $(pidof $sigd) 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 $sigd >/dev/null 2>/dev/null"
		if [ $? -ne 0 ]; then
			echo "$sigd is stopped"
			return;
		else
			echo "waiting for $sigd to finish($i/$max_retry)...."
			sleep 1
		fi
	done
	eval "pidof $sigd >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending KILL signal to $sigd..."
		eval "kill -KILL $(pidof $sigd) 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 $sigd >/dev/null 2>/dev/null"
		if [ $? -ne 0 ]; then
			echo "$sigd is stopped"
			break;
		else
			echo "waiting for $sigd to finish($i/$max_retry)...."
			sleep 1
		fi
	done
}

function stop_sangoma_mgd()
{
	max_retry=5
	#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"
		else
			#already stopped
			return 0;
		fi
	else 
		echo "sangoma_mgd not running..."
		return;
	fi

	for ((i=0;i<$max_retry;i++))
	do
		eval "pidof sangoma_mgd >/dev/null 2>/dev/null"
		if [ $? -ne 0 ]; then
			echo "sangoma_mgd is stopped"
			return 0
		else
			echo "waiting for sangoma_mgd to finish($i/$max_retry)...."
			sleep 1
		fi
	done
	
	#stop sangoma media gateway
	eval "pidof sangoma_mgd >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending KILL signal to sangoma_mgd..."
		eval "kill -KILL $(pidof sangoma_mgd) 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
		else 
			#already stopped
			return 0;
		fi
	else 
		echo "Ok"
	fi

	return 0
}

function stop_safe_sangoma()
{
	eval "killall safe_sangoma >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending TERM signal to safe_sangoma..."
	else 
		echo "safe_sangoma not running..."
		return;
	fi
	sleep 1

	
	eval "killall safe_sangoma >/dev/null 2>/dev/null"
	if [ $? -eq 0 ]; then
		echo -n "Sending KILL signal to safe_sangoma..."
		eval "killall -9 safe_sangoma 2>/dev/null >/dev/null"
		if [ $? -eq 0 ]; then
			echo "OK"
		else 
			echo "FAILED"
			echo "Failed to stop safe_sangoma"
			return 1
		fi
	else
		echo "Ok"
	fi
}

function remove_pid_files()
{
	echo -n "Removing PID files..."
	if [ -e /var/run/$sigd.pid ]; then
		eval "rm -f /var/run/$sigd.pid > /dev/null"
		if [ $? -ne 0 ]; then
			echo " " 
			echo "Failed to remove /var/run/$sigd.pid, try to remove it manually"
		fi
	fi
	if [ -e /var/run/sangoma_mgd.pid ]; then
		eval "rm -f /var/run/sangoma_mgd.pid > /dev/null"
		if [ $? -ne 0 ]; then
			echo " " 
			echo "Failed to remove /var/run/sangoma_mgd.pid, try to remove it manually"
		fi
	fi
	echo "done"
}

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

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


function start_all()
{
	check_running
	pre_args="";
	post_args="";
	#post_args=$post_args" >/dev/null 2>/dev/null &"
	echo " " 
	echo "Starting processes..."
	echo -n "Loading SCTP..."
	eval "modprobe sctp  >>$LOG 2>>$LOG"
	if [ $?	-eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to load SCTP module, check $LOG"
		return 1;
	fi
	sleep 1

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

	echo -n "Starting $sigd..."
	if [ $BRI -eq 1 ] && [ $use_syslog -eq 0 ]; then
		post_args=$post_args" -log"
	fi

	if [ $use_safe -eq 1 ]; then
		pre_args=" safe_sangoma"
		post_args=" $sigd_safe_args"
	else 
		post_args=" $sigd_bg_args"
	fi

 	eval "$pre_args $sigd $post_args >/dev/null 2>/dev/null &"
	if [ $?	-eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to start $sigd, check $SIG_LOG for errors"
		return 1;
	fi
	sleep 2 
	if [ ! $(pidof $sigd) ]; then
		echo "$sigd failed to start"
		echo "check $SIG_LOG for errors"
		return 1;
	fi
	sleep 3	

	pre_args="";
	post_args="";
	if [ $use_safe -eq 1 ]; then
		pre_args=" safe_sangoma"
		post_args=""
	else 
		post_args=" -bg"
	fi

	echo -n "Starting sangoma_mgd..."
	eval "$pre_args sangoma_mgd $post_args >/dev/null 2>/dev/null &"
	if [ $? -eq 0 ]; then
		echo "OK"
	else 
		echo "Failed"
		echo "Failed to start sangoma_mgd, check $LOG for errors"
		return 1;
	fi

	sleep 2	
	if [ ! $(pidof sangoma_mgd) ]; then
		echo "sangoma_mgd failed to start"
		echo "check $LOG for errors"
		return 1;
	fi

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

	return 0
}


function check_running ()
{
	local rc
	eval "pidof $sigd 2> /dev/null > /dev/null"
	rc=$?
	if [ $rc -eq 0 ]; then
		echo "$sigd is currently running"
		echo "exiting..."
		exit 0
	fi

	eval "pidof sangoma_mgd 2> /dev/null > /dev/null"
	rc=$?
	if [ $rc -eq 0 ]; then
		echo "sangoma_mgd is currently running"
		echo "exiting..."
		exit 0
	fi
	if [ -e /var/run/$sigd.pid ];then
		eval "rm -f /var/run/$sigd.pid >/dev/null 2>/dev/null";
	fi
	if [ -e /var/run/sangoma_mgd.pid ];then
		eval "rm -f /var/run/sangoma_mgd.pid >/dev/null 2>/dev/null";
	fi

	return 0
}

function check_pid_brid()
{
	if [ ! $(pidof $sigd) ]; then
		echo "$sigd is not running"
		exit 1
	fi
	return 0
}

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

	return $rc
}

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

	return $rc
}

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

	return $rc
}

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

	return $rc
}

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

	return $rc
}

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

	done	
}

parse_args

ret=0

if [ "$cmd" = "start" ]; then
	start_all
	ret=$?
	if [ $ret -ne 0 ]; then
		stop_all
	fi
elif [ "$cmd" = "safe_start" ]; then
	use_safe=1
	start_all
	ret=$?
	if [ $ret -ne 0 ]; then
		stop_all
	fi
	ret=$?
elif [ "$cmd" = "stop" ]; then
	stop_all
	ret=$?
elif [ "$cmd" = "restart" ]; then
	stop_all
	start_all
	if [ $ret -ne 0 ]; then
		stop_all
	fi
	ret=$?
else 
	if [ $SS7 -eq 1 ]; then
		echo "Error: Unsupported command $cmd for SS7"
		echo	
		usage
		exit 1
	fi

	if [ "$cmd" = "capture" ]; then
		toggle_capture	
		ret=$?
	elif [ "$cmd" = "v+" ]; then
		increase_verbose
		ret=$?
	elif [ "$cmd" = "v-" ]; then
		decrease_verbose
		ret=$?
	elif [ "$cmd" = "show_spans" ]; then
		show_spans
		ret=$?
	elif [ "$cmd" = "show_calls" ]; then
		show_calls
		ret=$?
	elif [ "$cmd" = "test" ]; then
		start_test
		ret=$?
	else 
		usage
		ret=1
	fi
fi

exit $ret
