#!/bin/sh

# ----------------------------------------------------------------------------
# Prompt user for input.
# ----------------------------------------------------------------------------
prompt()
{
	if test $NONINTERACTIVE; then
		return 0
	fi

	echo -ne "$*" >&2
	read CMD rest
	return 0
}

# ----------------------------------------------------------------------------
# Get Yes/No
# ----------------------------------------------------------------------------
getyn()
{
	if test $NONINTERACTIVE; then
		return 0
	fi

	while prompt "$* (y/n) "
	do	case $CMD in
			[yY])	return 0
				;;
			[nN])	return 1
				;;
			*)	echo -e "\nPlease answer y or n" >&2
				;;
		esac
	done
}



home=`pwd`;

force="false"
noss7="true"
rootdir=""
pbxdir=/usr/src/asterisk
pbxd="asterisk"
cfgdir="/etc/asterisk"
mysyslog=" "



while [ ! -z $1 ]
do
	if [ $1 = '-force' ]; then
		force="force"
	elif [ $1 = '-noss7' ]; then
		noss7="true"
	elif [ $1 = '-pbxdir' ]; then
		shift
                pbxdir=$1
                if [ "$pbxdir" = "" ]; then
                        echo "Error: Invalid $pbxd dir: $pbxdir!";
                        exit 1
                elif [ ! -d $pbxdir ]; then
                        echo "Error: $pbxd dir does not exist: $pbxdir!";
                        exit 1
                fi

	elif [ $1 = '-rootdir' ]; then
		shift
		rootdir=$1
		if [ "$rootdir" = "" ]; then
			echo "Error: Invalid root dir: $rootdir!";
			exit 1		
		elif [ ! -d $rootdir ]; then
			echo "Error: root dir does not exist: $rootdir!";
			exit 1		
		fi

	else
		echo "Invalid option: $1";
		echo
		echo "Usage: ./install <optinal args>"
		echo
		exit 1
	fi
	shift
done

if [ -f $pbxdir/include/callweaver.h ]; then
	echo "PBXD=callweaver" > /etc/wanpipe/pbxd
	pbxd="callweaver"
	cfgdir="/usr/local/etc/callweaver"
else
	echo "PBXD=asterisk" > /etc/wanpipe/pbxd
	pbxd="asterisk"
	cfgdir="/etc/asterisk"
fi

if [ $noss7 != 'true' ]; then
	if [ ! -e /usr/local/ss7box/$ss7boost ]; then
		echo "Error: ss7boost not found in /usr/local/ss7box dir";
		exit 1
	fi
	if [ ! -e /usr/local/ss7box/$ss7boxd ]; then
		echo "Error: ss7boxd not found in /usr/local/ss7box dir";
		exit 1
	fi
fi

if [ $force = "force" ]; then
	echo "Stopping SMG..."
	eval "sangoma_mgd -term"
        kill -TERM $(pidof asterisk);
	echo "OK."
	echo
else 
	if [ -f /var/run/sangoma_mgd.pid ]; then
		echo "Warning: sangoma_mgd is running!"
		getyn "Would you like to stop sangoma_mgd?"
		if [ $? -ne 0 ]; then
			exit 1
		fi
		
		eval "sangoma_mgd -term"
		if [ -f /var/run/sangoma_mgd.pid ];then
			echo "Failed to stop sangoma_mgd"
			exit 1
		fi
	fi
	if [ -f /var/run/asterisk.pid ]; then
		echo "Warning: asterisk is running"
		getyn "Would you like to stop asterisk?"
		if [ $? -ne 0 ]; then
			exit 1
		fi
		eval "asterisk -rx \"stop now\" >/dev/null 2>/dev/null"
		sleep 2
		if [ -f /var/run/asterisk.pid ]; then
       			kill -TERM $(pidof asterisk);
			sleep 2
			if [ -f /var/run/asterisk.pid ]; then
				echo "Failed to stop asterisk"
				exit 1
			fi
		fi
	fi
fi

echo
echo "Checking Syslog ...."
if [ -e  /etc/syslog.conf ]; then
        echo "found /etc/syslog.conf "
        mysyslog=syslog
elif [ -e /etc/rsyslog.conf ] ; then
        echo " found /etc/rsyslog.conf "
        mysyslog='rsyslog'
else
               echo " Warning : Syslog not found "
               mysyslog=" "
fi


if [  $mysyslog != " " ]; then
	eval "grep "local2.*sangoma_mgd" /etc/$mysyslog.conf" > /dev/null 2> /dev/null
	if [ $? -ne 0 ]; then
		eval "grep "local2" /etc/$mysyslog.conf " > /dev/null 2> /dev/null
		if [ $? -eq 0 ]; then
			echo
			echo "Warning : local2 is already used in $mysyslog.conf"
			echo
		fi
		echo -e "\n# Sangoma Media Gateway log" > tmp.$$
		echo -e "local2.*                /var/log/sangoma_mgd.log\n" >> tmp.$$
		eval "cat /etc/$mysyslog.conf tmp.$$ > tmp1.$$"
		\cp -f tmp1.$$ /etc/$mysyslog.conf
		eval "/etc/init.d/$mysyslog restart"
	fi
	eval "grep "local3.*sangoma_bri" /etc/$mysyslog.conf" > /dev/null 2> /dev/null
        if [ $? -ne 0 ]; then
                eval "grep "local3" /etc/$mysyslog.conf " > /dev/null 2> /dev/null
                if [ $? -eq 0 ]; then
                        echo
                        echo "Warning : local3 is already used in $mysyslog.conf"
                        echo
                fi
		echo -e "\n# Sangoma BRI Daemon (smg_bri)  log" > tmp.$$
                echo -e "local3.*                /var/log/sangoma_bri.log\n" >> tmp.$$
                eval "cat /etc/$mysyslog.conf tmp.$$ > tmp1.$$"
                \cp -f tmp1.$$ /etc/$mysyslog.conf
                eval "/etc/init.d/$mysyslog restart"
        fi
fi

if [ -f tmp1.$$ ]; then
	rm -f  tmp1.$$
fi
if [ -f tmp.$$ ]; then
        rm -f  tmp.$$
fi

echo "Ok"
echo
echo "Checking logrotate ..."
eval "type logrotate" > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
	echo "Error: Logrotate not found !"
fi

if [ -e /etc/logrotate.d ] && [ -e /etc/logrotate.d/syslog ]; then

	eval "grep sangoma_mgd /etc/logrotate.d/syslog" > /dev/null 2> /dev/null
	if [ $? -ne 0 ]; then
		eval "sed -e 's/messages/messages \/var\/log\/sangoma_mgd.log/' /etc/logrotate.d/syslog >tmp2.$$ 2>/dev/null"
		eval "cp -f tmp2.$$ /etc/logrotate.d/syslog"
		eval "logrotate -f /etc/logrotate.d/syslog" 
		if [ $? -ne 0 ]; then
			echo "Error: logrotate restart failed!";
			exit 1;
		fi
		echo "Logrotate is being changed and restarted!"
	else
		echo "Logrotate is configured!"
	fi

else
        echo "Error: Logrotate dir: /etc/logrotate.d not found !"
fi

echo "OK."
echo
echo "Checking for SCTP Utilities...."
if [ ! -e  /usr/include/netinet/sctp.h ] && [ ! -e /usr/include/sctp.h ]; then
	echo "Error: lksctp-tools-devel package missing"
	exit 1
fi 
echo "OK."
echo

echo "Checking for SCTP modules..."
eval "modprobe -l | grep \"\/sctp.ko\" >/dev/null 2>/dev/null"
if [ $? -ne 0 ]; then
	if [ ! -e /proc/net/sctp ]; then
		echo "Warning: Your Kernel does not support SCTP Protocol!"
		echo "SCTP is needed by SMG!"
		echo
		echo "Please contact sangoma support!"
		echo
		exit 1
	fi
fi
echo "OK."
echo


echo "Compiling Sangoma MGD ..."
eval "make clean 2> /dev/null > /dev/null"
if [ $noss7 == 'true' ]; then
        eval "make NO_SS7=YES" 
else
        eval "make" 
fi

if [ $? -ne 0 ]; then
	exit 1;
fi

if [ $noss7 == 'true' ]; then
	make INSTALLPREFIX=$rootdir NO_SS7=YES install
else
	make INSTALLPREFIX=$rootdir install
fi
echo "Ok."

if [ -d chan_woomera.trunk ]; then

	cd chan_woomera.trunk

	echo "Compiling Woomera Channel ..."

	if [ ! -e $pbxdir ]; then
		echo
		echo "Error: $pbxdir directory does not exist!"
		echo "       Please create symlink /usr/src/asterisk and"
		echo "       point to existing asterisk source!"
		echo "       Then re run ./install.sh "
		echo
		exit 1
	fi

	eval "make clean 2> /dev/null > /dev/null"
	eval "make PBXDIR=$pbxdir"
	if [ $? -ne 0 ]; then
		exit 1;
	fi

	make INSTALLPREFIX=$rootdir install
	echo "Ok."

else
	echo "Warning: chan_woomera directory does not exist!"
	exit 1
fi


echo "---------------------------------"
echo
echo " SMG Install Done"
echo
echo "--> Config: /etc/sangoma_mgd.conf"
echo "--> Start:  sangoma_mgd -bg"
echo
echo " Chan Woomera Install Done"
echo 
echo "--> Config: $cfgdir/woomera.conf"
echo "--> Start: start $pbxd (part of $pbxd)"
echo
echo "---------------------------------"

exit 0

