#!/bin/bash

clear
echo "This script installs Google Voice support on your PBX."
echo "You must have a system that is compatible with PBX in a Flash."
echo "WARNING: No error checking is provided."
echo "By using this script, you agree to assume ALL RISK."
echo "NO WARRANTY, EXPRESS OR IMPLIED, OF ANY KIND IS PROVIDED."
echo " "
echo "If you make a typo while entering values below, press Ctrl-C and start over."
echo " "
echo "Your Google Voice entries are stored in /etc/asterisk/extensions_custom.conf."
echo "Edit that file and reload your Asterisk dialplan if you make future changes."
echo " "
echo -n "10-digit Google Voice phone number (e.g. 9871234567): "
read gvnum
echo "GVNUM: $gvnum"
echo -n "Google Voice email address: "
read acctname
echo "ACCTNAME: $acctname"
echo -n "Google Voice password: "
read acctpass
echo "ACCTPASS: $acctpass"
echo -n "11-digit Ring Back DID (e.g. 16781234567): "
read ringback
echo "RINGBACK: $ringback"
echo -n "Parking Lot Magic Number: "
read callpark
echo "CALLPARK: $callpark"
echo " "
echo "We're now ready to begin the installation."
echo "Check your entries above and then press Enter key to proceed."
echo "Or press Ctrl-C to abort and try again."
read readysetgo
clear
echo "Installing Google Voice support for your PBX. One moment please..."
echo " "

echo " " >> /etc/asterisk/extensions_custom.conf
echo "[custom-gv]" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,1,Wait(1)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Set(ACCTNAME=$acctname)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Set(ACCTPASS=$acctpass)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Set(RINGBACK=$ringback)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Set(CALLPARK=$callpark)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Playback(pls-wait-connect-call)" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,System(gvoice -e \${ACCTNAME} -p \${ACCTPASS} call \${EXTEN} \${RINGBACK})" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Set(PARKINGEXTEN=\${CALLPARK})" >> /etc/asterisk/extensions_custom.conf
echo "exten => _X.,n,Park()" >> /etc/asterisk/extensions_custom.conf
echo " " >> /etc/asterisk/extensions_custom.conf
echo "[custom-park]" >> /etc/asterisk/extensions_custom.conf
echo "exten => s,1,Wait(4)" >> /etc/asterisk/extensions_custom.conf
echo "exten => s,2,Set(GVNUM=$gvnum)" >> /etc/asterisk/extensions_custom.conf
echo "exten => s,3,Set(CALLPARK=$callpark)" >> /etc/asterisk/extensions_custom.conf
echo "exten => s,4,NoOp(**CALLERID: \${CALLERID(number)})" >> /etc/asterisk/extensions_custom.conf
echo 'exten => s,5,GotoIf($["${CALLERID(number)}"="${GVNUM}"]?6:7)' >> /etc/asterisk/extensions_custom.conf
echo "exten => s,6,ParkedCall(\${CALLPARK})" >> /etc/asterisk/extensions_custom.conf
echo "exten => s,7,Goto(from-trunk,gv-incoming,1)" >> /etc/asterisk/extensions_custom.conf
echo " " >> /etc/asterisk/extensions_custom.conf

echo "Reloading Asterisk dialplan..."
asterisk -rx "dialplan reload"
echo " "

if [ -e "/usr/bin/gvoice" ]
then
  echo "pygooglevoice is already installed on your system. Skipping."
  echo "Be sure you have removed the original [custom-gv] context!!!"
  echo " "
else
 echo "Installing Python add-ons..."
cd /root
#yum install mercurial
yum -y install python-setuptools
easy_install simplejson
#hg clone https://pygooglevoice.googlecode.com/hg/ pygooglevoice

echo " "
echo "Installing pygooglevoice..."

wget http://bestof.nerdvittles.com/applications/gv/pygooglevoice.tgz
tar zxvf pygooglevoice.tgz

cd pygooglevoice
python setup.py install

#sed -i 's|    help(|#    help(|' /usr/bin/gvoice

fi


echo " "
echo "Installation script is finished. Do NOT run it again on this system!"
echo " "
echo "For complete documentation, see the following Nerd Vittles article:"
echo " "
echo "http://nerdvittles.com/?p=635"
