#!/bin/sh
# chkconfig: 2345 15 15
# description: Starts and stop SS7/Wanpipe devices


WAN_LOCK=/var/lock/wanrouter_lock


#Set the ss7boost file name
ss7boost="ss7boost"
#Set the ss7box file name
ss7box="ss7boxd"

if [ -f /etc/wanpipe/pbxd ]; then
	. /etc/wanpipe/pbxd
fi

if [ "$PBXD" = "" ]; then
	PBXD="asterisk"
fi

PBXD_LAUNCH="safe_$PBXD"


smgctrl_log="/etc/wanpipe/smgctrl_log"
ss7dir="/usr/local/ss7box"

homedir=$(pwd)

prog=$0
arg1=$1
arg2=$2

NICE="nice -n -15 "
RENICE="renice -15 -p "

ulimit -n 65536

function usage
{

cat << EOUT | more

./smgss7_init_ctrl [ start | stop | restart ]  < options >

The smgss7_init_ctrl should be used to start/stop/restart the
full SMP product.  

# FULL
# Start/Stop/Restart full SMG product.
# ss7box, all wanpipe devices,
# ss7boost, sangoma_mgd  and asterisk

  ./smgss7_init_ctrl start    
  ./smgss7_init_ctrl stop      
  ./smgss7_init_ctrl restart 

# PARTIAL
# Start/Stop/Restart partial SMG product.
# ss7boost, sangoma_mgd  and asterisk
# (This is equivalent of restarting asterisk only)
# In production ss7 line is usually not restarted

  ./smgss7_init_ctrl start 	isup
  ./smgss7_init_ctrl stop 	isup
  ./smgss7_init_ctrl restart isup


wanrouter wanrc:
  configure wanrouter wanrc to start Voice Only wanpipe
  devices.  The ss7box will start all wanpipe devices that
  contain Flink/Alink ss7 channels.
  --> wanrouter wanrc 


Boot Setup:
  copy smgss7_init_ctrl to /etc/init.d/ directory
  --> cp smgss7_init_ctrl /etc/init.d


EOUT

}




function pbx_ctrl ()
{
        cmd=$1

        cd $ss7dir

	if [ $cmd != "start" ]; then
		echo "Stopping:  $PBXD"
	fi
	eval "killall $PBXD_LAUNCH 2> /dev/null > /dev/null"
	eval "killall $PBXD 2> /dev/null > /dev/null"
	sleep 1
	eval "killall $PBXD 2> /dev/null > /dev/null"
	if [ $? -eq 0 ]; then
		eval "killall -9 $PBXD_LAUNCH 2> /dev/null > /dev/null"
		eval "killall -9 $PBXD 2> /dev/null > /dev/null"
	fi
	
        if [ $cmd = "start" ]; then
                sleep 1
		echo "Starting:  $PBXD"
                eval "$PBXD_LAUNCH"
        fi

        cd $homedir
}



function ss7_boost_ctrl ()
{
	cmd=$1
	
	cd $ss7dir

	if [ $cmd != "start" ]; then
		echo "Stopping:  $ss7boost" 
	fi
	
	eval "./$ss7boost --term 2>> $smgctrl_log"
	
	if [ $cmd != "start" ]; then
		echo "Stopping:  sangoma_mgd"
	fi
	eval "sangoma_mgd -term >> $smgctrl_log 2>> $smgctrl_log"

	if [ $cmd = "start" ]; then
		sleep 1
		echo "Starting:  $ss7boost"
		eval "./$ss7boost >> $smgctrl_log 2>> $smgctrl_log"
		sleep 5
		echo "Starting:  sangoma_mgd"
		eval "$RENICE $(pidof $ss7boost) 2>> $smgctrl_log >> $smgctrl_log"
		eval "sangoma_mgd -bg >> $smgctrl_log 2>> $smgctrl_log"
	fi

	cd $homedir
}


function ss7_box_ctrl ()
{
	local wp_list
        cmd=$1

        cd $ss7dir
	
	if [ $cmd != "start" ]; then
	echo "Stopping:  $ss7box" 
	fi
        eval "kill -TERM $(pidof  $ss7box) 2>> $smgctrl_log"
	
	if [ $cmd != "start" ]; then
		echo "Stopping:  wanrouter stop"
	fi
	
	eval "wanrouter stop >> $smgctrl_log 2>> $smgctrl_log" 
	stop_running_wanpipes $cmd
	
	eval "modprobe -r xmtp2km >> $smgctrl_log 2>> $smgctrl_log"

        if [ $cmd = "start" ]; then
                sleep 1
		echo "Starting:  $ss7box"
                eval "$NICE ./$ss7box >> $smgctrl_log"
		sleep 2
		while [ -e $WAN_LOCK ] 
		do
			echo "wanrouter lock detected $WAN_LOCK..."
			sleep 1
		done
	       	sleep 1
	
		echo "Starting:  wanrouter start"
		eval "wanrouter start >> $smgctrl_log 2>> $smgctrl_log"
        fi

        cd $homedir
}

function stop_running_wanpipes ()
{
	local wpcnt=0;
	local cmd=$1;
	
	wp_list=`wanrouter list | cut -d':' -f2 | grep wanpipe`
	
	for list in $wp_list
	do
		list=${list// /};
		if [ ! -z $list ]; then
			if [ $cmd != "start" ]; then
				echo "Stopping:  wanrouter stop $list"
			fi 
			wanrouter stop $list >> $smgctrl_log 2>> $smgctrl_log
		fi
	done
}



case "$arg1" in
  start)

	eval "modprobe sctp 2> /dev/null > /dev/null"
	
	if [ "$arg2" == isup ]; then
		ss7_boost_ctrl "$arg1"		
		pbx_ctrl "$arg1"
	else
		ss7_box_ctrl "$arg1"		
		ss7_boost_ctrl "$arg1"		
		pbx_ctrl "$arg1"
		
		echo
		cat /proc/net/wanrouter/status 
		echo
	fi
	;;

  stop)

	if [ "$arg2" == isup ]; then
                pbx_ctrl "$arg1"
                ss7_boost_ctrl "$arg1"
        else
                pbx_ctrl "$arg1"
                ss7_boost_ctrl "$arg1"
                ss7_box_ctrl "$arg1"
		
		if [ -e /proc/net/wanrouter/status ]; then 
			cat /proc/net/wanrouter/status 
		else 
			echo
			echo "Wanrouter Stopped"
			echo
		fi
        fi
	;;

  restart)

	cd "$homedir"
        $prog stop "$arg2"
        $prog start "$arg2"
	;;

	*)
	usage
	;;
esac

exit $?


