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


WAN_LOCK=/var/lock/wanrouter_lock
SS7_LOCK=/var/lock/ss7box_wanrouter_lock

#Set the ss7boost file name
ss7boost="ss7boost"
#Set the ss7box file name
ss7box="ss7boxd"
bindir="/usr/sbin"
ss7box_mon="ss7boxd_monitor.sh"

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

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


if [ $PBXD = "asterisk" ]; then
	PBXD_LAUNCH="safe_$PBXD"
elif [ $PBXD = "callweaver" ]; then
	PBXD_LAUNCH="safe_$PBXD"
elif [ $PBXD = "freeswitch" ]; then
	if [ "$PBXD_BIN" = "" ]; then
		PBXD_BIN="/usr/local/freswitch"
	fi
	if [ "$PBXD_LAUNCH" = "" ]; then
		PBXD_LAUNCH="freswitch -nc"
	fi
	TMP=$PBXD_BIN/$PBXD_LAUNCH
	PBXD_LAUNCH=$TMP
fi


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 $PBXD

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

# PARTIAL
# Start/Stop/Restart partial SMG product.
# ss7boost, sangoma_mgd  and $PBXD
# (This is equivalent of restarting $PBXD 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

}



read_smg_conf ()
{
	
	WAN_HOME=/etc/wanpipe
	WAN_CONF_DIR=$WAN_HOME
	META_SMG_CONF=$WAN_HOME/smg.rc

	# Read meta-configuration file.
	
	if [ -f $META_SMG_CONF ] 
	then . $META_SMG_CONF
	else
		logit	" $META_SMG_CONF not found !!!!"
		return 1 
	fi
	return 0
}

function init_smg_conf()
{
SMG_BOOT=
SANGOMA_PRID=
SANGOMA_BRID=
SANGOMA_SS7ISUP=
SANGOMA_MEDIA_GATEWAY=
SANGOMA_SS7BOOST=
SANGOMA_SAFE_MODE=
SANGOMA_FREESWITCH=
SANGOMA_ASTERISK=
}   


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
	
	eval "smg_ctrl $cmd"
}


function ss7_box_ctrl ()
{
	local wp_list
	local ss7_lock="NO"
	cmd=$1

	cd $ss7dir
	
	if [ $cmd != "start" ]; then
	echo "Stopping:  $ss7box" 
	fi


	if [ -e $bindir/$ss7box_mon ]; then
		echo "Stopping $ss7box_mon"
        	eval "killall $ss7box_mon 2>> $smgctrl_log"
	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 $SS7_LOCK ] 
		do
			ss7_lock="YES"
			echo "ss7box lock detected $SS7_LOCK..."
			sleep 1
		done

		if [ $ss7_lock = "NO" ]; then
			while [ 1 ]
			do
				cnt=5
				while [ -e $WAN_LOCK ] 
				do
					echo "wanrouter lock detected $WAN_LOCK..."
					sleep 1
				done

				endloop=0
				while [ ! -e $WAN_LOCK ]
				do
                	echo "Waiting for ss7box to finish wanpipe start ..."
					sleep 1
					cnt=$((cnt-1))
					if [ $cnt -lt 1 ]; then
						endloop=1
                      	break;
					fi
				done

				if [ $endloop -gt 0 ]; then
                  	break
				fi

			done
		fi
		sleep 2
	
		if [ -e $bindir/$ss7box_mon ]; then
			echo "Starting $ss7box_mon"
			eval "nice $ss7box_mon &"
		fi

		echo "Starting:  wanrouter start"
		eval "wanrouter start >> $smgctrl_log 2>> $smgctrl_log"

		for ((i=0;i<6;i++)) 
		do
			sleep 5;
			echo "Waiting for ss7box to connect $i/6..."
		done
	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
}


init_smg_conf
read_smg_conf

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 $?


