#!/bin/bash
# Asterisk/FreePBX setup script
# Christopher Poole
# email username: poolecl
# email server: yahoo.ca
# updated 13-march-2011
# you should generate your own passwords
# http://strongpasswordgenerator.com/
# no, these are not the same as the ones I used
MYSQL_ROOT="X9v2T704g7yn22"
MYSQL_ASTERISK="X9v2T704g7yn22"
AMPMGRPASS="JXoyRT0hR06Q3i"
#update packages
yum -y update
#upgrade distribution
yum -y upgrade
#install things for my sanity, https, and proper compilation
yum -y install joe tree mc mod_ssl php gcc mysql gcc-c++ php-mysql mysql mysql-server subversion doxygen
#probably want festival text to speech
#install things recommended
yum -y install e2fsprogs-devel keyutils-libs-devel krb5-devel libogg libselinux-devel libsepol-devel libxml2-devel libtiff-devel gmp php-pear php-pear-DB php-gd php-mysql php-pdo php-devel kernel-devel ncurses-devel audiofile-devel libogg-devel openssl-devel mysql-devel zlib-devel perl-DateManip sendmail-cf sox
#set timezone
ln -sf /usr/share/zoneinfo/US/Eastern /etc/localtime
#install lame
cd /usr/src
wget http://downloads.sourceforge.net/project/lame/lame/3.98.4/lame-3.98.4.tar.gz
tar zxvf lame-3.98.4.tar.gz
rm lame-3.98.4.tar.gz
cd lame-3.98.4
./configure
make
make install
#install mpg123
cd /usr/src
wget http://sourceforge.net/projects/mpg123/files/mpg123/1.13.1/mpg123-1.13.1.tar.bz2/download
tar jxvf mpg123-1.13.1.tar.bz2
rm mpg123-1.13.1.tar.bz2
cd mpg123-1.13.1
./configure
make
make install
#install iksemel
cd /usr/src
wget http://iksemel.googlecode.com/files/iksemel-1.4.tar.gz
tar zxvf iksemel-1.4.tar.gz
rm iksemel-1.4.tar.gz
cd iksemel-1.4
./configure
make
make install
#set up /usr/local/lib libraries
echo "/usr/local/lib" >> /etc/ld.so.conf.d/usr-local.conf
ldconfig
#add asterisk repos
cd /etc/yum.repos.d/
wget http://packages.asterisk.org/centos/centos-asterisk.repo
wget http://packages.digium.com/centos/centos-digium.repo
#install asterisk dependancies
yum -y install dahdi-linux dahdi-tools libpri bluez-libs dahdi-firmware dahdi-firmware-hx8 dahdi-firmware-oct6114-064 dahdi-firmware-oct6114-128 dahdi-firmware-tc400m kernel kmod-dahdi-linux kmod-dahdi-linux-fwload-vpmadt032 libopenr2 libss7 libtonezone yum-kmod
#install asterisk
cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-1.8-current.tar.gz
tar zxvf asterisk-1.8-current.tar.gz
rm asterisk-1.8-current.tar.gz
cd asterisk-1.8-current
cd `ls | grep asterisk-1.8*`
./configure
make menuselect.makeopts
menuselect/menuselect --enable-category MENUSELECT_ADDONS menuselect.makeopts
menuselect/menuselect --enable EXTRA-SOUNDS-EN-WAV menuselect.makeopts
contrib/scripts/get_mp3_source.sh
make
make progdocs
make install
make samples
make config
make install-logrotate
#start mysql and set autostart
/etc/init.d/mysqld start
chkconfig mysqld on
#create asterisk SQL dependancies
mysqladmin -u root password $MYSQL_ROOT
useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisk
mysqladmin -u root -p$MYSQL_ROOT create asterisk
mysqladmin -u root -p$MYSQL_ROOT create asteriskcdrdb
echo "GRANT ALL PRIVILEGES ON asterisk. * TO asterisk@localhost IDENTIFIED BY '$MYSQL_ASTERISK';" > ~/pbx-setup.sql
echo "GRANT ALL PRIVILEGES ON asteriskcdrdb. * TO asterisk@localhost IDENTIFIED BY '$MYSQL_ASTERISK';" >> ~/pbx-setup.sql
echo "flush privileges;" >> ~/pbx-setup.sql
mysql -u root -p$MYSQL_ROOT asteriskcdrdb < ~/pbx-setup.sql
#edit res_config_mysql
sed -i "s/^;dbhost = 127.0.0.1$/dbhost = 127.0.0.1/" /etc/asterisk/res_config_mysql.conf
sed -i "s/^;dbname = asterisk$/dbname = asterisk/" /etc/asterisk/res_config_mysql.conf
sed -i "s/^;dbuser = myuser$/dbuser = asterisk/" /etc/asterisk/res_config_mysql.conf
sed -i "s/^;dbpass = mypass$/dbpass = $MYSQL_ASTERISK/" /etc/asterisk/res_config_mysql.conf
#edit some files for freepbx
sed -i "s/User apache/User asterisk/" /etc/httpd/conf/httpd.conf
sed -i "s/Group apache/Group asterisk/" /etc/httpd/conf/httpd.conf
sed -i "s/AllowOverride None/AllowOverride All/" /etc/httpd/conf/httpd.conf
sed -i "s/post_max_size = 8M/post_max_size = 20M/" /etc/php.ini
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 20M/" /etc/php.ini
#use ssl only
sed -i "s/^$/\n RewriteEngine On\n RewriteCond %{HTTPS} off\n RewriteRule (.*) https:\/\/%{HTTP_HOST}%{REQUEST_URI}/g" /etc/httpd/conf/httpd.conf
/etc/init.d/httpd restart
#asterisk uses no tty
sed -i "s/^TTY=9/#TTY=9/g" /usr/sbin/safe_asterisk
#set some permissions for freepbx
chown -R asterisk:asterisk /var/spool/asterisk/
chown -R asterisk:asterisk /var/log/asterisk/
chown -R asterisk:asterisk /var/run/asterisk/
chown -R asterisk:asterisk /var/lib/php/session/
#install freepbx
cd /usr/src/
wget http://mirror.freepbx.org/freepbx-2.9.0beta2.tar.gz
tar zxvf freepbx-2.9.0beta2.tar.gz
rm freepbx-2.9.0beta2.tar.gz
cd freepbx-2.9.0beta2
#further set up mysql database for freepbx
mysql -u root -p$MYSQL_ROOT asterisk < SQL/newinstall.sql
mysql -u root -p$MYSQL_ROOT asteriskcdrdb < SQL/cdr_mysql_table.sql
#copy freepbx config and set defaults
cp amportal.conf /etc/amportal.conf
sed -i "s/^ARI_ADMIN_PASSWORD=ari_password$/ARI_ADMIN_PASSWORD=w47e0273O5441K/g" /etc/amportal.conf
sed -i "s/^# AMPDBNAME=asterisk$/AMPDBNAME=asterisk/g" /etc/amportal.conf
sed -i "s/^AMPDBENGINE=mysql/AMPDBENGINE=mysql\nAMPDBUSER=asterisk\nAMPDBPASS=$MYSQL_ASTERISK/g" /etc/amportal.conf
sed -i "s/^AMPMGRPASS=amp111$/AMPMGRPASS=$AMPMGRPASS/g" /etc/amportal.conf
sed -i "s/^# AMPWEBADDRESS=x.x.x.x|hostname$/AMPWEBADDRESS=pbx.newfire.org/g" /etc/amportal.conf
#sed -i "s/^AMPEXTENSIONS=extensions$/AMPEXTENSIONS=deviceanduser/g" /etc/amportal.conf
#start asterisk and install freepbx
./start_asterisk start
./install_amp
#fix a symlink
mv /etc/asterisk/sip_notify.conf ~/
ln -s /var/www/html/admin/modules/core/etc/sip_notify.conf /etc/asterisk/sip_notify.conf
#set the password
sed -i "s/^secret = amp111$/secret = $AMPMGRPASS/g" /etc/asterisk/manager.conf
#autostart freepbx
echo "/usr/local/sbin/amportal start" >> /etc/rc.local
#reboot when all is done
reboot