#Shell script used to create
#!/bin/bash
#
echo "This script is to install the Dahdi drivers, Asterisk and FreePBX
 on a new install of ubuntu 9.10 (Karmic Koala).

 Must be run with superuser privileges.

 Ubuntu 9.10 and downloaded versions of:
 asterisk-1.6.2.2
 asterisk-addons-1.6.2
 freepbx-2.6.0
 libpri-1.4.10.2
 dahdi-linux-2.2.1
 dahdi-tools-2.2.1

 Asterisk on Ubuntu Edited by:
 John Mathew,
 Mavelikara,
 Kerala,
 India.
 Ph:- +919944227447"

# NOTE: Please change these according to your configuration!!! 
export MYSQL_ROOT_PW=asterix
export ASTERISK_DB_PW=asterix
export IP_ADDRESS=131.193.35.155 
apt-get update
apt-get upgrade
apt-get -y install debconf-utils
debconf-set-selections <<CONF_EOF
debconf debconf/frontend select noninteractive
CONF_EOF
apt-get -y install mysql-server
mysqladmin -u root password ${MYSQL_ROOT_PW}
debconf-set-selections <<CONF_EOF
debconf debconf/frontend select Dialog
CONF_EOF
apt-get -y install linux-headers-`uname -r` --force-yes
apt-get -y install openssh-server
apt-get -y install make
apt-get -y install bison
apt-get -y install flex
apt-get -y install g++
apt-get -y install gcc
apt-get -y install apache2
apt-get -y install php5
apt-get -y install php5-curl
apt-get -y install php5-cli
apt-get -y install php5-mysql
apt-get -y install php-pear
apt-get -y install php-db
apt-get -y install php5-gd
apt-get -y install curl 
apt-get -y install sox
apt-get -y install libncurses5-dev
apt-get -y install libssl-dev
apt-get -y install libmysqlclient15-dev
apt-get -y install mpg123
cd /usr/src
wget http://downloads.asterisk.org/pub/telephony/dahdi-linux-complete/releases/dahdi-linux-complete-2.2.1+2.2.1.tar.gz
tar -xvzf dahdi-linux-complete-2.2.1+2.2.1.tar.gz
cd dahdi-linux-complete-2.2.1+2.2.1
make all
make install
make config
cd ..
wget http://downloads.asterisk.org/pub/telephony/libpri/releases/libpri-1.4.10.2.tar.gz
tar xfz libpri-1.4.10.2.tar.gz
cd libpri-1.4.10.2
make
make install
cd ..
wget http://downloads.asterisk.org/pub/telephony/asterisk/releases/asterisk-1.6.2.2.tar.gz
cd /usr/src
tar xfz asterisk-1.6.2.2.tar.gz 
cd asterisk-1.6.2.2
./configure --disable-xmldoc
make
make install
make samples
cd ..
wget http://downloads.asterisk.org/pub/telephony/asterisk/asterisk-addons-1.6.2.0.tar.gz
tar xfz asterisk-addons-1.6.2.0.tar.gz 
cd asterisk-addons-1.6.2.0
./configure
make
make install
cd ..
cd /var/lib/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 ..
cd /usr/src
adduser asterisk --disabled-password --gecos "asterisk PBX"
adduser www-data asterisk
cp /etc/apache2/apache2.conf /etc/apache2/apache2.conf-orig
sed -i "s/\(^User *\)\(.*\)/\1asterisk/" /etc/apache2/apache2.conf
sed -i "s/\(^Group *\)\(.*\)/\1asterisk/" /etc/apache2/apache2.conf
sed -i "s|#!/bin/sh|#!/bin/bash|" /usr/sbin/safe_asterisk
cat > /etc/init.d/asterisk <<-END_STARTUP
#!/bin/bash
set -e
set -a
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Asterisk"
NAME=amportal
DAEMON=/usr/sbin/\$NAME

test -x \$DAEMON || exit 0

d_start() {
	amportal start
}

d_stop() {
	amportal stop
}

d_reload() {
	amportal restart
}

case "\$1" in

start)
	echo -n "Starting \$DESC: \$NAME"
	d_start
	echo "."
;;

stop)
	echo -n "Stopping \$DESC: \$NAME"
	d_stop
	echo "."
;;

restart|force-reload)
	echo -n "Restarting \$DESC: \$NAME"
	d_stop
	sleep 10
	d_start
	echo "."
;;

*)

	echo "Usage: \$SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 3
;;

esac

exit 0
END_STARTUP

chmod 755 /etc/init.d/asterisk
update-rc.d asterisk defaults 90 10
wget http://mirror.freepbx.org/freepbx-2.6.0.tar.gz
tar xfz freepbx-2.6.0.tar.gz
cd freepbx-2.6.0
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
cp /etc/php5/apache2/php.ini /etc/php5/apache2/php.ini-orig
sed -i "s/\(upload_max_filesize *= *\)\(.*\)/\120M/" /etc/php5/apache2/php.ini
sed -i "s/\(memory_limit *= *\)\(.*\)/\1100M/" /etc/php5/apache2/php.ini
sed -i "s/\(magic_quotes_gpc *= *\)\(.*\)/\1Off/" /etc/php5/apache2/php.ini
chown asterisk:asterisk /var/run/
chown asterisk:asterisk -R /etc/asterisk
chown asterisk:asterisk -R /var/lib/asterisk
chown asterisk:asterisk -R /var/log/asterisk
chown asterisk:asterisk -R /var/spool/asterisk
chown asterisk:asterisk -R /var/www
sed -i "s/\[directories\](!) .*/[directories]/" /etc/asterisk/asterisk.conf
sed -i "s|astrundir *=> */var/run/asterisk|astrundir => /var/run/asterisk|" /etc/asterisk/asterisk.conf
./start_asterisk start
cp 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 "s|\(AMPWEBROOT=\)/var/www/html|\1/var/www|" /etc/amportal.conf
sed -i "s|\(FOPWEBROOT=\)/var/www/html/panel|\1/var/www/panel|" /etc/amportal.conf
sed -i "s/AMPWEBADDRESS=/AMPWEBADDRESS=${IP_ADDRESS}/" /etc/amportal.conf
./install_amp
/etc/init.d/apache2 restart
/etc/init.d/dahdi start
ln -s /var/lib/asterisk/moh /var/lib/asterisk/mohmp3
amportal start
echo " Note:- Go to /etc/aterisk/asterisk.conf and change line astrundir= /var/run"
echo " U have done it!"
echo " Now, Go to your web browser and type:http://`${IP_ADDRESS}`/admin"
echo " Also, check: http://wiki.ubuntu.com/AsteriskOnUbuntu to learn how to fine tune you PBX!"
echo " bye! "
echo "This script is to install the Dahdi drivers, Asterisk and FreePBX
 on a new install of ubuntu 9.10 (Karmic Koala).

 Must be run with superuser privileges.

 Ubuntu 9.10 and downloaded versions of:
 asterisk-1.6.2.2
 asterisk-addons-1.6.2
 freepbx-2.6.0
 libpri-1.4.10.2
 dahdi-linux-2.2.1
 dahdi-tools-2.2.1

 Asterisk on Ubuntu Edited by:
 John Mathew,
 Mavelikara,
 Kerala,
 India.
 Ph:- +919944227447"
echo " Thanking you John Mathew"