#!/bin/bash -p
#/****************************************************************************
# Makefile: Installation makefile for ss7box, sangoma_isupd, and xygsccpd
#
# Copyright (C) 2006-2009  Xygnada Technology, Inc.
#****************************************************************************/
REL=3.5.11.9
XREL=wanpipe-$REL
BASE=$PWD
TOP=
TOPDRV=$TOP/usr/src
TOPBIN=$TOP/usr/local
TOPETC=$TOP/etc
DRV=$TOPDRV/Sangoma
DRVSETUP=$TOPDRV/Sangoma/$XREL
DRVSETUP_OLD=$TOPDRV/Sangoma/wanpipe_old
ETC_SS7=$TOPETC/ss7box
ETC_WP=$TOPETC/wanpipe
BIN=$TOPBIN/ss7box
SRC=$XREL/patches/kdrivers/src
DS=$DRV/$SRC
DSLIP=$DS/lip/bin
SMGDIR=/usr/src/Sangoma/$XREL/ssmg/sangoma_mgd.trunk
DRVR=$XREL.tgz
S_DRVR=wanpipe-2.3.2-6.tgz
DRVR_UPDATE1=xmtp2bin.tgz
DRVR_XMTP2KM=sangoma_xmtp2km.tgz
SS7BOXCONF=ss7box.conf
SS7BOOSTCONF=sangoma_isup.conf
WP_RC=wanrouter.rc
DATESTR=`date --iso-8601`
ARCHIVE_DIR="archive.$DATESTR"

SMG_CTRL_APP=smg_ctrl
SMG_CTRL_INIT_APP=smgss7_init_ctrl
SS7_MONITOR_APP=ss7boxd_monitor.sh

function make_ss7box_symlink
{
	rm -f /ss7box
	ln -s $BIN /ss7box
}

function make_wanpipe_symlink
{
	rm -fr /usr/src/Sangoma/wanpipe
	ln -s $DRVSETUP /usr/src/Sangoma/wanpipe
}

function make_smg_symlink
{
	rm -f /smg
	ln -s $SMGDIR /smg
}

function make_ss7box_symlinks
{
	rm -f /ss7box /root/ss7box
	cd /
	make_ss7box_symlink
}

function make_smg_symlinks
{
	rm -f /smg /root/smg
	cd /
	make_smg_symlink
}

function smg_uninstall
{
	echo smg_uninstall
	echo "removing $ETC_SS7"
	rm -fr $ETC_SS7
	echo "removing $DRVSETUP"
	rm -fr $DRVSETUP
	echo "removing $BIN"
	rm -fr $BIN
}

function archive
{
	echo "archive begin"
	rm -f /etc/wanpipe/wanpipe1.conf.?1*
	cp $ETC_SS7/*.conf .
	echo "   - ss7 config"
	cp $ETC_WP/*.conf* .
	cp $ETC_WP/wanrouter.rc .
	echo "   - wanpipe config"
        if [ -f $BIN/ss7boxd ]; then
                cp $BIN/ss7boxd* .
                echo "   - ss7boxd"
        fi

	echo "archive end"
}

function stop_process
{
        astatus=`ps axj | grep $1 | wc -l`
        if [ $astatus -gt 1 ]; then
                echo
                echo "Stopping $1!"
                echo
                echo "press Enter to continue; ctl-c to quit"
                read $blah
                echo
                $2
        fi
        astatus=`ps axj | grep $1 | wc -l`
        if [ $astatus -gt 1 ]; then
                echo
                echo "Stopping $1 failed!"
                echo
                exit -1
        fi
}

function init_check 
{
		echo "Stopping all SMG and Asterisk processes"
		echo "press Enter to continue; ctl-c to quit"
		read $blah
		killall -9 freeswitch
		rm -f /usr/local/freeswitch/log/freeswitch.pid
		killall -9 callweaver
		rm -f /var/run/callweaver.pid 
        killall -9 safe_asterisk
        killall -9 asterisk
		rm -f /var/run/asterisk.pid
        killall -9 ss7boxd
		/usr/local/ss7box/sangoma_isupd --term
		if [ -f /usr/sbin/sangoma_mgd ]; then
        sangoma_mgd -term
		fi
        wanrouter stop
        wanrouter stop all
        wanrouter status
}

function init_check_old
{

	psoutput=`ps fax`

	eval "echo \"$psoutput\" | grep callweaver > /dev/null 2> /dev/null"
	if [ $? -eq 0 ]; then
		echo
		echo "Error: callweaver running!"
		echo
		echo "press Enter to continue; ctl-c to quit"
		read $blah
		echo 
	fi

        astatus=`ps axj | grep asterisk | wc -l`
        if [ $astatus -gt 1 ]; then
                echo
                echo "Stopping asterisk!"
                echo
                echo "press Enter to continue; ctl-c to quit"
                read $blah
                echo
                asterisk -r -x"stop now"
        fi
        astatus=`ps axj | grep asterisk | wc -l`
        if [ $astatus -gt 1 ]; then
                echo
                echo "Stopping asterisk failed!"
                echo
                exit -1
        fi

	eval "echo \"$psoutput\" | grep sangoma_mgd > /dev/null 2> /dev/null"
	if [ $? -eq 0 ]; then
		echo
		echo "Error: Sangoma MGD Running!"
		ps axj | grep mgd
		echo
		exit -1
	fi

	eval "echo \"$psoutput\" | grep ss7boxd > /dev/null 2> /dev/null"
	if [ $? -eq 0 ]; then
		echo
		echo "Error: ss7boxd  Running!"
		echo
		ps axj | grep ss7
		exit -1
	fi

	eval "echo \"$psoutput\" | grep sangoma_isupd > /dev/null 2> /dev/null"
	if [ $? -eq 0 ]; then
		echo
		echo "Error: sangoma_isupd Running!"
		echo
		ps axj | grep ss7
		exit -1
	fi

#	eval "type asterisk > /dev/null 2> /dev/null"
#	if [ $? -ne 0 ]; then
#		echo
#		echo "Error: Asterisk is not installed!"
#		echo "       Install Asterisk First"
#		echo
#		exit -1
#	fi
#
#	if [ ! -e /usr/src/asterisk ]; then
#		echo
#		echo "Error: Asterisk sym link /usr/src/asterisk not found!"
#		echo
#		exit -1
#	fi

	wanrouter stop
	wanrouter stop all
	wanrouter status
	echo "Ensure all wanpipes are stopped."
	echo "press Enter to continue; ctl-c to quit"
	read $blah
	echo 

	if [ -e /usr/local/ss7box ]; then
		echo
		echo "archiving ss7box binaries and configuration"
		echo "press Enter to continue; ctl-c to quit"
		read $blah
		echo
	fi

	return 0
}

function move_directory
{
	echo "move_directory $1 to $2"
	if  [ -z $1 ] || [ ! -d $1 ] ; then
		echo "missing source directory: $1"
		return 1
	fi
	if  [ -z $2 ] ; then
		echo "target directory not specified: $1"
		exit -1
	fi
	if [ -d $2 ] ; then
		echo "replacing directory: $2"
		echo "press Enter to continue; ctl-c to quit"
		read $blah
	fi
	if ! rm -fr $2 ; then
		echo "rm $2 failed"
		exit 1
	fi
	if ! mv -f $1 $2 ; then
		echo "move directory failed"
		exit 1
	fi

	return 0
}

function make_directory
{
	echo "make_directory $2"
	if  [ -z $1 ] || [ ! -d $1 ] ; then
		echo "missing top directory: $1"
		exit 1
	fi
	if [ -d $2 ] ; then
		echo "directory already exists: $2"
		return 0
	fi
	if ! mkdir $2 ; then
		echo "mkdir failed: $2"
		exit 1
	fi

	return 0
}

function copy_file
{
	echo "copy $2 to $1"
	if [ -z $2 ] || [ ! -e $2 ] ; then
		echo "missing file: $2"
		exit 1
	fi
	if [ ! -d $1 ] ; then
		echo "missing directory: $1"
		exit 1
	fi
	if ! cp $2 $1 ; then
		echo "copy failed: $2"
		exit 1
	fi
	
	return 0
}

function untar
{
	echo "untarring $1/$2"
	if ! cd $1 ; then
		echo "failed change directory to $1"
		exit 1
	fi
	if ! tar xzf $2 ; then
		echo "failed to untar $2"
		exit 1
	fi
	if ! cd $BASE ; then
		echo "failed to change directory to $BASE"
		exit 1
	fi

	return 0
}

function install_sangoma_driver
{
	echo "install_sangoma_driver: set up directories and untar"
	#install base driver
	make_directory $TOPDRV $DRV
	if [ -z $2 ] ; then
		copy_file $DRV $DRVR
		untar $DRV $DRVR

		echo "install_sangoma_driver: install xmtp2km update"
		copy_file $DS $DRVR_XMTP2KM
		untar $DS $DRVR_XMTP2KM
	elif [ $2 == "s_series" ] ; then
		copy_file $DRV $S_DRVR
		untar $DRV $S_DRVR
	else
		echo "illegal parm2 in install_sangoma_driver"
		exit 1
	fi

	echo ""
	echo "press Enter to continue; ctl-c to quit"
	read $blah

	rc=0
	if [ -n "$1" ] ; then
		if [ $1 = "manual" ] ; then
			echo "install_sangoma_driver: setup Sangoma driver: manual"
			make_wanpipe_symlink
			#cd $DRVSETUP
			cd /usr/src/Sangoma/wanpipe
			ls -l /usr/src/Sangoma | grep "wanpipe \->"
			echo "press Enter to continue; ctl-c to quit"
			read $blah
			./Setup install
			rc=$?
		elif [ $1 = "freeswitch" ] ; then
			echo "install_sangoma_driver: setup Sangoma driver: freeswitch"
			make_wanpipe_symlink
			cd /usr/src/Sangoma/wanpipe
			ls -l /usr/src/Sangoma | grep "wanpipe \->"
			echo "press Enter to continue; ctl-c to quit"
			read $blah
			make openzap_ss7
			make install
			echo " "
			echo "coping smg_ctrl script..."
			cp -f $SMGDIR/rc/smg.rc.isupd_only /etc/wanpipe/smg.rc

			echo "PBXD=freeswitch" > /etc/wanpipe/pbxd
			echo "PBXD_BIN=/usr/local/freeswitch/bin" >> /etc/wanpipe/pbxd
			echo "PBXD_LAUNCH=\"freeswitch -nc\"" >> /etc/wanpipe/pbxd
			
			install -D -m 755 $SMGDIR/$SMG_CTRL_APP /usr/sbin/$SMG_CTRL_APP
			install -D -m 755 $SMGDIR/safe_sangoma /usr/sbin/safe_sangoma
			
			install -D -m 755 $SMGDIR/scripts/init.d/$SMG_CTRL_INIT_APP /etc/init.d/$SMG_CTRL_INIT_APP
			install -D -m 755 $SMGDIR/scripts/init.d/$SMG_CTRL_INIT_APP /usr/sbin/$SMG_CTRL_INIT_APP
			install -D -m 755 $SMGDIR/scripts/init.d/$SS7_MONITOR_APP /usr/sbin/$SS7_MONITOR_APP     

			if [ ! -f /etc/wanpipe/safe_sangoma.rc ]; then
			cp -f /usr/src/Sangoma/wanpipe/ssmg/sangoma_mgd.trunk/rc/safe_sangoma.rc /etc/wanpipe/
			fi

			echo "done"
			echo " "
			echo "please confirm that there were no errors during driver compile"
			echo "press Enter to continue; ctl-c to quit"
			read $blah
			#TODO -> send make output to tmp file and check for "error"
		elif [ $1 = "asterisk" ] ; then
			echo "install_sangoma_driver: setup Sangoma driver: asterisk"
			make_wanpipe_symlink
			cd /usr/src/Sangoma/wanpipe
			ls -l /usr/src/Sangoma | grep "wanpipe \->"
			echo "press Enter to continue; ctl-c to quit"
			read $blah
			make dahdi
			echo "please confirm that there were no errors during driver compile"
			echo "press Enter to continue; ctl-c to quit"
			read $blah
			#TODO -> send make output to tmp file and check for "error"
		else
			echo "parameter $1 not recognized"
			return 1
		fi
	else
		make_wanpipe_symlink
		#cd $DRVSETUP
		cd /usr/src/Sangoma/wanpipe
		pwd
		echo "press Enter to continue; ctl-c to quit"
		read $blah
		./Setup install --silent --protocols=XMTP2-AFT_TE1 --noautostart
		rc=$? 
	fi
	cd $BASE
	if [ $rc != 0 ] ; then
		echo "Setup failed"
		return 1
	fi

	cp wanpipe*.conf* $ETC_WP
	cp wanrouter.rc $ETC_WP
	
	return 0
}

function install_all_etc
{
	cp ss7*.conf* $ETC_SS7
	cp sangoma*.conf $ETC_SS7
	cp tg*.conf $ETC_SS7
	cp wanpipe*.conf* $ETC_WP
	cp wanrouter.rc $ETC_WP
}

function install_ss7box_etc
{
	echo install_ss7box_etc
	make_directory $TOPETC $ETC_SS7
	#copy_file $ETC_SS7 $SS7BOXCONF
	cp ss7*.conf* $ETC_SS7
	cp tg*.conf $ETC_SS7
	cp sangoma*.conf $ETC_SS7

	return 0
}

function install_ss7box_bin
{
	echo install_ss7box_bin
	make_directory $TOPBIN $BIN
	copy_file $BIN xps
	copy_file $BIN Tail
	copy_file $BIN vb.sh
	copy_file $BIN cr.sh
	copy_file $BIN ss7boxd
	copy_file $BIN new/ss7box_cli

	return 0
}

function install_sangoma_isupd
{
	echo install_sangoma_isupd

	make_directory $TOPETC $ETC_SS7
	cp $SS7BOOSTCONF $ETC_SS7
	cp new/sangoma_isupd $BIN
	echo install_sangoma_isup_cli
	cp new/sangoma_isup_cli $BIN
	cp sangoma_isup_cli.conf $ETC_SS7
	return 0
}

function install_ss7box
{
	echo "installing $1"
	move_directory $DRVSETUP $DRVSETUP_OLD
	smg_uninstall
	install_sangoma_driver manual $2
	if [ $? -ne 0 ]; then
		return 1
	fi
	install_ss7box_etc
	install_ss7box_bin
	make_ss7box_symlinks
	make_wanpipe_symlink
}

function install_cdrlogger
{
	echo "CDR Logger installer"
	make_directory /var/log /var/log/isup
	cp $BASE/cdr*.conf $ETC_SS7
	cp $BASE/new/cdr_logger $BIN
}

function install_smg
{
	echo install_smg
	init_check
	move_directory $DRVSETUP $DRVSETUP_OLD
	smg_uninstall
	install_sangoma_driver $1
	if [ $? -ne 0 ]; then
		return 1
	fi
	install_ss7box_etc
	install_ss7box_bin
	install_sangoma_isupd
	install_cdrlogger
	#make_wanpipe_symlink
	make_ss7box_symlink
	make_smg_symlink
	ls -l / | grep smg
	ls -l / | grep ss7
	ls -l /usr/src/Sangoma | grep "wanpipe \->"

	return 0
}

function install_freeswitch
{
	init_check
	move_directory $DRVSETUP $DRVSETUP_OLD
	smg_uninstall
	install_sangoma_driver freeswitch
	install_ss7box_etc
	install_ss7box_bin
	install_sangoma_isupd
	install_cdrlogger
	#make_wanpipe_symlink
	make_ss7box_symlink
	make_smg_symlink
	ls -l / | grep smg
	ls -l / | grep ss7
	ls -l /usr/src/Sangoma | grep "wanpipe \->"

	return 0
}

function stop_all
{
	asterisk -rx "stop now"
	usleep 5000
	killall -9 asterisk
	killall ss7boxd
	usleep 5000
	killall -9 ss7boxd
	cd /smg
	/smg/$SMG_CTRL_APP stop
	wanrouter stop
	wanrouter stop all
	wanrouter stop force 2> /dev/null > /dev/null
}

function tarball
{
	echo tarball
	datestring=$(date +%F)
#  	if [ -z $1 ] ; then
# 		echo "Missing revision string."
# 		exit 1
# 	fi
 	rm -f *~ smginstall*.tgz
	cd ..
	mv smginstall/ smginstall-$datestring/
	ls -l smginstall-$datestring/
 	tar czvf smginstall-$datestring.tgz smginstall-$datestring/
	mv smginstall-$datestring/ smginstall/
	mv smginstall-$datestring.tgz smginstall/
}

action=${1:?"missing action"}

case $action in
	install 		) install_smg manual ;;
	install_sangoma_isupd	) install_sangoma_isupd;;
	archive			) archive ;;
	uninstall		) uninstall ;;
	tarball			) tarball $2 ;;
	symlink			) make_wanpipe_symlink
						make_ss7box_symlink
						make_smg_symlink;;
	stop_all		) stop_all ;;
#	ss7mon			) install_ss7box ss7mon ;;
	asterisk		) install_smg asterisk ;;
	freeswitch		) install_freeswitch ;;
	cp_configs		) install_all_etc ;;
	*			) echo "unknown action"
esac

exit 0
