#!/bin/bash
#
# This script to install the Zaptel drivers, Asterisk and FreePBX
# on a new install of Centos 5.3 
#
# Must be run with superuser privileges.
#
# There is no error checking here, but it is known to work on a base 
# install as of 01 Jun 2009, with downloaded versions:
# asterisk-1.4.25
# asterisk-addons-1.4.8
# freepbx-2.5.1
# libpri-1.4.10
# zaptel-1.4.12.1
#
# Sections of this script originally written for Ubuntu by Dr. Peter N. Steinmetz 
#
# Preston Moore - www.prestonmoore.com
#
# Don't forget to "yum update" and REBOOT before executing the script!
#
# definitions of items to possibly change
export IP_ADDRESS=192.168.1.1
export FREEPBX_VERSION=2.5.1
export MYSQL_ROOT_PW=passw0rd
export ASTERISK_DB_PW=passw0rd
export ASTERISK_MGR_PW=passw0rd
export ARI_PW=passw0rd

# install mysql server
yum -y install mysql-server
/etc/init.d/mysqld start
chkconfig mysqld on

# configure mysql root password
mysqladmin -u root password ${MYSQL_ROOT_PW}

# install packages needed beyond base install
yum -y install kernel-devel
yum -y install make bison flex gcc-c++ gcc httpd php
yum -y install php-cli php-mysql php-pear php-gd curl
yum -y install sox ncurses-devel openssl-devel mysql-devel
yum -y install gtk2-devel

# install pear DB module
pear install db

# place source packages in standard place
cd /usr/src

## download, make and install XML library for Google Talk
#yum -y install gnutls-devel
#wget http://iksemel.googlecode.com/files/iksemel-1.3.tar.gz
#tar xfz iksemel-1.3.tar.gz
#cd iksemel-1.3
#./configure
#make
#make install
#cd ..

## download, make and install Speex codec
#yum -y install libogg-devel
#wget http://downloads.xiph.org/releases/speex/speex-1.2rc1.tar.gz
#tar xfz speex-1.2rc1.tar.gz
#cd speex-1.2rc1
#./configure
#make
#make install
#cd ..

# download, make, install, and configure zaptel drivers
wget http://downloads.digium.com/pub/zaptel/zaptel-1.4-current.tar.gz
tar xfz zaptel-1.4-current.tar.gz 
cd `find . -name "zaptel-1.4.*" -print`
./configure
make 
make install 
make config
cd ..

# download, make and install libpri
wget http://downloads.digium.com/pub/libpri/libpri-1.4-current.tar.gz
tar xfvz libpri-1.4-current.tar.gz
cd `find . -name "libpri-1.4.*" -print`
make 
make install
cd ..

# download, make and install asterisk and configuration files
wget http://downloads.digium.com/pub/asterisk/asterisk-1.4-current.tar.gz
tar xfvz asterisk-1.4-current.tar.gz 
cd `find . -name "asterisk-1.4.*" -print`
./configure
make 
make install
make samples
cd ..

# download, make and install asterisk-addons
wget http://downloads.digium.com/pub/asterisk/asterisk-addons-1.4-current.tar.gz
tar xfvz asterisk-addons-1.4-current.tar.gz 
cd `find . -name "asterisk-addons-1.4.*" -print`
./configure
make 
make install
cd ..

# download and install extra asterisk sounds
mkdir asterisk-sounds
cd asterisk-sounds
wget http://downloads.digium.com/pub/telephony/sounds/asterisk-extra-sounds-en-gsm-current.tar.gz
tar -zxf asterisk-extra-sounds-en-gsm-current.tar.gz
cp -rf * /var/lib/asterisk/sounds
cd ..

# create asterisk user and group for apache server
useradd -c "Asterisk PBX" -d /var/lib/asterisk asterisk

# fix up apache configuration to run as asterisk user
chown asterisk /var/lib/php/session/
cp -v /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.original
sed -i "s/User apache/User asterisk/" /etc/httpd/conf/httpd.conf
sed -i "s/Group apache/Group asterisk/" /etc/httpd/conf/httpd.conf

# add dummy timing device for asterisk
modprobe ztdummy

# download and unpack freepbx
wget http://superb-east.dl.sourceforge.net/sourceforge/amportal/freepbx-${FREEPBX_VERSION}.tar.gz
tar xfvz freepbx-${FREEPBX_VERSION}.tar.gz

# configure freepbx
cd freepbx-${FREEPBX_VERSION}

# setup databases for freepbx use
mysqladmin -u root -p${MYSQL_ROOT_PW} create asterisk
mysqladmin -u root -p${MYSQL_ROOT_PW} create asteriskcdrdb
mysql -u root -p${MYSQL_ROOT_PW} asterisk < SQL/newinstall.sql
mysql -u root -p${MYSQL_ROOT_PW} asteriskcdrdb < SQL/cdr_mysql_table.sql
mysql -u root -p${MYSQL_ROOT_PW} <<-END_PRIVS
        GRANT ALL PRIVILEGES ON asterisk.* TO asteriskuser@localhost IDENTIFIED BY "${ASTERISK_DB_PW}";
        GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO asteriskuser@localhost IDENTIFIED BY "${ASTERISK_DB_PW}";
        flush privileges;
END_PRIVS

# reconfigure php for freepbx
cp -v /etc/php.ini /etc/php.ini-orig
sed -i "s/\(upload_max_filesize *= *\)\(.*\)/\120M/" /etc/php.ini
sed -i "s/\(memory_limit *= *\)\(.*\)/\1100M/" /etc/php.ini

# fix up directory use and permissions for asterisk
mkdir /var/run/asterisk
chown asterisk:asterisk -Rv /var/run/asterisk
chown asterisk:asterisk -Rv /etc/asterisk
chown asterisk:asterisk -Rv /var/lib/asterisk
chown asterisk:asterisk -Rv /dev/zap
chown asterisk:asterisk -Rv /var/log/asterisk
chown asterisk:asterisk -Rv /var/spool/asterisk
chown asterisk:asterisk -Rv /var/www/html
sed -i "s|astrundir *=> */var/run|astrundir => /var/run/asterisk|" /etc/asterisk/asterisk.conf

# configure amportal
cp -v amportal.conf /etc/amportal.conf
sed -i "s/# \(AMPDBUSER=asteriskuser\) */\1/" /etc/amportal.conf
sed -i "s/# \(AMPDBPASS=\).*/\1${ASTERISK_DB_PW}/" /etc/amportal.conf
sed -i "/#AMPWEBADDRESS=192.168.1.101/d" /etc/amportal.conf
sed -i "s/AMPWEBADDRESS=/AMPWEBADDRESS=${IP_ADDRESS}/" /etc/amportal.conf
sed -i "s/\(AMPMGRPASS=*\)\(.*\)/\1${ASTERISK_MGR_PW}/" /etc/amportal.conf
sed -i "s/\(secret *= *\)\(.*\)/\1${ASTERISK_MGR_PW}/" /etc/asterisk/manager.conf

# start asterisk
./start_asterisk start

# install freepbx
./install_amp

# start apache web server
/etc/init.d/httpd start
chkconfig httpd on

# set ARI admin password
sed -i "s/ari_password/${ARI_PW}/" /var/www/html/recordings/includes/main.conf.php

# moh fix
ln -s /var/lib/asterisk/moh /var/lib/asterisk/mohmp3
chown asterisk:asterisk /var/lib/asterisk/mohmp3

# asterisk logrotate
cat > /etc/logrotate.d/asterisk <<-END_ROTATE 
/var/log/asterisk/*log {
    missingok
    rotate 5
    weekly
    create 0640 asterisk asterisk
    postrotate
        /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
    endscript
}
/var/log/asterisk/full {
    missingok
    rotate 5
    daily
    create 0640 asterisk asterisk
    postrotate
        /usr/sbin/asterisk -rx 'logger reload' > /dev/null 2> /dev/null
    endscript
}
/var/log/asterisk/cdr-csv/*csv {
    missingok
    rotate 5
    monthly
    create 0640 asterisk asterisk
}
END_ROTATE

# start amportal at boot
echo "/usr/sbin/amportal start" >> /etc/rc.local

# start freepbx
/usr/sbin/amportal start
