#!/bin/bash

cd /
wget http://pbxinaflash.net/scripts/xtide.tgz
tar -zxvf xtide.tgz
rm -f xtide.tgz

cd /var/lib/asterisk/agi-bin

sed -i 's|Today at|Today, at|' /var/lib/asterisk/agi-bin/xtide.cepstral

echo -n "Choose default text-to-speech engine for xTide for Asterisk: 1=Flite 2=Cepstral [Default=1]: "
read var
if [ -z $var ] ; then
 echo "Choice was 1. Installing xTide with Flite support. Please wait..."
elif [ $var -eq "2" ] ; then
 echo "Choice was 2. Installing xTide with Cepstral support. Please wait.."
 cp xtide.cepstral xtide
else
 echo "Choice was 1. Installing xTide with Flite support. Please wait..."
fi

cd /root/xtide

rpm -ivh wvs*
rpm -ivh libtcd*
rpm -ivh xtide-common*
rpm -ivh xtide-2*

sed -i '
/\-A INPUT \-p tcp \-m tcp \-\-dport 5038 \-j ACCEPT/ {
a\
\# Allow connections to xTide web server\
\-A INPUT \-p tcp \-m tcp \-\-dport 88 \-j ACCEPT
}' /etc/sysconfig/iptables

service iptables restart
service xttpd restart


sed -i '
/\[from-internal-custom\]/ {
a\
exten => 8433,1,Answer\
exten => 8433,n,Wait(1)\
exten => 8433,n,System(/var/lib/asterisk/agi-bin/xtide)\
exten => 8433,n,Playback(/tmp/xtide)\
exten => 8433,n,Wait(1)\
exten => 8433,n,Hangup\

}' /etc/asterisk/extensions_custom.conf

asterisk -rx "dialplan reload"

echo "xTide for Asterisk installation completed."
echo "To test, dial T-I-D-E from any phone."
echo "For xTide web access, go to the following:"
echo "http://serverIPaddress:88/"
echo "For config info, read the tutorial at:"
echo "http://bestof.nerdvittles.com/xtide/"
echo "Modification is required to permanently"
echo "enable the xTide web server on port 88."
