#!/bin/sh

home=`pwd`;

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



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 "Error: sangoma_mgd is running!"
		exit 1
	fi
	if [ -f /var/run/asterisk.pid ]; then
		echo "Error: asterisk is running"
		exit 1
	fi
fi

echo
echo "Checking Syslog ...."
if [ -e  /etc/syslog.conf ]; then
eval "grep "local2.*sangoma_mgd" /etc/syslog.conf" > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
	eval "grep "local2" /etc/syslog.conf " > /dev/null 2> /dev/null
	if [ $? -eq 0 ]; then
		echo
		echo "Warning : local2 is already used in syslog.conf"
		echo
	fi
	echo -e "\nlocal2.*                /var/log/sangoma_mgd.log\n" > tmp.$$
	eval "cat /etc/syslog.conf tmp.$$ > tmp1.$$"
	\cp -f tmp1.$$ /etc/syslog.conf
	eval "/etc/init.d/syslog restart"
fi

else
	echo "Warning: /etc/syslog.conf not found"
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 ]; then
	if [ -d ../libs ]; then
		echo -n "Installing SCTP RPMS ..."
		eval "rpm -i ../libs/lksctp-tools-1.0.6-1.el5.1.i386.rpm  ../libs/lksctp-tools-devel-1.0.6-1.el5.1.i386.rpm"
		if [ ! -e  /usr/include/netinet/sctp.h ]; then
			echo "Error"
			echo
			echo "Please install SCTP devel package: yum install lksctp-tools-devel"
			echo 
			exit 1
		fi 
		echo "OK"
	else 
		echo "Please install SCTP devel package: yum install lksctp-tools-devel"
		echo 
		exit 1
	fi
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 LibTeletone MGD ..."
echo
sleep 1
cd lib/libteletone
eval "./configure > /dev/null"
echo "..."
eval "make clean > /dev/null"
eval "make > /devnull" 
if [ $? -ne 0 ]; then
	exit 1;
fi
make install
echo "Ok."

cd $home

echo
echo
echo "Compiling Sangoma MGD ..."
echo
sleep 1
eval "make clean 2> /dev/null > /dev/null"
eval "make"
if [ $? -ne 0 ]; then
	exit 1;
fi
make INSTALLPREFIX=$rootdir install
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

